Download CLASS AND OBJECT PROGRAMMING EXAMPLES

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CLASS AND OBJECT PROGRAMMING EXAMPLES
Crag
Crag is a dice game involving three dice. In a simple application, write the Java
code that uses the Die class to build the three dice, roll them and print their face up
values.
The algorithm is simply:
build die
roll die
output the die’s face up value
These are repeated three times for each of the three dice. Here’s the application:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Crag
{
public static void main( String [] args
{
// build 3 dice
Die die1 = new Die( );
Die die2 = new Die( );
Die die3 = new Die( );
// roll them
die1.roll( );
die2.roll( );
die3.roll( );
// print them
System.out.println( "Die 1 shows " +
System.out.println( "Die 2 shows " +
System.out.println( "Die 3 shows " +
}
}
Class and Object Programming Examples
)
die1.faceUp );
die2.faceUp );
die3.faceUp );
Page 1
Crag Scoring
Crag players score points by obtaining certain combinations on the three dice. For example, this
table shows three scoring combinations.
Some “Crag” Combinations and their Scores
Name
Crag
Thirteen
Three of a kind
Description
Example
Score
A pair plus another totaling 13
5‒5‒3
50
Any 3 dice totaling 13
6‒5‒2
26
Any combination of three equal dice
2‒2‒2
25
Extend the Crag dice example so that, after rolling the three dice, the application determines
whether or not any of these three combinations are obtained.
The initial algorithm rolls the dice and checks for one of these combinations:
Crag Scoring Algorithm (1st Draft)
build 3 dice
roll the dice
if a “Crag” is obtained then
score = 50
else if “Thirteen” is obtained then
score = 26
else if “Three of a kind” is obtained then
score = 25
else
score= 0
end if
output score
Class and Object Programming Examples
Page 2
We can simplify the coding considerably, however, by realizing that “Crag” is a special case of
“Thirteen.” If we know that the dice add up to thirteen then we can check for the “Crag” by
determining if any two of them are equal. The revised algorithm is shown below.
Crag Scoring Algorithm (2nd Draft)
build 3 dice
roll the dice
if sum of the three dice is 13 then
if any two dice are equal then
score = 50
else
score = 26
end if
else if all three dice are equal then
score = 25
else
score= 0
end if
output score
The completed program is shown below. It makes use of the following Java constructs covered
in subsequent topics: the if-else, discussed in The Java if-else Statement and Cascading If-Else; and
the operators && and ||, discussed in Logical Operators.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class CragScore
{
public static void main( String [] args )
{
int score;
// build 3 dice
Die die1 = new Die( );
Die die2 = new Die( );
Die die3 = new Die( );
// roll them
die1.roll( );
die2.roll( );
die3.roll( );
// print them
Class and Object Programming Examples
Page 3
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
System.out.println( "Die 1 shows " + die1.faceUp );
System.out.println( "Die 2 shows " + die2.faceUp );
System.out.println( "Die 3 shows " + die3.faceUp );
// determine the score
if ( die1.faceUp + die2.faceUp + die3.faceUp == 13 )
if ( die1.faceUp == die2.faceUp
|| die1.faceUp == die3.faceUp
|| die2.faceUp == die3.faceUp )
// "Crag"
score = 50;
else
// ordinary "Thirteen"
score = 26;
else if ( die1.faceUp == die2.faceUp
&& die2.faceUp == die3.faceUp )
// "Three of a kind"
score = 25;
else
score = 0;
System.out.println( "Score is " + score );
}
}
Craps
Gambling experts consider Craps to be one of the best casino games because the house edge is a
small 1.41%, meaning that after playing a long time making many bets, on the average you’ll
lose only 1.41¢ for every dollar that you bet.
For this example, we create a Java application that simulates the Craps game, tossing two dice
and determining if the toss wins or loses. If you know how to play Craps then you should
understand the algorithm below. If you don’t know how to play, look on the Internet for an
explanation. Michael Bluejay’s web site 1 has a particularly lucid explanation and includes a
simulator that allows you to practice the game.
1
Michael Bluejay, How to Play Craps, 2001‒2015 (Accessed 3/28/2015), Available WWW:
http://vegasclick.com/games/craps.html..
Class and Object Programming Examples
Page 4
Craps Algorithm
roll two dice and sum their face up values
if the sum = 7 or 11 then
the player wins
else if the sum = 2,3 or 12 then
the player loses
else
the point = the sum
repeat
roll two dice and sum their face up values
until the sum = the point or 7
if the sum = the point then
the player wins
else
the player loses
end if
end if
The completed program is shown below. It uses the Die class to model the two dice. To make
the program more usable, it outputs the result of each dice roll and the sum of the dice. The big
conditional construct in the algorithm is code using a Java switch statement, which is
discussed in the subsequent topic The Java switch Statement. The loop is coded with a do-while,
which is discussed in The Java do-while Statement.
1
2
3
4
5
6
7
8
9
10
11
public class Craps
{
public static void main( String [ ] args )
{
// declare data
Die die1 = new Die( );
Die die2 = new Die( );
int sum, thePoint;
die1.roll( );
// roll the dice
die2.roll( );
// and sum them
sum = die1.faceUp + die2.faceUp;
Class and Object Programming Examples
Page 5
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
System.out.print( "Come-out roll: " + die1.faceUp );
System.out.print( " + " + die2.faceUp );
System.out.println( " = " + sum );
// evaluate the come-out roll
switch ( sum )
{
case 7: case 11: // player wins
System.out.println( "Player wins" );
break;
case 2: case 3: case 12: // player loses
System.out.println( "Player loses" );
break;
default: // set "the point" and continue
thePoint = sum;
System.out.println( "The point = " + thePoint );
do {
// roll until 7 or thePoint
die1.roll( );
die2.roll( );
sum = die1.faceUp + die2.faceUp;
System.out.print( "Roll: " + die1.faceUp );
System.out.print( " + " + die2.faceUp );
System.out.println( " = " + sum );
} while ( sum != 7 && sum != thePoint );
if ( sum == 7 )
System.out.println( "Player loses" );
else
System.out.println( "Player wins" );
break;
} // end switch
}
}
Class and Object Programming Examples
Page 6