An employer has asked you to develop a game in a Java program that needs to select
Question:
An employer has asked you to develop a game in a Java program that needs to select a number at random between 1 to 10. Then, the user will be prompted to enter a number between 1 to 10 and, if it matches, the randomly selected number they win the game and the loop exists. However, if the do not get the number correct they are told they were too-high, or too-low, and the loop continues asking them to add another number.
In addition to the above, the user is given 'guess-fuel' as an integer value, and each number they enter to make a guess costs guess-fuel in an equal amount. Example., if the guess-fuel is 27, and they guess 10, then they have 17 guess-fuel remaining. Starting guess-fuel is the sum of the numbers from 1 to 10 divided by 2.
The game ends if the user guesses the number, or if they run out of guess-fuel.
- They can enter numbers greater than, or less than the guess-fuel remaining. If their last guess exceeds the guess-fuel remaining they are not stopped, however, the guess-fuel is set to zero of end of game reporting.
Once the game is complete, let them know the number of guesses they made and if they won or lost. Also, ask he user if they would like to play another game (loop) or exit the program.
Further Details
Another programmer has already started this project. Please compete the following .java files (CheckGuessResult.java, Exercise05.java, HighLowGame.java, and User.java) and ensure it runs as expected. Pseudocode is also given for the project.
Note that you can use User.java as is, it will not require any changes. Thanks for any assistance.
CheckGuessResult.java
/* * This class represents the result of checking if the user number matches * the randomly generated number. The field isWin indicates if the user * had won the game on a round of play, true means they won the game, false * means thay they did not win the game. The message field will contain * information, such as "you win", "too low", "too high". * Note that this class is only used as a method return value and does not do any processing itself. */ public class CheckGuessResult { private String message; // status message private boolean isWin; // true is win, false is loose /* * No argument constructor, sets message to "no game" and isWin to false */ public CheckGuessResult() { this("no game", false); } /* * Overloaded Constructor for message and isWin */ public CheckGuessResult(String message, boolean isWin) { this.message = message; this.isWin = isWin; } /* * Accessor for message */ public String getMessage() { return message; } /* * Mutator for message */ public void setMessage(String message) { this.message = message; } /* * accessor for isWin */ public boolean isWin() { return isWin; } /* * mutator for isWin */ public void setWin(boolean isWin) { this.isWin = isWin; } /* * Returns a String representation in the format of * String.format("CheckGuessResult: message %s, isWin %b", message, isWin) */ public String toString() { String report; report = String.format( "CheckGuessResult: message %s, isWin %b", message, isWin); return report; } }
Exercise05.java
/* * Description: Starter Code / Sample Solution Exercise 05 * Starter Code Note: Use the pseudocode and complete the main method */ /* * This class only contains method main, to drive the application. */ public class Exercise05 { /* * Supervisor wants a comment here overviewing the logic in method main */ public static void main(String[] args) { // ToDo: See Pseudocode and / or flowchart } }
HighLowGame.java
// Description: Starter Code / Sample Solution Exercise 05 // Starter Code Note: Use the pseudocode and complete the reset and run methods */ import java.util.Random; /* * Supervisor wants a comment here overviewing the class */ public class HighLowGame { private User user = new User(); // internal only, no get/set private Random random = new Random(); // internal only, no get/set private int fuelAvailable; private int numberToGuess; private static final int MIN = 1; // internal only, no get/set private static final int MAX = 10; // internal only, no get/set /* * No argument constructor, calls method reset to initialize the game */ public HighLowGame() { reset(); } /* * Accessor for fuelAvailable */ public int getFuelAvailable() { return fuelAvailable; } /* * Mutator for fuelAvailable */ public void setFuelAvailable(int fuelAvailable) { this.fuelAvailable = fuelAvailable; } /* * Access for numberToGuess */ public int getNumberToGuess() { return numberToGuess; } /* * Mutator for numberToGuess */ public void setNumberToGuess(int numberToGuess) { this.numberToGuess = numberToGuess; } /* * Supervisor wants comments overviewing the method */ public void reset() { // ToDo: See Pseudocode and / or flowchart } /* * Supervisor wants comments overviewing the method */ public void run() { // ToDo: See Pseudocode and / or flowchart } /* * This method examines the guess to see if it matches the numberToGuess * value. To report win or no-win, with a message as well a CheckGuessResult * instance is created with these results, and the reference to the * CheckGuessResult object is returned. */ private CheckGuessResult checkGuess(int guess) { boolean isWin = false; String message; if (guess == numberToGuess) { message = "You guessed the number"; isWin = true; } else { if(guess < numberToGuess) { message = "too low, "; } else { message = "too high, "; } message = message + String.format( "%d guess-fuel remaining", fuelAvailable); } CheckGuessResult checkGuessResult = new CheckGuessResult(message, isWin); return checkGuessResult; } /* * This method writes out a final result of the game (win or lose) as well * as the number of guesses used by the player (i.e. the user). */ private String reportGameResult(boolean isWon, int guessCount) { String message; if(isWon) { message = "You win! It took you "; } else { message = "You did not win, you used "; } message = String.format(message + "%d guesses", guessCount); return message; } }
User.java
/* Description: Exercise 05 (22W) Starter Code. * - No changes needed to this file for Exercise 05 (22W) */ import java.util.Scanner; /* * This class provides utility for getting user input from the console using * an instance of Scanner set to new Scanner(System.in) */ public class User { private Scanner keyboard = new Scanner(System.in); /* * Does not prompt the user, reads in an int value when there is one. If * the user does not provide input that can be converted into an int an * error message is printed requesting a correct input and a loop is used * to trap the user until they get this right. A future version of * the program might have a sanity check to break the loop after several * failed attempts and then shut down the program, rather than looping * (in theory) forever. */ public int inputInteger() { boolean isInputBad = true; boolean hasNextInt; int value = 0; while(isInputBad) { hasNextInt = keyboard.hasNextInt(); if(hasNextInt) { value = keyboard.nextInt(); isInputBad = false; // break out of loop } else { System.out.print("Invalid input. Enter an integer number: "); } keyboard.nextLine(); // clean up input stream } return value; } /* * This method prompts the user using the argument sent to message * and then calls to method inputInteger() to get the actual input value. */ public int inputInteger(String message) { // This method calls inputInteger(), so no changes are needed here. System.out.printf("%s", message); int value = inputInteger(); return value; } /* * Does not prompt the user, reads in a double value when there is one. If * the user does not provide input that can be converted into a double an * error message is printed requesting a correct input and a loop is used * to trap the user until they get this right. A future version of * the program might have a sanity check to break the loop after several * failed attempts and then shut down the program, rather than looping * (in theory) forever. */ public double inputDouble() { boolean isInputBad = true; boolean hasNextDouble; double value = 0.0; while(isInputBad) { hasNextDouble = keyboard.hasNextDouble(); if(hasNextDouble) { value = keyboard.nextDouble(); isInputBad = false; // break out of loop } else { System.out.print("Invalid input. Enter a number: "); } keyboard.nextLine(); // clean up input stream } return value; } /* * This method prompts the user using the argument sent to message * and then calls to method inputDouble() to get the actual input value. */ public double inputDouble(String message) { // This method calls inputDouble(), so no changes are needed here. System.out.printf("%s", message); double value = inputDouble(); return value; } /* * this method is used to get a line of text from the console, it reads * until it encounters a line terminator character. */ public String inputString() { String value = keyboard.nextLine(); return value; } /* * This method prompts the user using the argument sent to message, * then calls to method inputString() which reads in the actual text. */ public String inputString(String message) { System.out.printf("%s", message); String value = inputString(); return value; } }
Pseudocode
Class Exercise05 method main start declarations User user HighLowGame game String CONTINUE_GAME = "Y" String EXIT_GAME = "N" String shouldContinue = EXIT_GAME do if shouldContinue = CONTINUE_GAME then // case-insensitive comparison game.reset() endif game.run() shouldContinue = user.inputString("Would you like to play again? (Y/N)" while shouldContinue = CONTINUE_GAME // case-insensitive comparison out "Program by a learning Programmer" stop Class CheckGuessResult (all of it) public class CheckGuessResult declarations private String message private boolean isWin public CheckGuessResult() this("no game", false) return public CheckGuessResult(String message, boolean isWin) this.message = message this.isWin = isWin return public String getMessage() return message public void setMessage(String message) this.message = message return public boolean isWin() return isWin public void setWin(boolean isWin) this.isWin = isWin return public String toString() declarations String report report = "CheckGuessResult : message " + message + ", " report = report + "isWin " + isWin return report endClass Class HighLowGame (all of it) class HighLowGame private User user // class User is provided and complete private Random random // class Random is java.util.Random private num fuelAvailable //interger private num numberToGuess // integer private num MIN = 1 private num MAX = 10 public HighLowGame() reset() return public num getFuelAvailable() return fuelAvailable public void setFuelAvailable(num fuelAvailable) this.fuelAvailable = fuelAvailable return public num getNumberToGuess() return numberToGuess public void setnumberToGuess(num numberToGuess) this.numberToGuess = numberToGuess return public void reset() declaration num sum = 0 // radom.nextInt(MAX) returns a value from 0 to (MAX -1) // so add 1 to get a number between 1 to MAX inclusive numberToGuess = random.nextInt(MAX) + 1 for num value = MIN to MAX step 1 // MIN to MAX inclusive sum - sum + value endfor fuelAvailable = sum / 2 return public void run() declarations CheckGuessResult checkGuessResult = null String message boolean is Won = false num guessCount = 0 // int num userNumber // int output "Guess the number from " + MIN + " to " + MAX output "You have " + fuelAvailable + " guess-fuel remaining." while fuelAvailable > 0 AND isWon = false guessCount = guessCount + 1 userNumber = user.inputInteger("guess: ") // first loop control variable adjustment fuelAvailable = fuelAvailable - userNumber // let them have this last play, but prevent negative // fuel values in the output if fuelAvailable < 0 then fuelAvailable = 0 endif checkGuessResult = checkGuess(userNumber) // second loop control variable adjustment iWon = checkGuessResult.isWin() message = checkGuessResult.getMessage() output message // in game messaging endwhile message = reportGaemResult(isWon, guessCount) output message // after game report return private CheckGuessResult checkGuess(num guess) // int guess declarations boolean isWin = false String message if guess = numberToGuess then message = "you guessed the number" isWin = true else if guess < numberToGuess then message = "too low, " else message = "too high, " endif messag= message + fuelAvailable + " guess-fuel remaining" endif declarations CheckGuessResult checkGuessResult(message, isWin) return checkGuessResult private String reportGameResult(boolean is Won, num guessCount) declarations String message if isWon = true then message = "You win! It took you " eles message = "you did not win, you used " endif message = message + guessCount + "guesses." return message endClass
Auditing and Assurance services an integrated approach
ISBN: 978-0134065823
16th edition
Authors: Alvin A. Arens, Randal J. Elder, Mark S. Beasley, Chris E. Hogan