Question

For this lab you will write a Java program that plays the dice game High-Low. In...

For this lab you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totaling 8 or higher), Low (totaling 6 or less) or Sevens (totaling exactly 7). If the player wins, they receive a payout based on the schedule given in the table below:

Choice   Payout
------   ------
High     1 x Wager
Low      1 x Wager
Sevens   4 x Wager

The player will start with $100 for wagering. If the player chooses to wager $0 or if he runs out of money, the program should end. Otherwise it should ask them whether they are wagering on High, Low or Sevens, display the results of the die rolls, and update the money total accordingly. The following transcript shows how the program should work - remember that inputs in BOLD are user inputs:

Enter a random seed: 33
You have 100 dollars.
Enter an amount to bet (0 to quit): 30
High, low or sevens (H/L/S)?: H
Your dice are showing: [4, 4]
Your total is: 8
You won 30 dollars!

You have 130 dollars.
Enter an amount to bet (0 to quit): 30
High, low or sevens (H/L/S)?: h
Your dice are showing: [6, 6]
Your total is: 12
You won 30 dollars!

You have 160 dollars.
Enter an amount to bet (0 to quit): 60
High, low or sevens (H/L/S)?: Sevens
You must enter only H, L or S.
High, low or sevens (H/L/S)?: fdssd
You must enter only H, L or S.
High, low or sevens (H/L/S)?: s
Your dice are showing: [5, 2]
Your total is: 7
You won 240 dollars!

You have 400 dollars.
Enter an amount to bet (0 to quit): 75
High, low or sevens (H/L/S)?: Low
You must enter only H, L or S.
High, low or sevens (H/L/S)?: 
You must enter only H, L or S.
High, low or sevens (H/L/S)?: l
Your dice are showing: [2, 3]
Your total is: 5
You won 75 dollars!

You have 475 dollars.
Enter an amount to bet (0 to quit): 75
High, low or sevens (H/L/S)?: H
Your dice are showing: [3, 6]
Your total is: 9
You won 75 dollars!

You have 550 dollars.
Enter an amount to bet (0 to quit): 1000
Your bet must be between 0 and 550 dollars.
You have 550 dollars.
Enter an amount to bet (0 to quit): -24
Your bet must be between 0 and 550 dollars.
You have 550 dollars.
Enter an amount to bet (0 to quit): 50
High, low or sevens (H/L/S)?: H
Your dice are showing: [3, 1]
Your total is: 4
You lost your bet!

You have 500 dollars.
Enter an amount to bet (0 to quit): 0

You ended the game with 500 dollars left.
Goodbye!


For this assignment you will be writing a series of methods to make your program work. Each of these methods have separate, named test cases that you can use to test whether your method is working properly or not. You should work on the program incrementally, working on each method in turn and testing it to make sure it is working properly. You can use the main method to set up a "scratch" space for testing - just provide the minimal amount of code you need to run your method. You can also submit partially finished code to zyBooks - there are test cases set up that will just test the individual methods even if the whole program is not finished. Once you have all of the individual methods working, your last step will be to write the main method that links them together into a fully working program.

The first method you should write is promptForAmount. This method prints the prompt to the screen asking the user for a dollar amount. If the user enters an amount outside of the range allowed (0 to the maximum amount they have left to bet with), the method should keep prompting them for a valid amount (see the transcript above for the correct error message). This method takes a Scanner object as a parameter - in the full program this will be the Scanner object you create that links to the keyboard for input, so you should use this parameter to read input from the user.

The second method is promptForChoice. This method prints a prompt to the screen asking the user for a choice of 'H', 'L' or 'S' - in either upper or lowercase - and returns the UPPERCASE value of their choice. This method should also loop until it gets a valid input from the user - one single character that is one of the three allowed values. Like promptForAmount, this method takes a Scanner object as a parameter and it must be the same Scanner object you use for promptForAmount.

The third method is rollDice. This method takes a Random object and a number of dice to roll and return an array that contains that number of random values between 1 and 6. For example, if you tell the method to roll 2 dice, it will return an array of length 2. If you tell it to roll 5 dice it will return an array of length 5. As with Scanner, in your main program you will create just one Random object that will be passed to this method every time you need to call it.

The fourth method is displayAndTotalDice. This method takes an array of dice (as created by rollDice) and displays them to the screen as shown in the transcript above ("Die 0 rolls… Die 1 rolls… etc."). It must work for arrays of any length to pass the test cases. The method returns the sum of all of the values in the array.

