P Dunn's Super CS1 Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
P Dunn's Super CS1 Site  
 
 
Programming Challenge Archive

Incremental Programming
version 4 of Card Thing
 
 
  Java Application  -- CardThingApp

   // General Information
   // ---------------------------------------------------
  
   // File:  CardThingApp.java
   // Type:  java application file
   // Date:  Wed Nov  1, 2000
   // Name:  Patrick Dunn
   // Line:  V4 - reads 50 card pair representations
   //        from the input file and displays them
   //        along with displaying if they are ranks
   //        or suits are the same if not display a *
   //        This will also check the colors and
   //        display appropriately.
  
   // Application Description
   // ---------------------------------------------------
  
   /*
     V4 of this program will display the colors of the Cards
     on top of everything that V1->V3 does.
   */
  
   // Required Packages
   // ---------------------------------------------------
  
      import blue.io.*;
      import blue.cards.*;
  
   // Application Class
   // ---------------------------------------------------
  
      class CardThingApp
      {
         static public void main (String args[])
         {
     Card c;  // card 1
     Card d;  // card 2
  
     int counter = 1;  // init counter
     while(counter <= 50)
         {
     // read in the card rank and suit
     String cRank = IO.readString();
     String cSuit = IO.readString();
  
     // create a new card object
     c = new Card(cRank,cSuit);
  
     // read in the card rank and suit
     String dRank = IO.readString();
     String dSuit = IO.readString();
  
     // create a new card object
     d = new Card(dRank,dSuit);
  
     // display the objects
     c.print();
     d.print();
  
     // call the commands to check the suits and
     // ranks and display the R or S if they are
     // the same
     checkRanks(c,d);
     checkSuits(c,d);
  
     // call the command to check the suits and
     // ranks and display an * if neither are 
     // the same
     checkDiff(c,d);
  
     // check the colors
     checkColors(c,d);
  
     IO.println();
  
     // increment the counter
     counter++;
  
         }
         }
  
      static private void checkRanks(Card one,Card two)
      {
      // check to see if the the ranks are the same
      if(one.rank() == two.rank())
      {
      IO.print("     R");
      }
      }
  
      static private void checkSuits(Card one,Card two)
      {
      // check to see if the suits are the same
      if(one.suit() == two.suit())
      {
      IO.print("     S");
      }
      }
  
      static private void checkDiff(Card one,Card two)
      {
      // check to see if the ranks nor suits are the same
      // if so, display an *
      if(one.rank() != two.rank())
      {
      if(one.suit() != two.suit())
      {
      IO.print("     *");
      }
      }
      }
  
      static private void checkColors(Card one,Card two)
      {
      checkRed(one,two);
      checkBlack(one,two);
      }
  
      static private void checkRed(Card one,Card two)
      {
  
      // setup private booleans for holding true/false
      boolean firstCard = false;
      boolean secondCard = false;
  
      // check to see if the first card is a diamond or 
      // a heart, thus it is red
      boolean redHeartOne = (one.suit() == Card.HEART);
      boolean redDiamondOne = (one.suit() == Card.DIAMOND);+
  
      
      // if the first card is red, set firstCard to true
      if(redHeartOne || redDiamondOne)
      {
      firstCard = true;
      }
      else
      {
      firstCard = false;
      }
  
      // check to see if the second card is a diamond or
      // a heart, thus it is red
      boolean redHeartTwo = (two.suit() == Card.HEART);
      boolean redDiamondTwo = (two.suit() == Card.DIAMOND);+
  
      
      // if the second card is red, set secondCard to true
      if(redHeartTwo || redDiamondTwo)
      {
      secondCard = true;
      }
      else
      {
      secondCard = false;
      }
      
      // now see if both first and second card are red
      // if it is, print RED
      if(firstCard && secondCard)
      {
      IO.print("     RED");
      }
      
      }
  
      static private void checkBlack(Card one,Card two)
      {
  
      // setup private booleans for holding true/false
      boolean firstCard;
      boolean secondCard;
  
      // check to see if the first card is a diamond or 
      // a heart, thus it is red
      boolean blackClubOne = (one.suit() == Card.CLUB);
      boolean blackSpadeOne = (one.suit() == Card.SPADE);
      
      // if the first card is red, set firstCard to true
      if(blackClubOne || blackSpadeOne)
      {
      firstCard = true;
      }
      else
      {
      firstCard = false;
      }
  
      // check to see if the second card is a diamond or
      // a heart, thus it is red
      boolean blackClubTwo = (two.suit() == Card.CLUB);
      boolean blackSpadeTwo = (two.suit() == Card.SPADE);
      
      // if the second card is red, set secondCard to true
      if(blackClubTwo || blackSpadeTwo)
      {
      secondCard = true;
      }
      else
      {
      secondCard = false;
      }
      
      // now see if both first and second card are red
      // if it is, print RED
      if(firstCard && secondCard)
      {
      IO.print("     BLACK");
      }
      
      }
      }
  
   // Demo
   // ---------------------------------------------------
  
   /*
   [patdunn@k6 v4]$ java CardThingApp < ../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
   [patdunn@k6 v4]$ 
   */