Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this assignment you will have to investigate the use of the Java random number generator function, Math.random(). You can find this in the text

For this assignment you will have to investigate the use of the Java random number generator function, Math.random(). You can find this in the text or using the online resources given in the lectures.

A Slot Machine Simulation

Understand the Application

What it Looks Like to the User

The program will loop, asking the user for a bet amount from 0 to 50 (assume dollars, you can use ints orlongs). 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.

Each of the three positions in the string could be one of the following: "BAR", "7", "cherries" or "space".

Each of the three output positions must be generated by your program randomly with probabilities (defined, as always, by final statics):

BAR (38%)

cherries (40%)

space (7%)

7 (15%)

Therefore, cherries should be the most frequent symbol seen and space or 7 the least frequent.

The following combinations should pay the bet as shown (note ORDER MATTERS):

cherries [not cherries] [any] pays 5 bet (5 times the bet)

cherries cherries [not cherries] pays 15 bet

cherries cherries cherries pays 30 bet

BAR BAR BAR pays 50 bet

7 7 7 pays 100 bet

After the pull, display the three strings regardless of the outcome. If the user did not win, tell him/her "Sorry, you lose." If he won, pay him by displaying his winnings (his original bet times the winning factor from the above table). Then, repeat the whole process by requesting another bet amount.

Position counts! If you read the above bullet that contains the warning "ORDER MATTERS", you will see that cherries bar cherries pays 5 while cherries cherries bar pays 15 and bar cherries cherries pays nothing.

A Helper Class: TripleString

For this assignment we use our previous class TripleString. You can use your version, with corrections based on my feedback, or my instructor solution. Either way, be sure that it is corrected before incorporating here. I. e., if my instructor solution has any mistakes, you must fix them before using the class here -- I don't think it does, but it might. We will instantiate TripleString objects that can be used in our main() method and/or the static methods that main() invokes to simulate this casino project.

The Static Foothill Methods

Each static Foothill method that you have to write to simulate this casino app plays a special role. For example, there will be one method that gets the bet from the user and returns it to main():

 public static int getBet() 

Another method will simulate a random pull of the slot machine -- it generates three random strings and returns them as a TripleString object to main():

 public static TripleString pull() 

An output method will be used at the end of each loop-pass when the user needs to see the results of her pull, and receive the news about how much she won (or not):

 public static void display (TripleString thePull, int winnings ) 

We will describe each method -- and a few others -- in the next section.

The Program Spec

The Foothill Static Method Specs

int getBet()

This prompts the user for input and returns the bet amount as a functional return. It should vet the amount before it returns and insist on a legal bet (0 < bet < 50) until it gets one from the user. This method loops. If any other method is used to test for an illegal value or output an error message based on an illegal value, there will be a 4 point penalty. getBet() must return the legal value to the client and not take any other action besides getting the legal amount.

getBet() has to not only acquire the bet from the user, but also do the output that asks for the bet.

This method must not end the program, but return the valid input result, 0, to the client if/when the user provides it. The client, main() will do what it must when it sees a return value of 0 from this method.

TripleString pull()

This method instantiates and returns a TripleString object to the client. The data of the TripleString object has to first be filled with three randomly chosen strings according to the probabilities described in the "Understand the Application" section above. For example, it might return a TripleString object that contains the three strings ["cherries", "BAR" , "space"].

The way it determines and loads the three strings is by using a private helper method, described, next,randString(). So this method, pull() will call the next method randString() three times to get the three strings that will be stored into the TripleString object. Once that's done, pull() just returns the TripleStringobject to the client and its job is done.

String randString()

This private helper method does a little work -- yet is still quite short. It produces and returns a single random string based on the required probabilities. It does this by calling the java Math.random() function and using the return result of that function as a means of deciding which of the four possible strings to return. Take this in stages. Math.random() returns a double between 0 and 1. One idea (but not the only one) is to turn that double into an int between 1 and 1000 using techniques from five weeks ago. Then, decide which of those numbers should trigger a "7", which should trigger a "cherries", etc. based on the desired probabilities. Since a "Bar" should happen 38% the time, which numbers would you want to trigger a "Bar"? Since a "cherries" should happen 40% of the time, which numbers would trigger a "cherries"? So you see, this is a very simple -- and even short -- function, even though it has to be designed carefully. Common sense will go a long way here.

Although this is the only method the requires the constant (finals) that determine the probabilities, those probabilities should be declared as Foothill static finals for easy reference and future changes.

int getPayMultiplier (TripleString thePull)

