Contents
- ./cards/Card.java
- ./cards/Deal.java
- ./cards/Deck.java
- ./employee/Employee.java
- ./employee/EmployeeNameComparator.java
./cards/Card.java 1/5
[top][prev][next]
package cards;
/**
* Demonstrates use of "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;
}
private final Rank rank;
private final Suit suit;
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank rank() {
return rank;
}
public Suit suit() {
return suit;
}
/**
* 10, J, Q, K: 10 points A: 15 points all others: 5 points
*
* @return the value of the card in the game of Rummy
*/
public int getRummyValue() {
int rummyValue = 0;
switch (rank) {
case ACE:
rummyValue = 15;
break;
case KING:
case QUEEN:
case JACK:
case TEN:
rummyValue = 10;
break;
default:
rummyValue = 5;
}
return rummyValue;
}
/**
*
* @param c
* another Card to compare
* @return true iff the cards have the same suit
*/
public boolean sameSuit(Card c) {
return c.suit.equals(suit);
// return c.suit == suit; // also works because they're enums
}
/**
* leverages toString() methods of Rank and Suit
*/
public String toString() {
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/Deal.java 2/5
[top][prev][next]
package cards;
import java.util.*;
/**
*
*/
class Deal {
/**
* A little different dealing than before... Does not specify the type of
* objects in the List. Doesn't need to--still works.
*
* @param deck
* @param n
* @return
*/
public static List dealHand(List deck, int n) {
int deckSize = deck.size();
List handView = deck.subList(deckSize - n, deckSize);
List hand = new ArrayList(handView);
handView.clear();
return hand;
}
public static void main(String[] args) {
int numHands = Integer.parseInt(args[0]);
int cardsPerHand = Integer.parseInt(args[1]);
// Make a normal 52-card deck.
String[] suit = new String[] { "Spades", "Hearts", "Diamonds", "Clubs" };
String[] rank = new String[] { "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King" };
List<String> deck = new ArrayList<String>();
// initialize the deck
for (int i = 0; i < suit.length; i++)
for (int j = 0; j < rank.length; j++)
deck.add(rank[j] + " of " + suit[i]);
Collections.shuffle(deck);
for (int i = 0; i < numHands; i++)
System.out.println(dealHand(deck, cardsPerHand));
System.out.println("\n Changing decks ... \n");
List<Card> cardDeck = new ArrayList<Card>();
for (Card.Suit s : Card.Suit.values()) {
for (Card.Rank r : Card.Rank.values()) {
cardDeck.add( new Card(r, s) );
}
}
Collections.shuffle(cardDeck);
for (int i = 0; i < numHands; i++)
System.out.println(dealHand(cardDeck, cardsPerHand));
}
}
./cards/Deck.java 3/5
[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;
/**
* Creates a new, shuffled deck of cards
*/
public Deck() {
// Assign the List variable to the ArrayList implementation.
deck = new ArrayList<Card>();
restart(true);
}
/**
* Creates a new deck of cards
* @param isShuffled - true if the cards should be shuffled
*/
public Deck(boolean isShuffled) {
deck = new ArrayList<Card>();
restart(isShuffled);
}
/**
* Restart the deck from the beginning.
* @param isShuffled - true if the cards should be shuffled
*/
public void restart(boolean isShuffled) {
// 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( isShuffled ) {
shuffle();
}
}
/**
* Display the contents of the deck.
*/
public void display() {
// System.out.println(deck);
for (Card c : deck) {
System.out.println(c);
}
}
/**
*
*/
public void shuffle() {
Collections.shuffle(deck);
}
public Card draw() {
return deck.remove(0);
}
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();
}
}
./employee/Employee.java 4/5
[top][prev][next]
package employee;
import java.util.Arrays;
/**
* @author sprenkle
*
*/
public class Employee implements Comparable<Employee> {
private int id;
private String name;
private int age;
/**
* @param id
* @param name
* @param age
*/
public Employee(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuffer rep = new StringBuffer(getName());
rep.append("\n\t Employee ID: " + this.getId());
rep.append("\n\t Age:" + this.getAge());
return rep.toString();
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id
* the id to set
*/
public void setEmpId(int empId) {
this.id = empId;
}
/**
* @return the employee's name
*/
public String getName() {
return name;
}
/**
* @param name
* -- the name of the employee
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age
* -- the age of the employee
*/
public void setAge(int age) {
this.age = age;
}
/**
* Compares employees by their ids
*/
@Override
public int compareTo(Employee o) {
// note how this simple statement does what we want
return this.id - o.id;
}
public static void main(String[] args) {
Employee a = new Employee(12, "Homer J. Simpson", 38);
Employee b = new Employee(1, "Charles Montgomery Burns", 104);
Employee c = new Employee(3, "Waylon Smithers", 43);
Employee d = new Employee(14, "Carl Carlson", 38);
Employee e = new Employee(15, "Lenny Leonard", 38);
System.out.println("Comparing " + a.getName() + " with " + b.getName()
+ ": " + a.compareTo(b));
System.out.println("\nSorted: ");
Employee[] employees = { a, b, c, d, e };
Arrays.sort(employees);
for (Employee emp : employees) {
System.out.println(emp);
}
}
}
./employee/EmployeeNameComparator.java 5/5
[top][prev][next]
package employee;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @author sprenkle
*
*/
public class EmployeeNameComparator implements Comparator<Employee> {
/**
* Compare by names
*/
@Override
public int compare(Employee o1, Employee o2) {
return o1.getName().compareTo(o2.getName());
}
/**
* @param args
*/
public static void main(String[] args) {
Employee a = new Employee(12, "Homer J. Simpson", 38);
Employee b = new Employee(1, "Charles Montgomery Burns", 104);
Employee c = new Employee(3, "Waylon Smithers", 43);
Employee d = new Employee(14, "Carl Carlson", 38);
Employee e = new Employee(15, "Lenny Leonard", 38);
System.out.println("Comparing " + a.getName() + " with " + b.getName()
+ ": " + a.compareTo(b));
List<Employee> employees = new ArrayList<Employee>();
employees.add(a);
employees.add(b);
employees.add(c);
employees.add(d);
employees.add(e);
System.out.println("\nSorted natural order: ");
Collections.sort(employees);
for (Employee emp : employees) {
System.out.println(emp);
}
System.out.println("\nSorted by name: ");
Collections.sort(employees, new EmployeeNameComparator());
for (Employee emp : employees) {
System.out.println(emp);
}
}
}
Generated by GNU enscript 1.6.4.