CS1 Course Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
CS1 Course Site  
 
 
Programming Challenge Archive

Incremental Programming
Version 5
 
 
  JavaApplication  -- Card Thing

   // General Information
   // ---------------------------------------------------
  
   // File:  CardThing.java
   // Type:  java application file
   // Date:  Fri Nov 10, 2000
   // Name:  James W. Bremenour
   // Line:  Reads 50 pairs of cards and prints them.
  
   // Application Description
   // ---------------------------------------------------
  
   /*
      The fifth in a six part series, this version is 
      the same as the fourth version, however it displays 
      the percents for the same rank, suit, and color.
   */
  
   // Required Packages
   // ---------------------------------------------------
  
      import blue.io.*;
      import blue.cards.*;
  
   // Application Class
   // ---------------------------------------------------
  
      class CardThing
      {
         static public void main (String args[])
         {
         // Declare variables
            Card c;
            Card d;
         // Create a loop that will read, and display
         // each pair.
    for (int i = 1;i < 50;i++)
    {
        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();
    }
    displayStatistics();
         }
         static private double sameRankCount = 0.0;
         static private double sameSuitCount = 0.0;
         static private double redPairCount = 0.0;
         static private double blackPairCount = 0.0;
         static private double allDiffCount = 0.0;
         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("     *");
     allDiffCount++;
         }
     }
         }
         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 cDmnd = (c.suit() == Card.DIAMOND);
             boolean dHeart = (d.suit() == Card.HEART);
             boolean dDmnd = (d.suit() == Card.DIAMOND);
             if ((cHeart || cDmnd) && (dHeart || dDmnd))
             {
                 IO.print("     RED");
         redPairCount++;
             }
         }
         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");
         blackPairCount++;
     }
         }
         static private void displayStatistics()
         {
         double total = (sameRankCount + sameSuitCount 
                        + allDiffCount + redPairCount 
                        + blackPairCount);
         double rankPercent = (sameRankCount/total) *100;
         double suitPercent = (sameSuitCount/total) *100;
         double redPercent = (redPairCount/total) * 100;
         double blackPercent = 
              (blackPairCount/total) * 100;
         IO.print("The rank percentage is: ");
         IO.print(rankPercent + "%");
         IO.println();
         IO.print("The suit percentage is: ");
         IO.print(suitPercent + "%");
         IO.println();
         IO.print("The red pair percentage is: ");
         IO.print(redPercent + "%");
         IO.println();
         IO.print("The black pair percentage is: ");
         IO.print(blackPercent + "%");
         IO.println();
         }
      }
  
   // Demo
   // ---------------------------------------------------
  
   /*
   $ 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
   The rank percentage is: 17.28395061728395%
   The suit percentage is: 12.345679012345679%
   The red pair percentage is: 18.51851851851852%
   The black pair percentage is: 20.98765432098765%
   $ 
   */