After main() gets a TripleString object from pull(), which we will call pullString, it needs to know what the payout will be. That's the job of this function. getPayMultiplier() takes the pullString as a parameter, and inspects it to determine what its pay multiplier should be: 5? 15? 100? 0? It does this by looking at the three strings inside the passed-in TripleStringobject and using if statements to determine and return the right value. For example, if all three of the strings are "cherries", which is easily checked using an if statement, then this method returns a pay multiplier of 30. You can use logic like this to create a sequence of if or else if statements that will give you the desired multiplier. However you do it, the method will return one of the values; 0, 5, 15, 30, 50 or 100.

void display (TripleString thePull, int winnings )

This method takes the winnings (a dollar amount) and thePull as parameters and displays the three strings inside thePull along with "sorry - you lost " or "congrats, you won $X."

Where it All Goes

You can create the TripleString class as a non-public class directly in your client Foothill.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, Foothill.java will look like this:

import java.util.*; import java.lang.Math; public class Foothill { // main class stuff ... } class TripleString { // TripleString class stuff ... } 

As you see, TripleString is to be defined after, not within, the Foothill 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: Foothill).

main()'s Workflow

You can debug each of the above methods individually using a test main() that consists of a statement or two. That way you will make sure each component works before trying to write the final main() client.

main() will be a loop controlled by value returned from getBet(). As long as that value is non-zero, we keep playing.

Each time through the loop, we have to call pull() to get the pullString as a return value. Then we need to pass that to getPayMultiplier() to find the multiplier. We then compute the winnings based on the previous information, and finally we display it all using display(). That's all that each loop pass does. So main() is quite neat and clean.

Input Errors

The only place the user can make an input error is in getBet(), so that's the method that deals with such errors. Don't worry about non-numbers. Assume that an integer was entered. But do test for range and only return to main after you have a number in the valid range. getBet() may not decide about ending the program. That's up to main().

Test Run Requirements:

Submit one run that lasts a minimum of 40 pulls, but possibly more (continue reading). At least once enter an illegal amount to make sure that your program handles it correctly. Also, make sure your one run contains both a win of cherries cherries cherries and a win of BAR BAR BAR This may take many runs, but should be accomplished in less than two minutes if your program is written correctly.

General Requirements

Communicate all values as parameters or return values, not through globals (static class variables). The meaning of these terms and examples are contained in the module reading.

Again, getBet() must not only acquire the bet from the user, but also do the output that asks for the bet.

Between your source and your run, report which pull groups that resulted in all cherries and which pulls resulted in all BARs (e.g., "pull groups #33 and #5 resulted in all cherries and pull group #62 resulted in all BARs." You can type this out in your text file manually after you have pasted the source and run in their correct places.

Also, I will emphasize that in keeping with the separation of I/O and computation, we would not have any method other than display() output results to the screen, and display() is called from main(), not from any other method. Similarly, getBet() is the only method that does input. The other methods do no input, no output and do not call any methods that do input or output. Let's keep that idea fresh.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The Previous Assignment needed to do this assignment:

Understand the Application

The assignment is to first create a class called TripleString. TripleString will consist of three private member Strings as its basic data. There will be few instance methods to support that data.

Once defined, we will use it to instantiate TripleString objects that can be used in our main() method. In a later assignment, the TripleString class will help us create a more involved application.

TripleString will contain three private member Strings as its main data: string1, string2, and string3. We will also add a few public static member such as final int MAX_LEN and MIN_LEN. These represents the maximum and minimum length that our class will allow any of its strings to be set to. We can use these static members in the TripleString method whose job it is to test for valid strings (see below).

The Program Spec

Class TripleString Spec

Private Class Instance Members:

String string1

String string2

String string3

All legal strings should be between 1 and 50 characters.

As stated in the modules, we never want to see a literal in our methods. So the class should have staticmembers to hold values for the limits described above, as well as default values for any field that is construct-ed using illegal arguments from the client. These are put in the public staticsection.

Public Class Static Constants (declare to be final):

MIN_LEN = 1

MAX_LEN = 50

DEFAULT_STRING = " (undefined) "

Public Instance Methods

Default Constructor

TripleString() -- a default constructor that initializes all members to DEFAULT_STRING.

Paramteter-Taking Constructor

TripleString(String str1, String str2, String str3) -- a constructor that initializes all members according to the passed parameters. It has to to be sure each String satisfies the class requirement for a member String. It does this, as in the modules, by calling the mutators and taking possible action if a return value is false. If any passed parameter does not pass the test, a default String should be stored in that member.

Mutators/Accessor

set()s and get()s for these members. Mutators in our course are named using the convention as follows: setString1( ... ), setString3( ... ), etc. Likewise with accessors: getString2(). We need an accessor and mutator for each individual String member, so three pairs of methods in this category. Mutators make use of the private helper method validString(). When a mutator detects an invalid String, no action should be taken. The mutator returns false and the existing String stored in that member prior to the call remains in that member, not a new default String.

String toString() - a method that returns a String which contains all the information (three strings) of the TripleString object. This String can be in any format as long as it is understandable and clearly formatted.

Private Static Helper Methods

boolean validString( String str ) -- a helper function that the mutators can use to determine whether aString is legal. This method returns true if the string is not null and its length is between MIN_LEN and MAX_LEN (inclusive). It returns false, otherwise.

Where it All Goes

TripleString should be a class distinct from (and not contained within) your main class (which we call Foothill). You can create the TripleString class as a non-public class directly in your client Foothill.java file. 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: Foothill).

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, Foothill.java will look like this:

