



|
|
Byron's CSC212 Web Site
|
Programming Challenge Archive
Incremental Programming
Card Pairs Version 5
|
|
|
Java Application --
Card Thing App
// General Information
// ---------------------------------------------------
// File: CardThingApp.java
// Type: java application file
// Date: Monday Nov 8, 2000
// Name: Byron Bahr
// Line: Incremental Programming Challenge v5
// Application Description
// ---------------------------------------------------
/*
This is Programming Challenge #4, v5 designed to
provide practice in writing Java programs using
Incremantal Programming techniques. The program
will read 50 pairs of playing cards then determine
and display characteristics of the pairs. Printing
"R" for the same Rank, "S" for the same Suit, and
"*" if neither Rank nor Suit are the same. Version
4 uses code featuring Boolean Logic to determine the
color of the cards. The output prints the color RED
if both cards are red, and Black if both cards are
black in addition to the previous descriptions.
Version 5 prints algebraic calculations results of
the percentage of card characteristics.
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.cards.*;
// Application Class
// ---------------------------------------------------
class CardThingApp
{
static private double sameRankCount = 0;
static private double sameSuitCount = 0;
static private double redPairCount = 0;
static private double blackPairCount = 0;
static public void main (String args[])
{
// Define and Count Card Pairs.
Card c;
Card d;
int i = 1;
while (i <= 50)
{
// Read in Card Pairs.
String cRank = IO.readString();
String cSuit = IO.readString();
c = new Card(cRank, cSuit);
String dRank = IO.readString();
String dSuit = IO.readString();
d = new Card(dRank, dSuit);
c.print();
d.print();
checkRanks(c,d);
checkSuits(c,d);
checkDiff(c,d);
checkColors(c,d);
IO.println();
i = i + 1;
}
displayStatistics();
} // End of Main Method
static private void checkRanks(Card c, Card d)
{
if (c.rank() == d.rank())
{
IO.print(" R");
sameRankCount++;
}
}
static private void checkSuits(Card c, Card d)
{
if (c.suit() == d.suit())
{
IO.print(" S");
sameSuitCount++;
}
}
static private void checkDiff(Card c, Card d)
{
if (c.rank() != d.rank())
{
if (c.suit() != d.suit())
{
IO.print(" *");
}
}
}
static private void checkColors(Card c,Card d)
{
checkRed(c,d);
checkBlack(c,d);
}
static private void checkRed(Card c,Card d)
{
boolean cHeart = (c.suit() == Card.HEART);
boolean cDiamond = (c.suit() == Card.DIAMOND);
boolean dHeart = (d.suit() == Card.HEART);
boolean dDiamond = (d.suit() == Card.DIAMOND);
if ((cHeart || cDiamond)&&(dHeart || dDiamond))
{
IO.print (" RED");
redPairCount++;
}
}
static private void checkBlack(Card c,Card d)
{
boolean cSpade = (c.suit() == Card.SPADE);
boolean cClub = (c.suit() == Card.CLUB);
boolean dSpade = (d.suit() == Card.SPADE);
boolean dClub = (d.suit() == Card.CLUB);
if ((cSpade || cClub)&&(dSpade || dClub))
{
IO.print (" BLACK");
blackPairCount++;
}
}
static private void displayStatistics()
{
double sameRankPercent = (sameRankCount/50)*100;
double sameSuitPercent = (sameSuitCount/50)*100;
double diffRankAndSuitPercent =
((50 - (sameRankCount+sameSuitCount))/50*100);
double redPairPercent = (redPairCount/50)*100;
double blackPairPercent = (blackPairCount/50)*100;
double diffColorPercent =
(50 - (redPairCount+blackPairCount))/50*100;
IO.println("The percent of the card pairs which"
+ " have the same rank is " + sameRankPercent
+ "%.");
IO.println("The percent of the card pairs which"
+ " have the same suit is " + sameSuitPercent
+ "%.");
IO.println("The percent of the card pairs which"
+ " have both cards different ranks and suits is " +
+ diffRankAndSuitPercent + "%.");
IO.println("The percent of the card pairs which"
+ " have both red cards is " + redPairPercent
+ "%.");
IO.println("The percent of the card pairs which"
+ " have both black cards is " + blackPairPercent
+ "%.");
IO.println("The percent of the card pairs which"
+ " have different color cards is "
+ diffColorPercent + " %.");
}
}
// Demo
// ------------------------------------------------------
//3456789112345678921234567893123456789412345678951234567
/*
bahr - v5 > javac CardThingApp.java
bahr - 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
The percent of the card pairs which have the same
rank is 28.000000000000004%.
The percent of the card pairs which have the same
suit is 22.0%.
The percent of the card pairs which have both cards
different ranks and suits is 50.0%.
The percent of the card pairs which have both red
cards is 32.0%.
The percent of the card pairs which have both black
cards is 34.0%.
The percent of the card pairs which have different
color cards is 34.0 %.
*/
|
|
|