Question

In: Computer Science

What it Looks Like to the User The program will loop, asking the user for a...

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.

  • 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:
    • space 1/2   (50%)
    • cherries 1/4 (25%)
    • BAR 1/8 (12.5%)
    • 7 1/8 (12.5%)
    • Therefore, space should be the most frequent symbol seen and BAR 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 BARpays 50 × bet
    • 7 7 7 pays 100 × bet

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.

Solutions

Expert Solution

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


Related Solutions

First, write a program to loop asking for a number from the user until the user...
First, write a program to loop asking for a number from the user until the user inputs a zero or until the user has input 50 numbers. Store each value in an array except the values that are divisible by 5 and display all stored values. Determine how many of the values stored in the array were divisible by 10 and display result. Next, write a function getMinMax that accepts an array of floats and the size of the array,...
The program will loop, asking the user for a bet amount from 0 to 50 (assume...
The program will loop, asking the user for a bet amount from 0 to 50 (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...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
Python 10 - (10 pts) - Implement a program that starts by asking the user to...
Python 10 - (10 pts) - Implement a program that starts by asking the user to enter a userID (i.e., a string). The program then checks whether the id entered by the user is in the list of valid users. The current user list is: ['joe', 'sue', jamal, 'sophie'] Depending on the outcome, an appropriate message should be printed. Regardless of the outcome, your function should print 'Done.' before terminating. Here is an example of a successful login: >>> Login:...
Step by step in python Write a program that will keep asking for a user input...
Step by step in python Write a program that will keep asking for a user input (until a blank line is entered) and will inform me whether what I entered was a valid number or not (without crashing). The program should use at least one try/except loop The program should include at least two custom written functions (a main() function can count as one of the two)
Create a matlab program that calculates equivalent cpacitance and charge by asking the user whether the...
Create a matlab program that calculates equivalent cpacitance and charge by asking the user whether the circuit is in paraller , series or combination and then asks for the values of capacitances and voltage. After that it calculates the equivalent capacitance.
1. Write a program that keeps asking the user for a password until they correctly name...
1. Write a program that keeps asking the user for a password until they correctly name it. Once they correctly enter the password, the program congratulates the user and tells them how many guesses it took. Be sure to be grammatically correct with the guess/guesses output. Call the program LastNamePassword. Example (user input in italics) What is the password? monkeys Incorrect. Guess again. dishwasher Incorrect. Guess again. aardvark Correct! You got the password, and it took you 3 guesses to...
Write a short, java program that interacts with the user asking them to buy an xbox...
Write a short, java program that interacts with the user asking them to buy an xbox series x or PS5 and does the following: create at least 2 double variables create at least 1 constant get at least 1 string input from the user get at least 1 int/double input from the user include both, incorporating the input you got from the user: 1 multiway if-else with at least 3 "else if" 1 nested if Your program should combine all...
Python Implement a program that starts by asking the user to enter a login id (i.e.,...
Python Implement a program that starts by asking the user to enter a login id (i.e., a string). The program then checks whether the id entered by the user is in the list ['joe', 'sue', 'hani', 'sophie'] of valid users. Depending on the outcome, an appropriate message should be printed. Regardless of the outcome, your function should print 'Done.' before terminating. Here is an example of a successful login: >>> Login: joe You are in! Done. And here is one...
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT