



|
|
P Dunn's Super CS1 Site
|
Programming Challenge Archive
Incremental Programming
version 5 of Card Thing
|
|
Java Application --
CardThingApp
// General Information
// ---------------------------------------------------
// File: CardThingApp.java
// Type: java application file
// Date: Wed Nov 1, 2000
// Name: Patrick Dunn
// Line: V5 - reads 50 card pair representations
// from the input file and displays them
// along with displaying if they are ranks
// or suits are the same if not display a *
// This will also check the colors and
// display appropriately. Keeps stats
// on the cards.
// Application Description
// ---------------------------------------------------
/*
V5 - this will do everything V4 does but display
stats on the cards, in percentage form, for the
ranks being the same, suits are the same, neither
are the same, the cards are both red, the cards are
both black and the cards are different in color.
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.cards.*;
// Application Class
// ---------------------------------------------------
class CardThingApp
{
// declare variables for keeping counts
static private int sameRankCount = 0;
static private int sameSuitCount = 0;
static private int redPairCount = 0;
static private int blackPairCount = 0;
static public void main (String args[])
{
Card c; // card 1
Card d; // card 2
int counter = 1; // init counter
while(counter <= 50)
{
// read in the card rank and suit
String cRank = IO.readString();
String cSuit = IO.readString();
// create a new card object
c = new Card(cRank,cSuit);
// read in the card rank and suit
String dRank = IO.readString();
String dSuit = IO.readString();
// create a new card object
d = new Card(dRank,dSuit);
// display the objects
c.print();
d.print();
// call the commands to check the suits and
// ranks and display the R or S if they are
// the same
checkRanks(c,d);
checkSuits(c,d);
// call the command to check the suits and
// ranks and display an * if neither are
// the same
checkDiff(c,d);
// check the colors
checkColors(c,d);
IO.println();
// increment the counter
counter++;
}
// display the statistics
displayStatistics();
} // end of main method
static private void checkRanks(Card one,Card two)
{
// check to see if the the ranks are the same
if(one.rank() == two.rank())
{
IO.print(" R");
// since they are the same increase the count
sameRankCount++;
}
} // end of checkRanks
static private void checkSuits(Card one,Card two)
{
// check to see if the suits are the same
if(one.suit() == two.suit())
{
IO.print(" S");
// increment the counter
sameSuitCount++;
}
} // end of checkSuits
static private void checkDiff(Card one,Card two)
{
// check to see if the ranks nor suits are the same
// if so, display an *
if(one.rank() != two.rank())
{
if(one.suit() != two.suit())
{
IO.print(" *");
}
}
} // end of checkDiff
static private void checkColors(Card one,Card two)
{
checkRed(one,two);
checkBlack(one,two);
} // end of checkColors
static private void checkRed(Card one,Card two)
{
// setup private booleans for holding true/false
boolean firstCard = false;
boolean secondCard = false;
// check to see if the first card is a diamond or
// a heart, thus it is red
boolean redHeartOne = (one.suit() == Card.HEART);
boolean redDiamondOne = (one.suit() == Card.DIAMOND);+
// if the first card is red, set firstCard to true
if(redHeartOne || redDiamondOne)
{
firstCard = true;
}
else
{
firstCard = false;
}
// check to see if the second card is a diamond or
// a heart, thus it is red
boolean redHeartTwo = (two.suit() == Card.HEART);
boolean redDiamondTwo = (two.suit() == Card.DIAMOND);+
// if the second card is red, set secondCard to true
if(redHeartTwo || redDiamondTwo)
{
secondCard = true;
}
else
{
secondCard = false;
}
// now see if both first and second card are red
// if it is, print RED
if(firstCard && secondCard)
{
IO.print(" RED");
// increment the counter
redPairCount++;
}
} // end of checkRed
static private void checkBlack(Card one,Card two)
{
// setup private booleans for holding true/false
boolean firstCard;
boolean secondCard;
// check to see if the first card is a diamond or
// a heart, thus it is red
boolean blackClubOne = (one.suit() == Card.CLUB);
boolean blackSpadeOne = (one.suit() == Card.SPADE);
// if the first card is red, set firstCard to true
if(blackClubOne || blackSpadeOne)
{
firstCard = true;
}
else
{
firstCard = false;
}
// check to see if the second card is a diamond or
// a heart, thus it is red
boolean blackClubTwo = (two.suit() == Card.CLUB);
boolean blackSpadeTwo = (two.suit() == Card.SPADE);
// if the second card is red, set secondCard to true
if(blackClubTwo || blackSpadeTwo)
{
secondCard = true;
}
else
{
secondCard = false;
}
// now see if both first and second card are red
// if it is, print RED
if(firstCard && secondCard)
{
IO.print(" BLACK");
// increment the counter
blackPairCount++;
}
} // end of checkBlack
static private void displayStatistics()
{
// setup variables to compute stats
double rankPercent;
double suitPercent;
double redPairPercent;
double blackPairPercent;
double neitherRanksOrSuits;
double neitherColor;
// compute the rank percentage (50 pairs of cards)
rankPercent = ((double)sameRankCount / 50) * 100;
IO.println("Percentage of ranks that are the same: " +
+
rankPercent + " %");
// compute the suit percentage (50 pairs of cards)
suitPercent = ((double)sameSuitCount / 50) * 100;
IO.println("Percentage of suits that are the same: " +
+
suitPercent + " %");
// compute the percentage of cards that neither the
// ranks or suits are the same
neitherRanksOrSuits = 100 - (rankPercent + suitPercen+
t);
IO.println(
"Percentage of cards that don't have the same ranks o+
r suits: " +
neitherRanksOrSuits + " %");
// compute the red card pair percentage (50 pairs of +
cards)
redPairPercent = ((double)redPairCount / 50) * 100;
IO.println("Percentage of pairs of cards that are red+
" +
redPairPercent + " %");
// compute the black pair card percentage (50 pairs o+
f cards)
blackPairPercent = ((double)blackPairCount / 50) * 10+
0;
IO.println("Percentage of pairs of cards that are bla+
ck: " +
blackPairPercent + " %");
// compute the percentage of cards that neither the
// cards are the same color
neitherColor = 100 - (redPairPercent + blackPairPerce+
nt);
IO.println(
"Percentage of cards that don't have the same color: " +
+
neitherColor + " %");
}
} // end of CardThingApp class
// Demo
// ---------------------------------------------------
/*
[patdunn@k6 v5]$ java CardThingApp < ../CardThing.data
(2,S)(K,S) S BLACK
(K,D)(K,H) R RED
(3,C)(4,D) *
(Q,H)(Q,S) R
(7,D)(J,D) S RED
(2,H)(2,D) R RED
(7,H)(2,S) *
(9,S)(10,C) * BLACK
(J,C)(J,S) R BLACK
(Q,S)(6,H) *
(6,H)(7,D) * RED
(6,C)(6,S) R BLACK
(A,D)(2,C) *
(K,C)(J,H) *
(J,S)(10,S) S BLACK
(J,S)(10,C) * BLACK
(9,D)(9,H) R RED
(5,S)(J,C) * BLACK
(5,H)(6,H) S RED
(9,D)(4,C) *
(8,C)(8,S) R BLACK
(9,D)(10,S) *
(Q,H)(2,H) S RED
(5,C)(6,C) S BLACK
(9,D)(A,H) * RED
(6,S)(K,H) *
(K,D)(10,H) * RED
(3,C)(4,S) * BLACK
(5,H)(Q,D) * RED
(J,C)(J,D) R
(2,H)(2,D) R RED
(7,D)(3,S) *
(9,S)(9,C) R BLACK
(J,C)(J,S) R BLACK
(7,S)(6,H) *
(6,C)(7,D) *
(7,S)(6,S) S BLACK
(A,D)(A,C) R
(K,D)(J,H) * RED
(J,C)(10,S) * BLACK
(J,S)(J,C) R BLACK
(9,D)(5,D) S RED
(5,C)(J,C) S BLACK
(5,H)(6,H) S RED
(9,D)(4,H) * RED
(8,C)(8,D) R
(9,D)(6,S) *
(Q,H)(2,C) *
(5,S)(6,C) * BLACK
(Q,H)(A,H) S RED
Percentage of ranks that are the same: 28.000000000000004 +
%
Percentage of suits that are the same: 22.0 %
Percentage of cards that don't have the same ranks or suit+
s: 50.0 %
Percentage of pairs of cards that are red 32.0 %
Percentage of pairs of cards that are black: 34.0 %
Percentage of cards that don't have the same color: 34.0 %+
[patdunn@k6 v5]$
*/
|
|
|