CS1 Course Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
CS1 Course Site  
 
 
Programming Challenge Archive

Incremental Programming
Versoin 6
 
 
  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 final in a six part series, this version is 
      the same as the fifth version, with the exception
      of all the excess output (removes everything but 
      the statistics information).
   */
  
   // 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);
                checkRanks(c, d);
                checkSuits(c, d);
                checkDiff(c, d);
        checkColors(c, d);
    }
    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())
   {
      sameRankCount++;
           }
         }
         static private void checkSuits(Card c, Card d)
         {
           if (c.suit() == d.suit())
           {
      sameSuitCount++;
           }
         }
         static private void checkDiff(Card c, Card d)
         {
             if (c.suit() != d.suit())
             {
                 if (c.rank() != d.rank())
                 {
     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))
             {
         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))
             {
         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
   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%
   $ 
   */