Contents
- ./cards/Card.java
- ./cards/Deck.java
- ./examples/FindDuplicates.java
./cards/Card.java 1/3
[top][prev][next]
package cards;
/**
* Represents a playing card. Demonstrates use of enumerated types, enum.
*/
public class Card {
/**
* Represents the ranks in a deck of playing cards
*/
public enum Rank {
DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE;
}
/**
* Represents the suits in a deck of playing cards
*/
public enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES;
}
// a card won't change its rank or suit after it is created
private final Rank rank;
private final Suit suit;
/**
* Creates a new card object, with the given rank and suit
*
* @param rank
* @param suit
*/
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
/**
* Returns this card's rank
*
* @return card's rank
*/
public Rank rank() {
return rank;
}
/**
* Returns this card's suit
*
* @return card's suit
*/
public Suit suit() {
return suit;
}
/**
* Returns a string representation of this card.
*
* @return string representation of a Card in the form <rank> of
* <suit>
*/
@Override
public String toString() {
// leverages toString() methods of Rank and Suit
return rank + " of " + suit;
}
public static void main(String args[]) {
Card jackOfDiamonds = new Card(Rank.JACK, Suit.DIAMONDS);
Card aceOfDiamonds = new Card(Rank.ACE, Suit.DIAMONDS);
System.out.println(jackOfDiamonds);
if (jackOfDiamonds.sameSuit(aceOfDiamonds)) {
System.out.println(jackOfDiamonds + " and " + aceOfDiamonds
+ " are the same suit.");
}
System.out.println("The rummyValue of " + jackOfDiamonds + " is "
+ jackOfDiamonds.getRummyValue());
}
}
./cards/Deck.java 2/3
[top][prev][next]
package cards;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Represents a deck of playing cards
*
* @author Sara Sprenkle and CS209
*
*/
public class Deck {
// define our variable as an interface variable, List.
// Note that only Card objects can be put into the list or taken out of the
// list.
/** a list of Card objects */
private List<Card> deck= new ArrayList<Card>(); // assign the ArrayList implementation
// to the List variable
/**
* Creates a new, shuffled deck of cards
*/
public Deck() {
restart(true);
}
/**
* Creates a new deck of cards
*
* @param shuffle
* - true if the cards should be shuffled
*/
public Deck(boolean shuffle) {
restart(shuffle);
}
/**
* Restart the deck from the beginning.
*
* @param shuffle
* - true if the cards should be shuffled
*/
public void restart(boolean shuffle) {
// removes all the Cards from the deck
deck.clear();
// Use the enums defined in the Card class
for (Card.Suit suit : Card.Suit.values()) {
for (Card.Rank rank : Card.Rank.values()) {
Card c = new Card(rank, suit);
deck.add(c);
}
}
if (shuffle) {
shuffle();
}
}
/**
* Display the contents of the deck.
*/
public void display() {
for (Card c : deck) {
System.out.println(c);
}
}
/**
* Shuffles the deck of cards
*/
public void shuffle() {
Collections.shuffle(deck);
}
/**
* Draws the first card from the deck, removes it from the deck, and returns
* the chosen card
*
* @return the top card from the deck, which is removed
*/
public Card draw() {
return deck.remove(0);
}
/**
* Returns a list of cards that are drawn (thus from the deck.
*
* @param numCards
* @return a list of cards (of the specified size)
*/
public List<Card> deal(int numCards) {
List<Card> hand = new ArrayList<Card>();
for (int i = 0; i < numCards; i++) {
hand.add(draw());
}
return hand;
}
/**
* @param args
*/
public static void main(String[] args) {
Deck d = new Deck();
d.display();
}
}
./examples/FindDuplicates.java 3/3
[top][prev][next]
/**
*
*/
package examples;
import java.util.HashSet;
import java.util.Set;
/**
* From the array of command-line arguments, identify the duplicates. Example of
* using the Set interface and its implementation.
*
* @author CSCI209
*/
public class FindDuplicates {
/**
* From the array of command-line arguments, identify the duplicates
*
* @param args
*/
public static void main(String[] args) {
Set<String> noDuplicates = new HashSet<String>();
for (String arg : args) {
if (!noDuplicates.add(arg)) {
System.out.println("Found a duplicate: " + arg);
}
}
if (!noDuplicates.isEmpty()) {
System.out.println("The unique arguments are");
for (String nod : noDuplicates) {
System.out.println(nod);
}
}
}
}
Generated by GNU enscript 1.6.4.