



|
|
My Intro to Object-Oriented Programming
|
Programming Challenge Archive
Programming Assignment # 4
Version 6 of Incremental Programming
|
|
|
This programming assignment utilized the programming methodology of
incremental programming
. Each version, from 1 to 6, builds the program a little closer to the desired product.
JavaApplication - Programming Assignment # 4 --
Incremental Programming - v6
// General Information
// ---------------------------------------------------
// File: CardThing.java
// Type: java application file
// Date: Sat Nov 11, 2000
// Name: Jeffrey R. Norkoli
// Line: Programming Assignment #04
// Application Description
// ---------------------------------------------------
/*
Read 50 pairs of cards. Determine each card's
rank and suit. Determine each card's color. Keep
counts of the following: sameRankCount,
sameSuitCount, redPairCount, and blackPairCount.
Display the six desired statistics by manipulating
the counts. This is Version 6 of 6.
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.cards.Card;
// Application Class
// ---------------------------------------------------
class CardThing
{
// Data local to CardThing.
static private int sameRankCount = 0;
static private int sameSuitCount = 0;
static private int redPairCount = 0;
static private int blackPairCount = 0;
static private final int nrPairs = 50;
static public void main (String args[])
{
Card c;
Card d;
int i = 1;
while (i <= 50)
{
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);
checkRanks(c, d);
checkSuits(c, d);
checkColors(c, d);
i = i + 1;
}
displayStatistics();
}
static private void checkRanks(Card c, Card d)
{
// Check for identical ranks and print R if true.
// Keep count of identical ranks.
if (c.rank() == d.rank())
{
sameRankCount = sameRankCount + 1;
}
}
static private void checkSuits(Card c, Card d)
{
// Check for identical suits and print S if true.
// Keep count of identical suits.
if (c.suit() == d.suit())
{
sameSuitCount = sameSuitCount + 1;
}
}
static private void checkColors(Card c, Card d)
{
// Print RED or BLACK if both cards in the pair
// are the same color. Keep count of number
// of pairs with the same colors.
if (checkRed(c, d))
{
redPairCount = redPairCount + 1;
}
if (checkBlack(c, d))
{
blackPairCount = blackPairCount + 1;
}
}
static private boolean checkRed(Card c, Card d)
{
// Method to check each card in pair for RED color.
boolean checkCForRed = ((c.suit() == 106) || (c.suit()+
== 105));
boolean checkDForRed = ((d.suit() == 106) || (d.suit()+
== 105));
boolean checkRed = checkCForRed && checkDForRed;
return checkRed;
}
static private boolean checkBlack(Card c, Card d)
{
// Method to check each card in pair for BLACK color.
boolean checkCForBlack = ((c.suit() == 107) || (c.suit+
() == 104));
boolean checkDForBlack = ((d.suit() == 107) || (d.suit+
() == 104));
boolean checkBlack = checkCForBlack && checkDForBlack;+
return checkBlack;
}
static private void displayStatistics()
{
// Method to compute and display required statistics.
double percentSameRanks = (double)sameRankCount / nrPa+
irs * 100;
double percentSameSuits = (double)sameSuitCount / nrPa+
irs * 100;
double percentDiffRankSuit = 100.00 - (percentSameRank+
s + percentSameSuits);
double percentRedPair = (double)redPairCount / nrPairs+
* 100;
double percentBlackPair = (double)blackPairCount / nrP+
airs * 100;
double percentDiffColor = 100.00 - (percentRedPair + p+
ercentBlackPair);
IO.println();
IO.println("For the 50 pairs of cards, the following s+
tatistics apply:");
IO.println("A. Ranks: " + percentSameRanks + "% of th+
e pairs have the same rank.");
IO.println("B. Suits: " + percentSameSuits + "% of th+
e pairs have the same suit.");
IO.print("C. Ranks and Suits Different: " + pe+
rcentDiffRankSuit + "% of the ");
IO.println("pairs have neither the same rank nor the s+
ame suit.");
IO.println("D. Red: " + percentRedPair + "% of the p+
airs are both red in color.");
IO.print("E. Black: " + percentBlackPair + "% of the +
pairs are both black ");
IO.println("in color.");
IO.print("F. Color is Different: " + percentDiffColor+
+ "% of the pairs are ");
IO.println("different in color.");
}
}
// Demo
// ----------------------------------------------------
/*
$ javac CardThing.java
$ java CardThing <../CardThing.data
For the 50 pairs of cards, the following statistics apply:+
A. Ranks: 28.000000000000004% of the pairs have the same +
rank.
B. Suits: 22.0% of the pairs have the same suit.
C. Ranks and Suits Different: 50.0% of the pairs have nei+
ther the same rank nor the same suit.
D. Red: 32.0% of the pairs are both red in color.
E. Black: 34.0% of the pairs are both black in color.
F. Color is Different: 34.0% of the pairs are different i+
n color.
$
*/
|
|
|