CS1 Course Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
CS1 Course Site  
 
 
Programming Challenge Archive

Conditional Constructs
DieApp
 
 

 
  JavaApplication  -- DieApp

   // General Information
   // ---------------------------------------------------
  
   // File:  TheDieApp.java
   // Type:  java application file
   // Date:  Tue Oct 24, 2000
   // Name:  blue +
   // Line:  Exercise in Conditional Constructions
  
   // Application Description
   // ---------------------------------------------------
  
   /*
      IF, IF-ELSE, MULTIWAY-IF
   */
  
   // Required Packages
   // ---------------------------------------------------
  
      import blue.io.*;
      import blue.chance.*;
  
   // Application Class
   // ---------------------------------------------------
  
      class DieApp
      {
         static public void main (String args[])
         {
         // Create a couple of dice
            Die blue = new Die("blue",6);
  
         // Roll for "1"
            IO.println();
            IO.println("CHECKING FOR 'ONE'");
            for ( int i = 1; i <= 10; i++ )
            {
                rollAndCheckForOne(blue);
                IO.println();
            }
  
         // Roll for "1"
            IO.println();
            IO.println("ROLLING FOR 'ONE'");
            for ( int i = 1; i <= 10; i++ )
            {
                rollForOne(blue);
                IO.println();
            }
  
         // Roll for an odd number
            IO.println();
            IO.println("ROLLING FOR ODD NUMBER");
            for ( int i = 1; i <= 10; i++ )
            {
                rollForOdd(blue);
                IO.println();
            }
            IO.println();
  
         // Roll and display "odd" or "even"
            IO.println();
            IO.println("ROLL AND CHECK FOR PARITY");
            for ( int i = 1; i <= 10; i++ )
            {
                rollAndCheckParity(blue);
                IO.println();
            }
            IO.println();
  
  
         // Generate word name
            IO.println();
            IO.println("ROLL AND DISPLAY NAME");
            for ( int i = 1; i <= 10; i++ )
            {
                rollAndDisplayNumberName(blue);
                IO.println();
            }
            IO.println();
  
            
         // Check for fairness
            IO.println();
            IO.println("Check fairness...");
            checkFairness(blue,600);
            IO.println();
         }
  
         private static void rollAndCheckForOne(Die d)
         {
             d.roll(); d.print();
     if (d.top() == 1)
         IO.println("OK");
             // Task A.
             /*
                Display the word "OK" if the top face of 
                Die d is a 1.  Do not issue any form of
                newline command in the process.
             */
         }
  
         private static void rollForOne(Die d)
         {
             d.roll();
             IO.print(d.top() + " ");
     if (d.top() != 1)
         rollForOne(d);
             // Task B.
             /*
                Recursively call this command (rollForOne)
                if the top face of Die d is NOT a 1.  
             */
         }
  
         private static void rollForOdd(Die d)
         {
             d.roll();
             IO.print(d.top() + " ");
     if (!odd(d))
         rollForOdd(d);
             // Task C.
             /*
                Recursively call this command (rollForOd)
                if the top face of Die d is NOT odd.  Use
                the method which follows (odd).
             */
         }
  
         private static boolean odd(Die d)
         {
             boolean one = d.top() == 1;
             boolean three = d.top() == 3;
             boolean five = d.top() == 5;
             boolean odd = one || three || five;
             return odd;
         }
  
         private static void 
         rollAndCheckParity(Die d)
         {
             d.roll(); d.print();
     if (odd(d))
         IO.println("ODD");
     else if (!odd(d))
         IO.println("EVEN");
             // Task D.
             /*
                Print the word "ODD" if the top face of
                Die d is odd.  Print the word "EVEN"
                if not.  Do not issue any form of newline
                command in the process.  
             */
         }
  
         private static void 
         rollAndDisplayNumberName(Die d)
         {
             d.roll(); d.print();
     if (d.top() == 1)
         IO.println("ONE");
     else if (d.top() == 2)
         IO.println("TWO");
     else if (d.top() == 3)
         IO.println("THREE");
     else if (d.top() == 4)
         IO.println("FOUR");
     else if (d.top() == 5)
         IO.println("FIVE");
     else if (d.top() == 6)
         IO.println("SIX");
             // Task E.
             /*
                Display the English name of the number on
                the top face of Die d.  Do not issue any 
                form of newline command in the process.  
             */
         }
  
         private static void 
         checkFairness(Die d, int n)
         {
             int count1 = 0;
             int count2 = 0;
             int count3 = 0;
             int count4 = 0;
             int count5 = 0;
             int count6 = 0;
             for ( int i = 1; i < n; i++ )
         {
     d.roll();
     if (d.top() == 1)
         {
     count1++;
         }
     else if (d.top() == 2)
         {
     count2++;
         }
     else if (d.top() == 3)
         {
     count3++;
         }
     else if (d.top() == 4)
         {
     count4++;
         }
     else if (d.top() == 5)
         {
     count5++;
         }
     else if (d.top() == 6)
         {
     count6++;
         }
                // Task F.
                /*
                   Increment the counter corresponding to
                   the number on the top face of Die d.
                */
             }
             IO.println("1 was rolled " + count1 + " times.")+
   ;
             IO.println("2 was rolled " + count2 + " times.")+
   ;
             IO.println("3 was rolled " + count3 + " times.")+
   ;
             IO.println("4 was rolled " + count4 + " times.")+
   ;
             IO.println("5 was rolled " + count5 + " times.")+
   ;
             IO.println("6 was rolled " + count6 + " times.")+
   ;
         }
          
          
      }
  
   // Demo
   // ---------------------------------------------------
  
   /*
     $ java DieApp
     
     CHECKING FOR 'ONE'
     ( blue, 6 )
     ( blue, 2 )
     ( blue, 5 )
     ( blue, 4 )
     ( blue, 3 )
     ( blue, 5 )
     ( blue, 1 )OK
     
     ( blue, 2 )
     ( blue, 6 )
     ( blue, 5 )
     
     ROLLING FOR 'ONE'
     6 3 3 2 2 1
     5 6 2 2 5 1
     5 1
     4 5 1
     4 5 5 2 1
     1
     2 3 4 2 6 2 1
     2 6 5 4 2 1
     3 2 3 2 5 1
     6 6 1
     
     ROLLING FOR ODD NUMBER
     2 3
     6 4 3
     3
     3
     2 3
     3
     3
     2 5
     2 5
     4 3
     
     
     ROLL AND CHECK FOR PARITY
     ( blue, 1 )ODD
     
     ( blue, 2 )EVEN
     
     ( blue, 1 )ODD
     
     ( blue, 1 )ODD
     
     ( blue, 6 )EVEN
     
     ( blue, 3 )ODD
     
     ( blue, 6 )EVEN
     
     ( blue, 1 )ODD
     
     ( blue, 1 )ODD
     
     ( blue, 5 )ODD
     
     ROLL AND DISPLAY NAME
     ( blue, 5 )FIVE
     
     ( blue, 2 )TWO
     
     ( blue, 2 )TWO
     
     ( blue, 2 )TWO
     
     ( blue, 4 )FOUR
     
     ( blue, 4 )FOUR
     
     ( blue, 1 )ONE
     
     ( blue, 5 )FIVE
     
     ( blue, 5 )FIVE
     
     ( blue, 2 )TWO
     
     
     
     Check fairness...
     1 was rolled 102 times.
     2 was rolled 99 times.
     3 was rolled 106 times.
     4 was rolled 90 times.
     5 was rolled 97 times.
     6 was rolled 105 times.
     
     $   
   */