Note:
|
|
A roulette table consists of a roulette wheel:
|
and a betting area:
|
mkdir ~/cs170/lab9 cp ~cs170002/share/lab10/*.java ~/cs170/lab9 cd ~/cs170/lab9 |
In this lab, we will use gedit and javac.
|
(0, Green) (9, Red) (18, Red) (27, Red) (36, Red) (1, Red) (10, Black) (19, Red) (28, Black) (00, Green) (2, Black) (11, Black) (20, Black) (29, Black) (3, Red) (12, Red) (21, Red) (30, Red) (4, Black) (13, Black) (22, Black) (31, Black) (5, Red) (14, Red) (23, Red) (32, Red) (6, Black) (15, Black) (24, Black) (33, Black) (7, Red) (16, Red) (25, Red) (34, Red) (8, Black) (17, Black) (26, Black) (35, Black) |
Notice that:
|
We will use 2 arrays:
String[] value; String[] color; |
Take a look at these variables inside the Roulette.java program file:
public class Roulette { public String[] value; // Variables to represent public String[] color; // the Roullete table public int outcome; /* ================================================ Task 1: write the constructor ================================================ */ public Roulette( ) { } .... (other methods omitted) } |
Note:
|
|
|
|
|
|
|
public class Roulette { public String[] value; public String[] color; public int outcome; ..... } |
javac Test1.java javac Test2.java |
You will get compile errors, because the instance variables value, color and outcome can no longer be accessed.
(Notice that before we made the change from public into private, the test programs Test1.java and Test2.java could acces the variables value, color and outcome.
Therefore, we could make changes to these variables !!! In other words, we can ruin the correctness (e.g., change the roulette table that will only spin the number 9 !!!
After changing the access specifiers from public into private, this "trick" is no longer possible !)
public class Roulette { public String[] value; // Store the values of all outcomes public String[] color; public int outcome; // represents the current oucome ... /* ================================================ Task 3: write the value() method ================================================ */ public String value() { return ""; // This return statement is wrong, write a correct one. } } |
|
public class Roulette { public String[] value; public String[] color; // Store colors of all outcomes public int outcome; // represents the current oucome ... /* ================================================ Task 4: write the color() method ================================================ */ public String color() { return ""; // This return statement is wrong, write a correct one. } .... } |
|
public class Test5 { public static void main( String[] args ) { int i; int win=0, N=0; Roulette x = new Roulette( ); System.out.println("Test5: toString method in class Roulette\n"); N = 10; for ( i = 1; i <= N; i++ ) { x.spin(); System.out.println( "x = " + x ); // Converts a Roulette object x to a String !!! } System.out.println(); System.out.println("If you don't see '0 G' or '00 G', run again"); System.out.println(); } } |
The Test5.java program will print a Roulette object as a String
We will show you how to control the printing of objects that you define as a class.
|
Make a note on the output.
/* ================================================ Task 5: write the toString() method ================================================ */ public String toString() // Write this toString method.... { return "Hello World !"; } |
Compile and run Test5.java
javac Test5.java java Test5 |
So, did you notice how the Roulette objects get printed ? (You should see 10 roulette objects printed, but the print out is "Hello World !" which is not very informative about a roulette object).
|
After you have written this method, compile and run Test5.java
javac Test5.java java Test5 |
You should see an output like this:
Test5: toString method in class Roulette x = 17 B x = 5 R x = 3 R x = 34 R x = 17 B x = 00 G x = 22 B x = 0 G x = 18 R x = 34 R If you don't see '0 G' or '00 G', run again |
cd ~/cs170/lab10 /home/cs170002/turnin-lab Roulette.java lab10 |
before midnight today.