Return to Jeff's Main Page

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
My Intro to Object-Oriented Programming  
 
 
Programming Challenge Archive

Programming Assignment # 4
Version 5 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 - v5

   // 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.  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.  Keep counts of the 
      following:  sameRankCount, sameSuitCount, redPairCount,+
    
      and blackPairCount.  Display the six desired statistics+
  
      by manipulating the counts.  This is Version 5 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);
     c.print();
     d.print();
     checkRanks(c, d);
     checkSuits(c, d);
     checkDiff(c, d);
     checkColors(c, d);
     IO.println();
     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())
          {
      IO.print("     R");
      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())
          {
      IO.print("     S");
      sameSuitCount = sameSuitCount + 1;
          }
          }
  
          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.  Keep count of number
      // of pairs with the same colors.
      if (checkRed(c, d))
          {
      IO.print("     RED");
      redPairCount = redPairCount + 1;
          }
  
      if (checkBlack(c, d))
          {
      IO.print("     BLACK");
      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
   (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
  
   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.
   $  
   */