In: Computer Science
Introduction to java: Gambling
Objectives:
•Performing decisions using if and switch statements.
•Using relational and logical operators
•Working with random numbers
Write an application that simulates a gambling slot machine. In the application, perform the following tasks:
1. Prompt the player to enter the amount of money that the player is willing to gamble.
2. Create three random numbers in range from one to four to represent three of the following four objects (Apple, Orange, Cherry and Pineapple).
3. Compare the numbers for equality and calculates the prize.oIf all three numbers are the same, the player wins three times the amount inserted.oIf two objects are the same the player wins back the amount inserted (gets the money back), otherwise the player wins $0.
4. Display the randomly selected objects (fruits names, not integers). Use switch or if else statements to examine the randomly created integers in order to display the corresponding fruits.
5. Display the dollar amount that the player wins
6. Here is an example what the program should do:
Enter the dollar amount inserted into the slot machine: 100
Orange Apple Cherry
Sorry, you lost
Or
Enter the dollar amount inserted into the slot machine: 100
Orange Cherry Cherry
Congratulations, you won $100
Or
Enter the dollar amount inserted into the slot machine: 100
Orange Orange Orange
Congratulations, you won $300
Short Summary:
********* Please upvote the answer and appreciate our time. *********
Source Code:
import java.util.Scanner;
public class Gambling {
/**
* Generates random integer
* @return random integer in range of 1 to 4
*/
public static int getRandomInt() {
int min = 1;
int max = 4;
return (int)(Math.random() * (max -
min + 1) + min);
}
/**
* @param num fruit number
* @return fruit name
*/
public static String getFruit(int num) {
if(num == 1) {
return
"Apple";
}
else if(num == 2) {
return
"Orange";
}
else if(num == 3) {
return
"Cherry";
}
else if(num == 4) {
return
"Pineapple";
}
else {
return
"InvalidFruit";
}
}
public static void main(String[] args) {
// Scanner object to get user
input
Scanner keyboard = new
Scanner(System.in);
//1. Prompt the player to enter the
amount of money that the player is willing to gamble.
System.out.print("Enter the dollar
amount inserted into the slot machine: ");
int dollarAmount =
keyboard.nextInt();
keyboard.nextLine();
//Create three random numbers in
range from one to four to represent
//three of the following four
objects (Apple, Orange, Cherry and Pineapple).
int num1 = getRandomInt();
int num2 = getRandomInt();
int num3 = getRandomInt();
//Compare the numbers for
equality
//calculates the prize.
int sameFruitsCount = 1;
if(num1 == num2) {
sameFruitsCount++;
}
if(num2 == num3) {
sameFruitsCount++;
}
if(num3 == num1) {
sameFruitsCount++;
}
//Display the randomly selected
objects (fruits names, not integers).
System.out.println(getFruit(num1) +
" " + getFruit(num2) + " " + getFruit(num3));
// Display the dollar amount that
the player wins
if(sameFruitsCount >= 3) {
System.out.println("Congratulations, you won $" + (3 *
dollarAmount));
}
//If two objects are the same the
player wins back the amount inserted
//(gets the money back)
else if(sameFruitsCount == 2)
{
System.out.println("Congratulations, you won $" +
dollarAmount);
}
else {
System.out.println("Sorry, you lost");
}
// close the scanner
keyboard.close();
}
}
Sample Run 1:
Sample Run 2:
Sample Run 3:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
**************************************************************************************