Question

In: Computer Science

Introduction to java: Gambling Objectives: •Performing decisions using if and switch statements. •Using relational and logical...

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

Solutions

Expert Solution

Short Summary:

  • Implemented the gambling program and shown you the sample output.
  • Provided inline comments appropriately

********* 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!!!

**************************************************************************************


Related Solutions

USING C++: Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub...
USING C++: Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub language to be as follows (NOTE: 5 has highest precedence and 0 lowest): 5 *, /, % 4 +, - 3 <, <=, >, >=, !=, == 2 not 1 and 0 or 1.  Infix-Postfix Conversion and Evaluation with Logical and Relational operators – Convert the following infix expression to a postfix expression and evaluate the result (assume that true=1 and false=0). Provide both the...
C language <stdio.h> (Decisions only) (else if and Switch statements) A bank wants an application to...
C language <stdio.h> (Decisions only) (else if and Switch statements) A bank wants an application to help in the loan approval process and has asked us to create it for them. Write a program to ask the user for the loan amount, the annual interest rate (as a percentage, not as a decimal), the length of the loan (in years), and the gross monthly income of the applicant. Validate the inputs per below, and if invalid, inform the user and...
***Using Java Using the switch/case construct, write a program that takes a character from the user...
***Using Java Using the switch/case construct, write a program that takes a character from the user and classifies it as a number (‘1’,’2’, ‘3’, …), a letter from the alphabet (‘A’, ‘a’, ‘B’, ‘b’, …, ‘Z’, ‘z’), an arithmetic operator (‘+’, ‘-‘, ‘/’, ‘*’, ‘%’), a comparison operator (‘<’, ‘>’, ‘=’), punctuation (‘!’, ‘?’, ‘,’, ‘,’, ‘:’, ‘;’), and all other characters as a Special Character, and informs the user of the same. If the user entered the character A,...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
Use JAVA language. Using the switch method concept create a program in which you have an...
Use JAVA language. Using the switch method concept create a program in which you have an artist (singer) and the list of his or her songs. Add 18 songs. Then alter the script to achieve the following in each test run: a) Print all the songs. b) Print only song 15. c) Print only song 19.
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this...
Develop a scenario with a series of logical statements that can be "solved" using k-maps. Once...
Develop a scenario with a series of logical statements that can be "solved" using k-maps. Once you have the scenario and logical statements, translate them into a truth table and use a k-map to build a simplified Boolean equation.
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to...
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to input two numbers using scanner class Print the instruction of the menu for the calculator program Ask user to press 0 to Quit Ask user to press 1 to Add Ask user to press 2 to Substract Ask user to press 3 to Multiply Ask user to press 4 to Divide Perform correct calcuation based on user inputs and print the result Print error...
8. write a program using switch-case statements that ask a user to enter a temperature value...
8. write a program using switch-case statements that ask a user to enter a temperature value in degrees Fahrenheit (In your program, use an integer to record the user’s entry. If the temperature falls between 0 and 96 make it print “Temperature below normal”. If the temperature is 97, 98, make it “Temperature normal”. If the temperature is between 100 and 150, print “You have a fever”. If the temperature is outside the ranges given above, display “Are you human”....
Propositional Logic Using operator properties and other logical equivalences (not truth tables), prove these statements. 1....
Propositional Logic Using operator properties and other logical equivalences (not truth tables), prove these statements. 1. ((p→r)∧(q→r)∧(p∨q))→r (tautology) 2. ¬(q→p)∧(p∧q∧s→r)∧p (contradiction) 3. (p→q)∧(p→r)≡p→(q∧r)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT