Return to Jeff's Main Page

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
My Intro to Object-Oriented Programming  
 
 
Programming Challenge Archive

Laboratory Challenges
Conditional Constructs
 
The program below was featured in laboratory on October 27,  2000..   The idea of this laboratory was to write some conditional constructs with ``if'',  ``if-else'',  and ``multiway-if'' statements.  

 
  Laboratory Challenges  -- Conditional Constructs

   // 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();
             // 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.
             */
     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))
         {
     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))
     {
         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 = count1 + 1;
    }
        else if (d.top() == 2)
    {
        count2 = count2 + 1;
    }
        else if (d.top() == 3)
    {
        count3 = count3 + 1;
    }
        else if (d.top() == 4)
    {
        count4 = count4 + 1;
    }
        else if (d.top() == 5)
    {
        count5 = count5 + 1;
    }
        else
    {
        count6 = count6 + 1;
    }
  
    
             }
             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
   // ---------------------------------------------------
  
   /*
   $ javac DieApp.java
   $ java DieApp
  
   CHECKING FOR 'ONE'
   ( blue, 6 )
   ( blue, 5 )
   ( blue, 6 )
   ( blue, 3 )
   ( blue, 6 )
   ( blue, 6 )
   ( blue, 5 )
   ( blue, 3 )
   ( blue, 5 )
   ( blue, 3 )
  
   ROLLING FOR 'ONE'
   3 5 1 
   5 3 2 3 3 2 6 1 
   2 6 1 
   4 5 6 1 
   6 5 5 4 4 1 
   1 
   6 2 1 
   3 4 6 6 4 5 3 3 4 4 3 6 2 5 3 2 6 5 5 5 5 1 
   5 3 4 3 1 
   5 4 1 
  
   ROLLING FOR ODD NUMBER
   6 6 2 2 2 1 
   5 
   3 
   1 
   6 5 
   5 
   1 
   5 
   4 2 6 5 
   4 3 
  
  
   ROLL AND CHECK FOR PARITY
   ( blue, 6 ) EVEN
   ( blue, 3 ) ODD
   ( blue, 6 ) EVEN
   ( blue, 6 ) EVEN
   ( blue, 4 ) EVEN
   ( blue, 1 ) ODD
   ( blue, 5 ) ODD
   ( blue, 6 ) EVEN
   ( blue, 1 ) ODD
   ( blue, 4 ) EVEN
  
  
   ROLL AND DISPLAY NAME
   ( blue, 1 ) one
   ( blue, 5 ) five
   ( blue, 5 ) five
   ( blue, 6 ) six
   ( blue, 2 ) two
   ( blue, 6 ) six
   ( blue, 5 ) five
   ( blue, 4 ) four
   ( blue, 3 ) three
   ( blue, 5 ) five
  
  
   Check fairness...
   1 was rolled 95 times.
   2 was rolled 94 times.
   3 was rolled 105 times.
   4 was rolled 103 times.
   5 was rolled 106 times.
   6 was rolled 96 times.
  
   $  
   */