



|
|
Byron's CSC212 Web Site
|
Programming Challenge Archive
Program Potpouri ( Lab Programming )
Conditional Constructs
|
|
|
This program from Lab # 8 provides an opportunity to write conditional constructs as a ``Program Maintenance'' task.
JavaApplication --
Die
// 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.
if (d.top() == 1)
{
IO.print("OK");
}
/*
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() + " ");
// Task B.
if (d.top() != 1)
{
rollForOne(d);
}
/*
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() + " ");
// 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 if (d.top() == 6)
{
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 if (d.top() == 6)
{
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
// ---------------------------------------------------
/*
CHECKING FOR 'ONE'
( blue, 4 )
( blue, 5 )
( blue, 2 )
( blue, 2 )
( blue, 1 )OK
( blue, 4 )
( blue, 4 )
( blue, 3 )
( blue, 3 )
( blue, 6 )
ROLLING FOR 'ONE'
5 4 2 4 5 2 4 4 5 6 6 5 2 3 4 1
5 5 6 5 3 4 3 2 2 4 5 6 2 6 3 3 2 3 3 2 4 1
1
4 3 4 4 5 6 2 5 4 1
3 1
3 4 5 4 3 2 1
5 3 6 3 1
1
1
2 3 4 6 6 6 2 5 2 1
ROLLING FOR ODD NUMBER
2 3
4 2 1
1
1
5
5
5
2 3
2 1
2 4 1
ROLL AND CHECK FOR PARITY
( blue, 1 )Odd
( blue, 6 )Even
( blue, 4 )Even
( blue, 1 )Odd
( blue, 4 )Even
( blue, 2 )Even
( blue, 4 )Even
( blue, 2 )Even
( blue, 4 )Even
( blue, 4 )Even
ROLL AND DISPLAY NAME
( blue, 6 )Six
( blue, 2 )Two
( blue, 5 )Five
( blue, 3 )Three
( blue, 6 )Six
( blue, 4 )Four
( blue, 1 )One
( blue, 6 )Six
( blue, 4 )Four
( blue, 3 )Three
Check fairness...
1 was rolled 104 times.
2 was rolled 98 times.
3 was rolled 102 times.
4 was rolled 117 times.
5 was rolled 92 times.
6 was rolled 86 times.
*/
|
|
|