import java.util.*; public class Foothill { // main class stuff ... } class TripleString { // TripleString class stuff ... }

As you see, TripleString is to be defined after, not within, the Foothill 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: Foothill).

The Foothill main()

Instantiate four or more TripleString objects, some of them using the default constructor, some using the constructor that takes parameters.

Immediately display all objects.

Mutate one or more members of every object.

Display all objects a second time.

Do two explicit mutator tests. For each, call a mutator in an if/else statement which prints one message if the call is successful and a different message if the call fails.

Make two accessor calls to demonstrate that they work.

More:

Be sure that all output is descriptive. Use labels to differentiate your output sections and full sentences in your mutator/accessor tests.

No user input should be done in this program.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

My previous class TripleString :

public class Foothill { public static void main(String args[]) {

System.out.println("Creating Four Objects");

TripleString t1 = new TripleString(); TripleString t2 = new TripleString(); TripleString t3 = new TripleString("Jasmine", "Billy", "Ryan"); // Fourth object one string with zero length and another one with more than 50 TripleString t4 = new TripleString("Chef", "", "abcdefghijklmnopqrstuvwxyz1234567891011121314151617"); System.out.println(t1.toString()); System.out.println(t2.toString()); System.out.println(t3.toString()); System.out.println(t4.toString());

System.out.println(); System.out.println();

System.out.println("Mutating Objects ");

// Mutating Objects t1.setString1("Beach"); t2.setString2("Mountain"); t3.setString1("Lake"); t3.setString2("River"); t4.setString1("Field"); t4.setString3(">MAX");

System.out.println(); System.out.println("After Mutation objects are ");

// Printing it again System.out.println(t1.toString()); System.out.println(t2.toString()); System.out.println(t3.toString()); System.out.println(t4.toString());

System.out.println(); System.out.println("Explicitly calling mutator and accessor");

// Explicitly calling mutator

if (t1.setString1("")) { System.out.println("Mutator call is successfull"); } else { System.out.println("Mutator call is unsuccessful"); }

// Explicitly call accessor

System.out.println(t1.getString1());

if (t2.setString2("hello")) { System.out.println("Mutator call is successfull"); } else { System.out.println("Mutator call is unsuccessful"); }

// Explicitly call accessor System.out.println(t2.getString2());

} }

class TripleString { public static final String DEFAULT_STRING = "(undefined)"; public static final int MIN_LEN = 1; public static final int MAX_LEN = 50; private String string1; private String string2; private String string3;

public TripleString() { this.string1 = DEFAULT_STRING; this.string2 = DEFAULT_STRING; this.string3 = DEFAULT_STRING; }

public TripleString(String string1, String string2, String string3) { if (validString(string1)) { setString1(string1); } else { this.string1 = DEFAULT_STRING; }

if (validString(string2)) { setString2(string2); } else { this.string2 = DEFAULT_STRING; }

if (validString(string3)) { setString3(string3); } else { this.string3 = DEFAULT_STRING; }

}

public String getString1() { return string1; }

public boolean setString1(String string1) { if (validString(string1)) { this.string1 = string1; return true; } else { return false; } }

public String getString2() { return string2; }

public boolean setString2(String string2) { if (validString(string2)) { this.string2 = string2; return true; } else { return false; }

}

public String getString3() { return string3; }

public boolean setString3(String string3) { if (validString(string3)) { this.string3 = string3; return true; } else { return false; }

}

private static boolean validString(String input) { if (input.length() >= MIN_LEN && input.length() <= MAX_LEN) { return true; } return false; }

public String toString() { return "string1: " + this.string1 + " string2: " + this.string2 + " string3: " + this.string3; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What is the geometry and hybridization of the atom indicated?

Answered: 1 week ago

Question

explain the need for human resource strategies in organisations

Answered: 1 week ago

Question

describe the stages involved in human resource planning

Answered: 1 week ago