



|
|
CS1 Course Site
|
Programming Challenge Archive
Incremental Programming
CardThing -- v5
|
|
|
JavaApplication --
CardThing
// General Information
// ---------------------------------------------------
// File: CardThing.java
// Type: java application file
// Date: Thu Nov 9, 2000
// Name: Kara Becker
// Line: Display the percents called for in the nominal p+
roblem specification.
// Application Description
// ---------------------------------------------------
/*
Display the percents called for in the nominal problem s+
pecification.
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.cards.*;
// Application Class
// ---------------------------------------------------
class CardThing
{
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 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);
c.print();
d.print();
checkRanks(c,d);
checkSuits(c,d);
checkDiff(c,d);
checkRed(c,d);
checkBlack(c,d);
IO.println();
i = i + 1;
}
displayStatistics();
}
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.suit() != d.suit())
{
if (c.rank() != d.rank())
{
IO.print(" *");
}
}
}
static private void checkColors(Card c,Card d)
{
checkRed(c,d);
checkBlack(c,d);
}
static private void checkRed(Card c,Card d)
{
if ((c.suit() == Card.DIAMOND || c.suit() == Card.HEART) +
&& (d.suit() == Card.DIAMOND || d.suit() == Card.HEART))
{
IO.println(" RED");
redPairCount ++;
}
}
static private void checkBlack(Card c,Card d)
{
if ((c.suit() == Card.SPADE || d.suit() == Card.CLUB) && +
(c.suit() == Card.SPADE || c.suit() == Card.CLUB))
{
IO.println(" BLACK");
blackPairCount ++;
}
}
static private void displayStatistics()
{
double sameRank = (sameRankCount / 50.0) * 100.0;
double sameSuit = (sameSuitCount / 50.0) * 100.0;
double redPair = (redPairCount / 50.0) * 100.0;
double blackPair = (blackPairCount / 50.0) * 100.0;
double diff = ((50 - sameRankCount - sameSuitCount) / 50.+
0) * 100.0;
double diffColor = ((50 - redPairCount - blackPairCount) +
/ 50.0) * 100.0;
IO.println("sameRank " + sameRank);
IO.println("sameSuit " + sameSuit);
IO.println("redPair " + redPair);
IO.println("blackPair " + blackPair);
IO.println("diff " + diff);
IO.println("diffColor " + diffColor);
}
}
// Demo
// ---------------------------------------------------
/*
$ javac CardThing.java
$ java CardThing < 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
(Q,S)(6,H) * BLACK
(6,H)(7,D) * RED
(6,C)(6,S) R
(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
(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) * BLACK
(K,D)(10,H) * RED
(3,C)(4,S) *
(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
(7,S)(6,H) * BLACK
(6,C)(7,D) *
(7,S)(6,S) S BLACK
(A,D)(A,C) R
(K,D)(J,H) * RED
(J,C)(10,S) *
(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
sameRank 28.000000000000004
sameSuit 22.0
redPair 32.0
blackPair 28.000000000000004
diff 50.0
diffColor 40.0
$
*/
|
|
|