CS1 Course Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
CS1 Course Site  
 
 
Programming Challenge Archive

Incremental Programming
CardThing -- v6
 
 

 
  JavaApplication  -- CardThing

   // General Information
   // ---------------------------------------------------
  
   // File:  CardThing.java
   // Type:  java application file
   // Date:  Thu Nov  9, 2000
   // Name:  Kara Becker
   // Line:  Delete all instructions which serve to display a+
   ny information excepting that generated by the displayStat+
   istics method.
  
   // Application Description
   // ---------------------------------------------------
  
   /*
     Delete all instructions which serve to display any infor+
   mation excepting that generated by the displayStatistics m+
   ethod.
   */
  
   // Required Packages
   // ---------------------------------------------------
  
   import blue.io.*;
   import blue.cards.*;
  
   // Application Class
   // ---------------------------------------------------
  
   class CardThing
   {
  
       static private int sameRankCount = 0;
       static private int sameSuitCount = 0;
       static private int redPairCount = 0;
       static private int blackPairCount = 0;
  
       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);
   checkRanks(c,d);
   checkSuits(c,d);
   checkDiff(c,d);
   checkRed(c,d);
   checkBlack(c,d);
   i = i + 1;
       }
   displayStatistics();
       }
       
       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())
       {
       }
       }
       }
  
       static private void checkColors(Card c,Card d)
       {
   checkRed(c,d);
   checkBlack(c,d);
       }
  
       static private void checkRed(Card c,Card d)
       {
   if ((c.suit() == Card.DIAMOND || c.suit() == Card.HEART) +
   && (d.suit() == Card.DIAMOND || d.suit() == Card.HEART))
       {
   redPairCount ++;
       }
       }
  
       static private void checkBlack(Card c,Card d)
       {
   if ((c.suit() == Card.SPADE || d.suit() == Card.CLUB) && +
   (c.suit() == Card.SPADE || c.suit() == Card.CLUB))
       {
   blackPairCount ++;
       }
       }
  
       static private void displayStatistics()
       {
   double sameRank = (sameRankCount / 50.0) * 100.0;
   double sameSuit = (sameSuitCount / 50.0) * 100.0;
   double redPair = (redPairCount / 50.0) * 100.0;
   double blackPair = (blackPairCount / 50.0) * 100.0;
   double diff = ((50 - sameRankCount - sameSuitCount) / 50.+
   0) * 100.0;
   double diffColor = ((50 - redPairCount - blackPairCount) +
   / 50.0) * 100.0;
   IO.println("sameRank " + sameRank);
   IO.println("sameSuit " + sameSuit);
   IO.println("redPair " + redPair);
   IO.println("blackPair " + blackPair);
   IO.println("diff " + diff);
   IO.println("diffColor " + diffColor);
       }
       
   }
  
   // Demo
   // ---------------------------------------------------
  
   /*
     
     $ javac CardThing.java
     $ java CardThing < CardThing.data
     sameRank 28.000000000000004
     sameSuit 22.0
     redPair 32.0
     blackPair 28.000000000000004
     diff 50.0
     diffColor 40.0
     $
     
   */