



|
|
My Intro to Object-Oriented Programming
|
Programming Challenge Archive
Programming Assignment # 4
Version 4 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 - v4
// General Information
// ---------------------------------------------------
// File: CardThing.java
// Type: java application file
// Date: Wed Nov 8, 2000
// Name: Jeffrey R. Norkoli
// Line: Programming Assignment #04
// Application Description
// ---------------------------------------------------
/*
Read 50 pairs of cards. Display each card's rank and
suit. Place the letter R five spaces after the second
card if the ranks of the two cards are the same and the+
letter S five spaces after the second card on the line
if the suits of the two cards are the same. Place the
character * five spaces after the second card on the
line if neither the ranks nor the suits of the two card+
s
are the same. Place the word RED five spaces after the+
rank/suit classification if both cards are red, and pla+
ce
the word BLACK five spaces after the rank/suit classifi+
-
cation if both cards are black. This is Version 4 of 6+
.
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.cards.Card;
// Application Class
// ---------------------------------------------------
class CardThing
{
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);
checkColors(c, d);
IO.println();
i = i + 1;
}
}
static private void checkRanks(Card c, Card d)
{
// Check for identical ranks and print R if true.
if (c.rank() == d.rank())
{
IO.print(" R");
}
}
static private void checkSuits(Card c, Card d)
{
// Check for identical suits and print S if true.
if (c.suit() == d.suit())
{
IO.print(" S");
}
}
static private void checkDiff(Card c, Card d)
{
// Print * if both ranks and suits don't match.
if (c.rank() != d.rank())
{
if (c.suit() != d.suit())
{
IO.print(" *");
}
}
}
static private void checkColors(Card c, Card d)
{
// Print RED or BLACK if both cards in the pair
// are the same color.
if (checkRed(c, d))
{
IO.print(" RED");
}
if (checkBlack(c, d))
{
IO.print(" BLACK");
}
}
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;
}
}
// 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 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
$
*/
|
|
|