Byron's CSC212 Web Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
Byron's CSC212 Web Site  
 
 
Programming Challenge Archive

Incremental Programming
Card Pairs Version 6
 
 

 
  Java Application  -- Card Thing App

   // General Information
   // ---------------------------------------------------
  
   // File:  CardThingApp.java
   // Type:  java application file
   // Date:  Monday Nov 8, 2000
   // Name:  Byron Bahr
   // Line:  Incremental Programming Challenge v6
  
   // Application Description
   // ---------------------------------------------------
  
   /*
      This is Programming Challenge #4, v6 designed to 
      provide practice in writing Java programs using  
      Incremantal Programming techniques.  The program
      will read 50 pairs of playing cards then determine
      and display characteristics of the pairs. This 
      Versions 6 displays only the percentages of pairs.
      All intermediate data is not displayed
   */
  
   // Required Packages
   // ---------------------------------------------------
  
      import blue.io.*;
      import blue.cards.*;
  
   // Application Class
   // ---------------------------------------------------
  
     class CardThingApp
     {
         static private double sameRankCount = 0;
         static private double sameSuitCount = 0;
         static private double redPairCount = 0;
         static private double blackPairCount = 0;
  
         static public void main (String args[])
       {
        // Define and Count Card Pairs.
           Card c;
           Card d;
           int i = 1;
           while (i <= 50)
       {
         // Read in Card Pairs.
         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();
  
      }  // End of Main Method
  
         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.rank() != d.rank())
           {
       if (c.suit() != d.suit())
             {
   //       IO.print("     *");
             }
           }
         }
         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 cDiamond = (c.suit() == Card.DIAMOND);
   boolean dHeart = (d.suit() == Card.HEART);
   boolean dDiamond = (d.suit() == Card.DIAMOND);
  
           if ((cHeart || cDiamond)&&(dHeart || dDiamond))
          {
   //   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 sameRankPercent = (sameRankCount/50)*100;
          double sameSuitPercent = (sameSuitCount/50)*100;
          double diffRankAndSuitPercent = 
            ((50 - (sameRankCount+sameSuitCount))/50*100);
          double redPairPercent = (redPairCount/50)*100;
          double blackPairPercent = (blackPairCount/50)*100;
          double diffColorPercent = 
            (50 - (redPairCount+blackPairCount))/50*100; 
  
          IO.println("The percent of the card pairs which" 
          + " have the same rank is " + sameRankPercent 
          + "%.");
  
          IO.println("The percent of the card pairs which"
          + " have the same suit is " + sameSuitPercent
          + "%.");
  
          IO.println("The percent of the card pairs which"
          + " have both cards different ranks and suits is " +
  
          + diffRankAndSuitPercent + "%.");
  
          IO.println("The percent of the card pairs which"
          + " have both red cards is " + redPairPercent
          + "%.");
  
          IO.println("The percent of the card pairs which"
          + " have both black cards is " + blackPairPercent
          + "%.");
  
          IO.println("The percent of the card pairs which"
          + " have different color cards is " 
          + diffColorPercent + " %.");
        }      
    }
  
   // Demo 
   // ------------------------------------------------------
   //3456789112345678921234567893123456789412345678951234567
   /*
   bahr - v6 > javac CardThingApp.java
   bahr - v6 > java CardThingApp < ../CardThing.data
  
   The percent of the card pairs which have the same rank
    is 28.000000000000004%.
   The percent of the card pairs which have the same suit
    is 22.0%.
   The percent of the card pairs which have both cards
    different ranks and suits is 50.0%.
   The percent of the card pairs which have both red cards
    is 32.0%.
   The percent of the card pairs which have both black cards
    is 34.0%.
   The percent of the card pairs which have different color
    cards is 34.0 %.
   */