Skip to content

Commit

Permalink
review ch12..ch14
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisMayfield committed Apr 29, 2016
1 parent a60b1d1 commit eaf96b1
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 116 deletions.
1 change: 1 addition & 0 deletions ch12/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ public int position() {
public String toString() {
return RANKS[this.rank] + " of " + SUITS[this.suit];
}

}
11 changes: 5 additions & 6 deletions ch12/CardTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
import javax.swing.JFrame;

public class CardTable extends Canvas {

private Image[][] images;
private int cardWidth, cardHeight;

// this long is here to suppress a warning; you can read about it at
// http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
static final long serialVersionUID = 1;

/**
* Creates a CardTable.
* cardset is the name of the folder that contains the card images.
Expand All @@ -29,7 +26,8 @@ public CardTable(String cardset) {
char c = suits.charAt(suit);

for (int rank = 1; rank <= 13; rank++) {
String s = String.format("%s/%02d%c.gif", cardset, rank, c);
String s = String.format("%s/%02d%c.gif",
cardset, rank, c);
images[rank][suit] = new ImageIcon(s).getImage();
}
}
Expand Down Expand Up @@ -79,7 +77,7 @@ public void paint(Graphics g) {

public static void main(String[] args) {
// make the frame
JFrame frame = new JFrame();
JFrame frame = new JFrame("Card Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// add the CardTable
Expand All @@ -91,4 +89,5 @@ public static void main(String[] args) {
frame.pack();
frame.setVisible(true);
}

}
66 changes: 38 additions & 28 deletions ch12/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@
*/
public class Search {

/**
* Make an array of 52 cards.
*/
public static Card[] makeDeck() {
Card[] cards = new Card[52];
int index = 0;
for (int suit = 0; suit <= 3; suit++) {
for (int rank = 1; rank <= 13; rank++) {
cards[index] = new Card(rank, suit);
index++;
}
}
return cards;
}

/**
* Displays the given deck of cards.
*/
public static void printDeck(Card[] cards) {
for (int i = 0; i < cards.length; i++) {
System.out.println(cards[i]);
}
}

/**
* Sequential search.
*/
Expand All @@ -24,14 +48,14 @@ public static int binarySearch(Card[] cards, Card target) {
while (low <= high) {
System.out.println(low + ", " + high);

int mid = (low + high) / 2; // step 1
int mid = (low + high) / 2; // step 1
int comp = cards[mid].compareTo(target);

if (comp == 0) { // step 2
if (comp == 0) { // step 2
return mid;
} else if (comp < 0) { // step 3
} else if (comp < 0) { // step 3
low = mid + 1;
} else { // step 4
} else { // step 4
high = mid - 1;
}
}
Expand All @@ -41,40 +65,25 @@ public static int binarySearch(Card[] cards, Card target) {
/**
* Binary search (recursive version).
*/
public static int binarySearchRec(Card[] cards, Card target,
int low, int high) {
public static int binarySearch(Card[] cards, Card target,
int low, int high) {
System.out.println(low + ", " + high);

if (high < low) {
return -1;
}
int mid = (low + high) / 2; // step 1
int mid = (low + high) / 2; // step 1
int comp = cards[mid].compareTo(target);

if (comp == 0) { // step 2
if (comp == 0) { // step 2
return mid;
} else if (comp < 0) { // step 3
return binarySearchRec(cards, target, mid + 1, high);
} else { // step 4
return binarySearchRec(cards, target, low, mid - 1);
} else if (comp < 0) { // step 3
return binarySearch(cards, target, mid + 1, high);
} else { // step 4
return binarySearch(cards, target, low, mid - 1);
}
}

/**
* Make an array of 52 cards.
*/
public static Card[] makeDeck() {
Card[] cards = new Card[52];
int index = 0;
for (int suit = 0; suit <= 3; suit++) {
for (int rank = 1; rank <= 13; rank++) {
cards[index] = new Card(rank, suit);
index++;
}
}
return cards;
}

/**
* Demonstrates how to call the search methods.
*/
Expand All @@ -96,7 +105,8 @@ public static void main(String[] args) {
System.out.println();

System.out.println("Recursive binary search");
System.out.println(binarySearch(cards, jack));
System.out.println(binarySearch(cards, jack, 0, 51));
System.out.println();
}

}
36 changes: 36 additions & 0 deletions ch13/Deck.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,40 @@ public void print() {
}
}

/**
* Returns a string representation of the deck.
*/
public String toString() {
return Arrays.toString(this.cards);
}

/**
* Chooses a random number between low and high, including both.
*/
public int randomInt(int low, int high) {
return 0;
}

/**
* Swaps the cards at indexes i and j.
*/
public void swapCards(int i, int j) {
}

/**
* Randomly permutes the array of cards.
*/
public void shuffle() {
}

/**
* Finds the index of the lowest card
* between low and high inclusive.
*/
public int indexLowest(int low, int high) {
return 0;
}

/**
* Sorts the cards (in place) using selection sort.
*/
Expand All @@ -68,6 +96,13 @@ public Deck subdeck(int low, int high) {
return sub;
}

/**
* Combines two previously sorted subdecks.
*/
public static Deck merge(Deck d1, Deck d2) {
return null;
}

/**
* Returns a sorted copy of the deck using merge sort.
*/
Expand All @@ -80,4 +115,5 @@ public Deck mergeSort() {
*/
public void insertionSort() {
}

}
1 change: 1 addition & 0 deletions ch13/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ public static void main(String[] args) {
deck.insertionSort();
checkSorted(deck);
}

}
72 changes: 33 additions & 39 deletions ch14/CardCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,46 @@ public CardCollection(String label) {
}

/**
* Returns the label.
* Returns the label of the card collection.
*/
public String getLabel() {
return label;
}

/**
* Returns the number of cards.
* Adds the given card to the collection.
*/
public int size() {
return cards.size();
public void addCard(Card card) {
cards.add(card);
}

/**
* True if the collection is empty, false otherwise.
* Removes and returns the card with the given index.
*/
public boolean empty() {
return cards.size() == 0;
public Card popCard(int i) {
return cards.remove(i);
}

/**
* Randomly permute the cards.
* Removes and returns the last card.
*/
public void shuffle() {
Random random = new Random();
for (int i = size() - 1; i > 0; i--) {
int j = random.nextInt(i);
swapCards(i, j);
}
public Card popCard() {
int i = size() - 1;
return popCard(i);
}

/**
* Swaps the cards at indexes i and j.
* Returns the number of cards.
*/
public void swapCards(int i, int j) {
Card temp = cards.get(i);
cards.set(i, cards.get(j));
cards.set(j, temp);
public int size() {
return cards.size();
}

/**
* True if the collection is empty, false otherwise.
*/
public boolean empty() {
return cards.size() == 0;
}

/**
Expand All @@ -76,13 +78,6 @@ public void dealAll(CardCollection that) {
deal(that, n);
}

/**
* Adds the given card to the collection.
*/
public void addCard(Card card) {
cards.add(card);
}

/**
* Returns the card with the given index.
*/
Expand All @@ -99,18 +94,23 @@ public Card last() {
}

/**
* Removes and returns the card with the given index.
* Swaps the cards at indexes i and j.
*/
public Card popCard(int i) {
return cards.remove(i);
public void swapCards(int i, int j) {
Card temp = cards.get(i);
cards.set(i, cards.get(j));
cards.set(j, temp);
}

/**
* Removes and returns the last card.
* Randomly permute the cards.
*/
public Card popCard() {
int i = size() - 1;
return popCard(i);
public void shuffle() {
Random random = new Random();
for (int i = size() - 1; i > 0; i--) {
int j = random.nextInt(i);
swapCards(i, j);
}
}

/**
Expand All @@ -120,10 +120,4 @@ public String toString() {
return label + ": " + cards.toString();
}

/**
* Gets the internal cards array (should only be used for testing).
*/
public Card[] getCards() {
return (Card[]) cards.toArray();
}
}
1 change: 1 addition & 0 deletions ch14/Deck.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public Deck(String label) {
}
}
}

}
Loading

0 comments on commit eaf96b1

Please sign in to comment.