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...
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...
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”....
3. Write a program using switch-case statements that accepts an integer number between 0 and 100...
3. Write a program using switch-case statements that accepts an integer number between 0 and 100 and, based on its value, reports its equivalent letter grade (A, B, C, D, or F).
**** All these methods should be implemented using RECURSIVE solutions (no looping statements) // Java //...
**** All these methods should be implemented using RECURSIVE solutions (no looping statements) // Java // This method takes an integer array as well as an integer (the starting // index) and returns the sum of the squares of the elements in the array. // This method uses recursion. public int sumSquaresRec(int[] A, int pos) { // TODO: implement this method        return -1; // replace this statement with your own return }    // This method takes a...
In what situation would you prefer to implement several alternative logical branches using Multi-Way if/else statements?...
In what situation would you prefer to implement several alternative logical branches using Multi-Way if/else statements? When would you prefer to use Switch?
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 2: Find the largest value of each row of a 2D array             (filename: FindLargestValues.java) Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 1: Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT