



|
|
P Dunn's Super CS1 Site
|
Programming Challenge Archive
Lab Challenge Archive
Conditionals
|
|
Java Application --
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);
if(blue.top() != 1)
{
while(blue.top() != 1)
{
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);
// check to see if odd
if(!odd(blue))
{
while(!odd(blue))
{
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.
*/
}
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).
*/
}
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(d.top()==2 || d.top()==4 || d.top()==6)
{
IO.print(" EVEN");
}
else
{
IO.print(" ODD");
}
}
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
{
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
// ---------------------------------------------------
/*
<?DEMO>
*/
|
|
|