The fifth and final method is determineWinnings. This method takes the user's choice, the total of the two dice, and the amount bet, and returns back a value based on the payout table above. For example, if the user chose 'H' and rolled an 8 or higher, the method returns back the positive amount of their bet. On the other hand, if the user chose 'H' and rolled a 6 or less, the method would return back the negative amount of their bet (representing a loss).

Once you have all five methods working, you can write the main method. The main method should be a fairly short method that uses the above five methods to produce a transcript like the one seen above. You may if you choose create additional methods beyond the required ones to complete this project - if you do, you MUST document them as discussed in class (and as shown in the code skeleton below).

For this assignment you must start with the following "skeleton" of Java code. Create a class named HiLowSevens.java and then paste the following code directly into your project:

/**
 * DESCRIPTION OF PROGRAM HERE
 * @author YOUR NAME HERE
 * @version DATE HERE
 *
 */
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class HiLowSevens {

    /**
     * Prompts the user for an amount to bet. Ensures that the amount will be
     * between 0 and the maximum number of dollars they have available.
     *
     * @param in
     *            A Scanner to provide input
     * @param maxDollars
     *            the maximum number of dollars available
     * @return the amount to bet, guaranteed to be between 0 and maxDollars
     */
    public static int promptForAmount(Scanner in, int maxDollars) {
        // TODO - complete this function

        // TODO - the following line is only here to allow this program to
        //  compile.  Replace it and remove this comment when you complete
        //  this method.
        return 0;
    }

    /**
     * Prompts the user to enter a single character and ensures that the user
     * must input either an 'H', an 'L' or an 'S'. If they enter a correct
     * character in lowercase, converts it to uppercase.
     *
     * @param in
     *            A Scanner to provide input
     * @return a choice of 'H', 'L' or 'S' guaranteed to be in uppercase.
     */
    public static char promptForChoice(Scanner in) {
        // TODO - complete this function

        // TODO - the following line is only here to allow this program to
        //  compile.  Replace it and remove this comment when you complete
        //  this method.
        return 0;
    }

    /**
     * Returns an array of length numDice where each entry holds a random value
     * between 1 and 6 (representing the roll of a single die).
     *
     * @param rnd
     *            A Random number generator to use
     * @param numDice
     *            number of dice to roll
     * @return an array containing numDice values between 1 and 6
     */
    public static int[] rollDice(Random rnd, int numDice) {
        // TODO - complete this function

        // TODO - the following line is only here to allow this program to
        //  compile.  Replace it and remove this comment when you complete
        //  this method.
        return null;
    }

    /**
     * Returns the sum of all of the values in the array dice. Note that this
     * method should be able to be called with an arbitrary number of dice so do
     * not hardcode it to only work with 2 dice.
     *
     * @param dice
     *            the values to be displayed
     * @return the sum of the values in the array dice
     */
    public static int totalDice(int[] dice) {
        // TODO - complete this function

        // TODO - the following line is only here to allow this program to
        //  compile.  Replace it and remove this comment when you complete
        //  this method.
        return 0;
    }

    /**
     * Returns the amount won or lost according to the choice made, the total on
     * the dice and the bet. If the choice is 'S' and the total is 7 this is 4
     * times the bet. If the choice is 'H' and the total is >=8 this is the same
     * as the bet. If the choice is 'L' and the total is <=6 - it is also the
     * same as the bet Otherwise, the player has lost and this function returns
     * the negative value of their bet.
     *
     * @param choice
     *            One of 'H', 'L', or 'S' - must be uppercase
     * @param total
     *            the total of the dice rolled
     * @param bet
     *            the dollar amount that has been bet
     * @return the correct amount won or lost according to the rules of the
     *         game.
     */
    public static int determineWinnings(char choice, int total, int bet) {
        // TODO - complete this function

        // TODO - the following line is only here to allow this program to
        //  compile.  Replace it and remove this comment when you complete
        //  this method.
        return 0;
    }

    public static void main(String[] args) {
        // TODO - Complete the main method using the functions created
        // above.
    }

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

package com.HomeworkLib.solutions;

import java.util.Random;
import java.util.Scanner;

public class HighLowSevens {


   public static int promptForAmount(Scanner scan,int userAmount) {      
       int betAmount=userAmount+1;
       System.out.print("Enter an amount to bet (0 to quit): ");
       betAmount=scan.nextInt();  
       while(userAmount<betAmount || betAmount<0) {
           System.out.println("Your bet must be between 0 and "+userAmount+" dollars.");
           System.out.print("Enter an amount to bet (0 to quit): ");
           betAmount=scan.nextInt();
       }
       return betAmount;
   }
   public static Character promptForChoice(Scanner scan) {
       System.out.printf("High, low or sevens (H/L/S)?: ");
       String choice=scan.next();
       while(!(choice.equals("H")||choice.equals("h")||choice.equals("L")||choice.equals("l")||choice.equals("S")||choice.equals("s"))) {
           System.out.println("You must enter only H, L or S.");  
           System.out.print("High, low or sevens (H/L/S)?: ");
           choice=scan.next();
       }
       return choice.toUpperCase().charAt(0);
   }
   public static int[] rollDice(Random random,int numDice) {

       int dice[]=new int[numDice];
       for(int i=0;i<numDice;i++) {
           int val=random.nextInt(7);
           while(val==0||val>6)val=random.nextInt(7);
           dice[i]=val;
       }
       return dice;
   }
   public static int displayAndTotalDice(Scanner scan,int[] dice) {
       int total=0;
       System.out.print("Your dice are showing: [");
       int l=0;
       for (int i : dice) {
           if(l==0)System.out.print(""+i);
           else System.out.print(", "+i);
           l++;
           total=total+i;
       }
       System.out.print("]\n");
       System.out.println("Your total is: "+total);
       return total;
   }
   public static int determineWinnings(int amount,int betAmount,char choice,int sumOfDice) {
       int userAmount=amount;
       switch(choice) {
       case 'H':
           if(sumOfDice>7) userAmount=userAmount+betAmount;
           else userAmount=userAmount-betAmount;
           break;      
       case 'L':
           if(sumOfDice>7) userAmount=userAmount+betAmount;
           else userAmount=userAmount-betAmount;
           break;
       case 'S':
           if(sumOfDice==7) userAmount=userAmount+(betAmount*4);
           else userAmount=userAmount-betAmount;
           break;
       }
       if(userAmount>amount)System.out.println("You won "+(userAmount-amount)+" dollars!");
       else System.out.println("You lost your bet!");
       return userAmount;
   }

   public static void main(String args[]) {
       Scanner scan=new Scanner(System.in);
       Random random=new Random();
       System.out.print("Enter a random seed: ");
       int seed=scan.nextInt();

       int userAmount=100;      
       while(userAmount>0) {  
           System.out.println("\nYou have "+userAmount+" dollars.");
           int betAmount=promptForAmount(scan,userAmount);
           if(betAmount<=0)break;          
           userAmount=determineWinnings(userAmount, betAmount, promptForChoice(scan), displayAndTotalDice(scan,rollDice(random, 2)));
       }
       System.out.println("\nYou ended the game with "+userAmount+" dollars left.\r\nGoodbye!");
   }
}

Add a comment
Know the answer?
Add Answer to:
For this lab you will write a Java program that plays the dice game High-Low. In...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Program Note: no break statements or switch staements High, Low, Sevens For this lab you...

    Java Program Note: no break statements or switch staements High, Low, Sevens For this lab you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totaling 8 or higher), Low (totaling 6 or less) or Sevens (totaling exactly 7). If the player wins, they receive a payout based on the schedule given in the table below: Choice Payout ------...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • USE PYTHON ONLY Please write a Python program to let you or the user play the...

    USE PYTHON ONLY Please write a Python program to let you or the user play the game of rolling 2 dice and win/lose money. Initially, you have 100 dollars in your account, and the dealer also has 100 dollars in his/her account. You would be asked to enter a bet to start the game. If your bet is zero, the game would be stopped immediately. Otherwise, dealer would roll 2 dice and get a number from ( random.randint(1, 6) +...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • ********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM For this lab use the program...

    ********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM For this lab use the program at the bottom to sort an array using selection sort. Write a selection sort to report out the sorted low values: lowest to highest. Write another to report out the sorted high values – highest to lowest. Last but not least, the program should output all the values in the array AND then output the 2 requested sorted outputs. -----> MY OLD PROGRAM BELOW...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • This program will implement a simple guessing game... Write a program that will generate a random...

    This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...

  • You will write a single java program called MadLibs. java.

    You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT