



|
|
P Dunn's Super CS1 Site
|
Programming Challenge Archive
Incremental Programming
version 6 of Card Thing
|
|
Java Application --
CardThingApp
// General Information
// ---------------------------------------------------
// File: CardThingApp.java
// Type: java application file
// Date: Wed Nov 1, 2000
// Name: Patrick Dunn
// Line: Computes various stats on 50 pairs of cards
// Application Description
// ---------------------------------------------------
/*
V6 - This is the final version of the CardThingApp
that displays stats on 50 pairs of cards including:
1)Percentage of pairs that have the same rank
2)Percentage of pairs that have the same suit
3)Percentage of pairs that neither have the same rank
or suit
4)Percentage of pairs that are red
5)Percentage of pairs that are black
6)Percentage of pairs that have neither the same 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);
// 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);
// check the colors
checkColors(c,d);
// 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())
{
// 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())
{
// increment the counter since they are the same
sameSuitCount++;
}
} // end of checkSuits
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)
{
// 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)
{
// 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;
// display header
IO.println(
"CardThingApp v6 - display stats about 50 pairs of card+
s");
// 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 displayStatistics
} // end of CardThingApp class
// Demo
// ---------------------------------------------------
/*
[patdunn@k6 v6]$ java CardThingApp < ../CardThing.data
CardThingApp v6 - display stats about 50 pairs of cards
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 v6]$
*/
|
|
|