CS1 Course Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
CS1 Course Site  
 
 
Programming Challenge Archive

Program Potpouri
Conditional Constructs
 
This program was featured in the lab devoted to our experiences with writing conditional constructs,  as well as to perform a ``program maintenance'' task.
 
  JavaApplication  -- Die App

   // 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)
         {
     // 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.
             */
             d.roll(); d.print();
             if (d.top() == 1)
     {
         IO.print("OK");
     }
         }
  
         private static void rollForOne(Die d)
         {
             d.roll();
             IO.print(d.top() + " ");
             // Task B.
             /*
                Recursively call this command (rollForOne)
                if the top face of Die d is NOT a 1.  
             */
     if (d.top() != 1)
     {
         rollForOne(d);
     }
         }
  
         private static void rollForOdd(Die d)
         {
             d.roll();
             IO.print(d.top() + " ");
             // Task C.
             /*
                Recursively call this command (rollForOd)
                if the top face of Die d is NOT odd.  Use
                the method which follows (odd).
             */
     if (odd(d) != true)
     {
         rollForOdd(d);
     }
         }
  
         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();
             // 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.  
             */
     if (odd(d) == true)
     {
         IO.print("ODD");
     }
     else
     IO.print("EVEN");
         }
  
         private static void 
         rollAndDisplayNumberName(Die d)
         {
             d.roll(); d.print();
             // 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.  
             */
     if (d.top() == 1)
     {
         IO.print(" ONE");
     }
     else if (d.top() == 2)
     {
         IO.print(" TWO");
     }
     else if (d.top() == 3)
     {
         IO.print(" THREE");
     }
     else if (d.top() == 4)
     {
         IO.print(" FOUR");
     }
     else if (d.top() == 5)
     {
         IO.print(" FIVE");
     }
     else
     {
         IO.print(" SIX");
     }
  
         }
  
         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();
                // Task F.
                /*
                   Increment the counter corresponding to
                   the number on the top face of Die d.
                */
        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++;
        }
     }
             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, 4 )
   ( blue, 3 )
   ( blue, 6 )
   ( blue, 6 )
   ( blue, 3 )
   ( blue, 3 )
   ( blue, 5 )
   ( blue, 3 )
   ( blue, 5 )
   ( blue, 1 )OK
  
   ROLLING FOR 'ONE'
   4 5 3 3 1 
   1 
   6 5 1 
   5 2 6 2 5 3 2 6 6 5 2 5 5 3 3 6 6 5 1 
   3 4 5 4 4 5 5 6 1 
   5 5 4 3 4 4 5 5 1 
   4 5 5 2 1 
   1 
   1 
   5 1 
  
   ROLLING FOR ODD NUMBER
   3 
   1 
   3 
   4 4 5 
   2 1 
   1 
   4 4 5 
   5 
   3 
   6 3 
  
  
   ROLL AND CHECK FOR PARITY
   ( blue, 3 )ODD
   ( blue, 3 )ODD
   ( blue, 1 )ODD
   ( blue, 3 )ODD
   ( blue, 2 )EVEN
   ( blue, 3 )ODD
   ( blue, 1 )ODD
   ( blue, 6 )EVEN
   ( blue, 4 )EVEN
   ( blue, 6 )EVEN
  
  
   ROLL AND DISPLAY NAME
   ( blue, 5 ) FIVE
   ( blue, 4 ) FOUR
   ( blue, 6 ) SIX
   ( blue, 6 ) SIX
   ( blue, 4 ) FOUR
   ( blue, 1 ) ONE
   ( blue, 2 ) TWO
   ( blue, 2 ) TWO
   ( blue, 5 ) FIVE
   ( blue, 4 ) FOUR
  
  
   Check fairness...
   1 was rolled 111 times.
   2 was rolled 92 times.
   3 was rolled 109 times.
   4 was rolled 90 times.
   5 was rolled 101 times.
   6 was rolled 599 times.
  
   $ 
   */