Skip to content

Commit eaf96b1

Browse files
committed
review ch12..ch14
1 parent a60b1d1 commit eaf96b1

File tree

11 files changed

+151
-116
lines changed

11 files changed

+151
-116
lines changed

ch12/Card.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,5 @@ public int position() {
7979
public String toString() {
8080
return RANKS[this.rank] + " of " + SUITS[this.suit];
8181
}
82+
8283
}

ch12/CardTable.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77
import javax.swing.JFrame;
88

99
public class CardTable extends Canvas {
10+
1011
private Image[][] images;
1112
private int cardWidth, cardHeight;
1213

13-
// this long is here to suppress a warning; you can read about it at
14-
// http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
15-
static final long serialVersionUID = 1;
16-
1714
/**
1815
* Creates a CardTable.
1916
* cardset is the name of the folder that contains the card images.
@@ -29,7 +26,8 @@ public CardTable(String cardset) {
2926
char c = suits.charAt(suit);
3027

3128
for (int rank = 1; rank <= 13; rank++) {
32-
String s = String.format("%s/%02d%c.gif", cardset, rank, c);
29+
String s = String.format("%s/%02d%c.gif",
30+
cardset, rank, c);
3331
images[rank][suit] = new ImageIcon(s).getImage();
3432
}
3533
}
@@ -79,7 +77,7 @@ public void paint(Graphics g) {
7977

8078
public static void main(String[] args) {
8179
// make the frame
82-
JFrame frame = new JFrame();
80+
JFrame frame = new JFrame("Card Table");
8381
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
8482

8583
// add the CardTable
@@ -91,4 +89,5 @@ public static void main(String[] args) {
9189
frame.pack();
9290
frame.setVisible(true);
9391
}
92+
9493
}

ch12/Search.java

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@
33
*/
44
public class Search {
55

6+
/**
7+
* Make an array of 52 cards.
8+
*/
9+
public static Card[] makeDeck() {
10+
Card[] cards = new Card[52];
11+
int index = 0;
12+
for (int suit = 0; suit <= 3; suit++) {
13+
for (int rank = 1; rank <= 13; rank++) {
14+
cards[index] = new Card(rank, suit);
15+
index++;
16+
}
17+
}
18+
return cards;
19+
}
20+
21+
/**
22+
* Displays the given deck of cards.
23+
*/
24+
public static void printDeck(Card[] cards) {
25+
for (int i = 0; i < cards.length; i++) {
26+
System.out.println(cards[i]);
27+
}
28+
}
29+
630
/**
731
* Sequential search.
832
*/
@@ -24,14 +48,14 @@ public static int binarySearch(Card[] cards, Card target) {
2448
while (low <= high) {
2549
System.out.println(low + ", " + high);
2650

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

30-
if (comp == 0) { // step 2
54+
if (comp == 0) { // step 2
3155
return mid;
32-
} else if (comp < 0) { // step 3
56+
} else if (comp < 0) { // step 3
3357
low = mid + 1;
34-
} else { // step 4
58+
} else { // step 4
3559
high = mid - 1;
3660
}
3761
}
@@ -41,40 +65,25 @@ public static int binarySearch(Card[] cards, Card target) {
4165
/**
4266
* Binary search (recursive version).
4367
*/
44-
public static int binarySearchRec(Card[] cards, Card target,
45-
int low, int high) {
68+
public static int binarySearch(Card[] cards, Card target,
69+
int low, int high) {
4670
System.out.println(low + ", " + high);
4771

4872
if (high < low) {
4973
return -1;
5074
}
51-
int mid = (low + high) / 2; // step 1
75+
int mid = (low + high) / 2; // step 1
5276
int comp = cards[mid].compareTo(target);
5377

54-
if (comp == 0) { // step 2
78+
if (comp == 0) { // step 2
5579
return mid;
56-
} else if (comp < 0) { // step 3
57-
return binarySearchRec(cards, target, mid + 1, high);
58-
} else { // step 4
59-
return binarySearchRec(cards, target, low, mid - 1);
80+
} else if (comp < 0) { // step 3
81+
return binarySearch(cards, target, mid + 1, high);
82+
} else { // step 4
83+
return binarySearch(cards, target, low, mid - 1);
6084
}
6185
}
6286

63-
/**
64-
* Make an array of 52 cards.
65-
*/
66-
public static Card[] makeDeck() {
67-
Card[] cards = new Card[52];
68-
int index = 0;
69-
for (int suit = 0; suit <= 3; suit++) {
70-
for (int rank = 1; rank <= 13; rank++) {
71-
cards[index] = new Card(rank, suit);
72-
index++;
73-
}
74-
}
75-
return cards;
76-
}
77-
7887
/**
7988
* Demonstrates how to call the search methods.
8089
*/
@@ -96,7 +105,8 @@ public static void main(String[] args) {
96105
System.out.println();
97106

98107
System.out.println("Recursive binary search");
99-
System.out.println(binarySearch(cards, jack));
108+
System.out.println(binarySearch(cards, jack, 0, 51));
100109
System.out.println();
101110
}
111+
102112
}

ch13/Deck.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,40 @@ public void print() {
4545
}
4646
}
4747

48+
/**
49+
* Returns a string representation of the deck.
50+
*/
51+
public String toString() {
52+
return Arrays.toString(this.cards);
53+
}
54+
55+
/**
56+
* Chooses a random number between low and high, including both.
57+
*/
58+
public int randomInt(int low, int high) {
59+
return 0;
60+
}
61+
62+
/**
63+
* Swaps the cards at indexes i and j.
64+
*/
65+
public void swapCards(int i, int j) {
66+
}
67+
4868
/**
4969
* Randomly permutes the array of cards.
5070
*/
5171
public void shuffle() {
5272
}
5373

74+
/**
75+
* Finds the index of the lowest card
76+
* between low and high inclusive.
77+
*/
78+
public int indexLowest(int low, int high) {
79+
return 0;
80+
}
81+
5482
/**
5583
* Sorts the cards (in place) using selection sort.
5684
*/
@@ -68,6 +96,13 @@ public Deck subdeck(int low, int high) {
6896
return sub;
6997
}
7098

99+
/**
100+
* Combines two previously sorted subdecks.
101+
*/
102+
public static Deck merge(Deck d1, Deck d2) {
103+
return null;
104+
}
105+
71106
/**
72107
* Returns a sorted copy of the deck using merge sort.
73108
*/
@@ -80,4 +115,5 @@ public Deck mergeSort() {
80115
*/
81116
public void insertionSort() {
82117
}
118+
83119
}

ch13/Test.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ public static void main(String[] args) {
3939
deck.insertionSort();
4040
checkSorted(deck);
4141
}
42+
4243
}

ch14/CardCollection.java

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,46 @@ public CardCollection(String label) {
1818
}
1919

2020
/**
21-
* Returns the label.
21+
* Returns the label of the card collection.
2222
*/
2323
public String getLabel() {
2424
return label;
2525
}
2626

2727
/**
28-
* Returns the number of cards.
28+
* Adds the given card to the collection.
2929
*/
30-
public int size() {
31-
return cards.size();
30+
public void addCard(Card card) {
31+
cards.add(card);
3232
}
3333

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

4141
/**
42-
* Randomly permute the cards.
42+
* Removes and returns the last card.
4343
*/
44-
public void shuffle() {
45-
Random random = new Random();
46-
for (int i = size() - 1; i > 0; i--) {
47-
int j = random.nextInt(i);
48-
swapCards(i, j);
49-
}
44+
public Card popCard() {
45+
int i = size() - 1;
46+
return popCard(i);
5047
}
5148

5249
/**
53-
* Swaps the cards at indexes i and j.
50+
* Returns the number of cards.
5451
*/
55-
public void swapCards(int i, int j) {
56-
Card temp = cards.get(i);
57-
cards.set(i, cards.get(j));
58-
cards.set(j, temp);
52+
public int size() {
53+
return cards.size();
54+
}
55+
56+
/**
57+
* True if the collection is empty, false otherwise.
58+
*/
59+
public boolean empty() {
60+
return cards.size() == 0;
5961
}
6062

6163
/**
@@ -76,13 +78,6 @@ public void dealAll(CardCollection that) {
7678
deal(that, n);
7779
}
7880

79-
/**
80-
* Adds the given card to the collection.
81-
*/
82-
public void addCard(Card card) {
83-
cards.add(card);
84-
}
85-
8681
/**
8782
* Returns the card with the given index.
8883
*/
@@ -99,18 +94,23 @@ public Card last() {
9994
}
10095

10196
/**
102-
* Removes and returns the card with the given index.
97+
* Swaps the cards at indexes i and j.
10398
*/
104-
public Card popCard(int i) {
105-
return cards.remove(i);
99+
public void swapCards(int i, int j) {
100+
Card temp = cards.get(i);
101+
cards.set(i, cards.get(j));
102+
cards.set(j, temp);
106103
}
107104

108105
/**
109-
* Removes and returns the last card.
106+
* Randomly permute the cards.
110107
*/
111-
public Card popCard() {
112-
int i = size() - 1;
113-
return popCard(i);
108+
public void shuffle() {
109+
Random random = new Random();
110+
for (int i = size() - 1; i > 0; i--) {
111+
int j = random.nextInt(i);
112+
swapCards(i, j);
113+
}
114114
}
115115

116116
/**
@@ -120,10 +120,4 @@ public String toString() {
120120
return label + ": " + cards.toString();
121121
}
122122

123-
/**
124-
* Gets the internal cards array (should only be used for testing).
125-
*/
126-
public Card[] getCards() {
127-
return (Card[]) cards.toArray();
128-
}
129123
}

ch14/Deck.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ public Deck(String label) {
1515
}
1616
}
1717
}
18+
1819
}

0 commit comments

Comments
 (0)