



|
|
Byron's CSC212 Web Site
|
Programming Challenge Archive
Incremental Programming
Card Pairs Version 4
|
|
|
Java Application --
Card Thing App
// General Information
// ---------------------------------------------------
// File: CardThingApp.java
// Type: java application file
// Date: Monday Nov 13, 2000
// Name: Byron Bahr
// Line: Incremental Programming Challenge v4
// Application Description
// ---------------------------------------------------
/*
This is Programming Challenge #4, v4 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.
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.cards.*;
// Application Class
// ---------------------------------------------------
class CardThingApp
{
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;
}
} // End of Main Method
static private void checkRanks(Card c, Card d)
{
if (c.rank() == d.rank())
{
IO.print(" R");
}
}
static private void checkSuits(Card c, Card d)
{
if (c.suit() == d.suit())
{
IO.print(" S");
}
}
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");
}
}
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");
}
}
}
// Demo
// ------------------------------------------------------
//3456789112345678921234567893123456789412345678951234567
/*
bahr - v4 > javac CardThingApp.java
bahr - v4 > 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
*/
|
|
|