Skip to content

Commit

Permalink
up to exercise 11.11
Browse files Browse the repository at this point in the history
  • Loading branch information
karlosmid committed Nov 22, 2011
1 parent 39aaeec commit 3b1ea57
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/thinkjava/CardDeck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package thinkjava;

/**
*
* @author karlo
*/
public class CardDeck {
Card[] deck = null;
public CardDeck(){
this.deck = new Card[52];
}
public void buildDeck(){
int index = 0;
for (int suit = 0; suit <= 3; suit++) {
for (int rank = 1; rank <= 13; rank++) {
this.deck[index] = new Card (suit, rank);
index++;
}
}
}
public void printDeck () {
for (int i=0; i<this.deck.length; i++) {
deck[i].printCard();
}
}
public int findCardLinear (Card card) {
for (int i = 0; i< this.deck.length; i++) {
if (card.deepSameCard(deck[i])) return i;
}
return -1;
}
public int findCardBisect (Card card, int low, int high) {
System.out.println (low + ", " + high);
if (high < low) return -1;
int mid = (high + low) / 2;
int comp = card.compareCard (this.deck[mid]);
if (comp == 0) {
return mid;
} else if (comp > 0) {
return this.findCardBisect (card, low, mid-1);
} else {
return this.findCardBisect (card, mid+1, high);
}
}
}
class Card
{
int suit, rank;
public Card () {
this.suit = 0;
this.rank = 0;
}
public Card (int suit, int rank) {
this.suit = suit; this.rank = rank;
}
public void printCard (){
String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen", "King" };
System.out.println (ranks[this.rank] + " of " + suits[this.suit]);

}
public boolean shallowSameCard(Card c){
return c == this;
}
public boolean deepSameCard (Card c) {
return (c.suit == this.suit && c.rank == this.rank);
}
public int compareCard(Card c){
if (c.suit > this.suit) return 1;
if (c.suit < this.suit) return -1;
if (c.rank > this.rank) return 1;
if (c.rank < this.rank) return -1;
return 0;
}

}

110 changes: 110 additions & 0 deletions test/thinkjava/TestCardDeck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package thinkjava;
import junit.framework.Assert.*;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import thinkjava.CardDeck;
import thinkjava.Card;

/**
*
* @author karlo
*/
public class TestCardDeck extends TestCase{
public TestCardDeck(String name){
super(name);
}
public void testPrintCard(){
Card c = new Card(3,4);
c.printCard();
assertTrue(true);
}
public void testShallowSameCardTrue(){
Card c1 = new Card(3,4);
Card c2 = new Card(3,5);
c2 = c1;
assertTrue(c1.shallowSameCard(c2));
}
public void testShallowSameCardFalse(){
Card c1 = new Card(3,4);
Card c2 = new Card(3,5);
assertFalse(c1.shallowSameCard(c2));
}
public void testDeepSameCardTrue(){
Card c1 = new Card(3,5);
Card c2 = new Card(3,5);
assertTrue(c1.deepSameCard(c2));
}
public void testDeepSameCardFalse(){
Card c1 = new Card(3,4);
Card c2 = new Card(3,5);
assertFalse(c1.deepSameCard(c2));
}
public void testCompareCardInputGreaterInRank(){
Card c1 = new Card(3,4);
Card c2 = new Card(3,5);
TestCardDeck.assertEquals(1,c1.compareCard(c2));
}
public void testCompareCardInputGreaterInSuite(){
Card c1 = new Card(2,4);
Card c2 = new Card(3,5);
TestCardDeck.assertEquals(1,c1.compareCard(c2));
}
public void testCompareCardInputEqual(){
Card c1 = new Card(3,5);
Card c2 = new Card(3,5);
TestCardDeck.assertEquals(0,c1.compareCard(c2));
}
public void testCompareCardInputLessInRank(){
Card c1 = new Card(2,4);
Card c2 = new Card(2,3);
TestCardDeck.assertEquals(-1,c1.compareCard(c2));
}
public void testCompareCardInputLessInSuite(){
Card c1 = new Card(2,4);
Card c2 = new Card(1,5);
TestCardDeck.assertEquals(-1,c1.compareCard(c2));
}
public void testbuildDeck(){
CardDeck carddeck = new CardDeck();
carddeck.buildDeck();
Card c1 = new Card(2,5);
carddeck.deck[30].printCard();
TestCardDeck.assertEquals(0,c1.compareCard(carddeck.deck[30]));
}
public void testPrintDeck(){
CardDeck carddeck = new CardDeck();
carddeck.buildDeck();
carddeck.printDeck();
}
public void testFindCardLinear(){
CardDeck carddeck = new CardDeck();
carddeck.buildDeck();
Card c = new Card(3,6);
TestCardDeck.assertEquals(44, carddeck.findCardLinear(c));
}
public void testFindCardLinearFail(){
CardDeck carddeck = new CardDeck();
carddeck.buildDeck();
Card c = new Card(4,6);
TestCardDeck.assertEquals(-1, carddeck.findCardLinear(c));
}
public void testFindCardBisec(){
CardDeck carddeck = new CardDeck();
carddeck.buildDeck();
Card c = new Card(3,6);
TestCardDeck.assertEquals(44, carddeck.findCardBisect(c,0,carddeck.deck.length-1));
}
public void testFindCardBisecFail(){
CardDeck carddeck = new CardDeck();
carddeck.buildDeck();
Card c = new Card(3,14);
TestCardDeck.assertEquals(-1, carddeck.findCardBisect(c,0,carddeck.deck.length-1));
}
}

0 comments on commit 3b1ea57

Please sign in to comment.