In: Computer Science
What it Looks Like to the User
The program will loop, asking the user for a bet amount from 0 to 100 (assume dollars, you can use ints or longs). If the user types a 0 that means she wants to quit. Otherwise, accept the amount as their bet and simulate a slot machine pull. Your program will print out a line that looks like a slot machine result containing three strings. Some examples are: BAR 7 BAR, 7 7 cherries, cherries BAR space, space BAR BAR, or cherries cherries BAR.
The Data
It will contain three private member Strings as its main data: string1, string2, and string3. We will also add a public static member which is to be a final int MAX_LEN set to 20. This represents the maximum length that our class will allow any of its strings to be set to. We can use MAX_LEN in the ThreeString method whose job it is to test for valid strings (see below).
Additionally, we want to keep track of the winnings in an array and then print them out at the end of the program. The static int array will be called pullWinnings and have a size equal to MAX_PULLS (a static final int), which will be set to 40.
boolean saveWinnings(int winnings), and String displayWinnings()
Create two more methods for the pullWinnings array. One will save the winnings from the round, boolean saveWinnings(int winnings), and the other will use a loop to get the values out of the array as well as the total winnings and return a string, String displayWinnings(). Call both methods from the main. saveWinnings() will return a boolean according to whether there was space to save the incoming value of winnings. If it returns false, then have the main stop playing the game. displayWinnings() should also be called from the main, where it will print the returned string.
Where it All Goes
You can create the ThreeString class as a non-public class directly in your client Assig2.java file. You type it directly into that file; do not ask Eclipse to create a new class for you or it will generate a second .java file which we don't want right now. In other words, the file will look like this:
import java.util.*; import java.lang.Math; public class Assig2 { // main class stuff ... } class ThreeString { // ThreeString class stuff ... }
As you see, ThreeString is to be defined after, not within, the Assig2 class definition. This makes it a sibling class, capable of being used by any other classes in the file (of which there happens to be only one: Assig2).
After writing this class, test it using a simple main() which instantiates an object, mutates the members, displays the object, etc. Don't turn this test in. It's part of your development cycle.
import java.util.*;
public class Assig2 {
public static void main(String[] args) {
// TODO Auto-generated method
stub
Scanner sc=new Scanner(System.in);
ThreeString tRef=new ThreeString();
while(true) {
System.out.println("Enter the bet amount between 0 to 100:");
int n=sc.nextInt();
if(n==0) {
break;
}
else {
System.out.println(tRef.ranstring()); //display random
generated String
int amt=tRef.calculateAmt(n);
System.out.println(amt); //Winning amount in amt
if(!tRef.saveWinnings(amt)) {
break;
}
}
}
//System.out.println(tRef.displayWinnings()); //To display the
result of Winnings
sc.close();
}
}
class ThreeString
{
private String string1;
private String string2;
private String string3;
public static final int MAX_LEN=20;
public static final int MAX_PULLS=40;
static int pullWinnings[]=new int[MAX_PULLS];
static int i=0;
String ranstring() {
String str="";
string1=generate();
string2=generate();
string3=generate();
str+=string1+" "+string2+" "+string3;
return str;
}
int calculateAmt(int bet) {
int amt=0;
if(string1.equals("cherries")) {
if(string2.equals("cherries"))
{
if(string3.equals("cherries")) {
amt=30*bet;
}
else {
amt=15*bet;
}
}
else {
amt=5*bet;
}
}
if(string1.equals("BAR") &&
string2.equals("BAR") && string3.equals("BAR")) {
amt=50*bet;
}
if(string1.equals("7") && string2.equals("7")
&& string3.equals("7")) {
amt=100*bet;
}
return amt;
}
String generate() {
String str="";
Random r=new Random();
//random numbers are generated with probabilities
final int ran = r.nextInt(1000);
if (ran > 500) { str="space"; }
else if (ran > 250) { str="cherries"; }
else if(ran>125){ str="BAR"; }
else { str="7"; }
return str;
}
boolean saveWinnings(int winnings) {
if(i<=39) {
pullWinnings[i]=winnings;
i++;
return
true;
}
return false;
}
String displayWinnings() {
String str="";
for(int
j=0;j<pullWinnings.length;j++) {
str+=pullWinnings[i]+" ";
}
return str;
}
}
Sample
output
Screnshot of code