Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java: Please help with my code I am trying to get the following output and am not sure what I am doing wrong. Thanks. Code:

Java: Please help with my code I am trying to get the following output and am not sure what I am doing wrong. Thanks.

image text in transcribed

Code:

import java.util.Random;

public class GuessingGame {

/** * Seven instance variables; * answer-an integer, * generator a random Generator object * gameOver a Boolean, * differential an integer, * max maximum value of the number to guess, * maxGuessesAllowed the maximum number of guesses the user gets, * numGuessesTaken an integer that stores the number of guessed taken so far in any * game. */ private int answer;//represents random generator private Random generator; //random generator object private boolean gameOver; //Boolean, false if game in progress, true if game is over private int differential;//an integer represents difference b/w a guess and an answer private int max; /umber to guess would be between 0 and 100. private int maxGuessesAllowed; //max number of guesses the user gets private int numGuessesTaken; //stores the number of guessed taken so far in the game

/** * Default Constructor, initialize the object with max equal to zero */ public GuessingGame() { max = 0; //set max to zero generator = new Random(); //creates random generator object }

/** * Parameterized Constructor, parameter is integer max * Creates the random number generator object */ public GuessingGame(int max) { this.max = max; //integer parameter represents max value of number to guess generator = new Random(); //creates random generator object }

/** * newGame method; * 1. Takes in an integer as a parameter representing the maximum number of * guesses and sets maxGuessesAllowed . In other words, the parameter represents * how many guesses the user gets before the game is over. * 2. Generates the answer using the random number generator. (0 - max). * 3. Sets gameOver to false. * 4. Sets differential to the max value. * 5. Sets numGuessTaken to zero. */ public void newGame(int maxGuessesAllowed) //takes an integer as parameter, sets maxGuessesAllowed { this.maxGuessesAllowed = maxGuessesAllowed; //take integer as a parameter rep max number of guesses allowed answer = generator.nextInt(max); //generates the answer using the random number generator gameOver = false; //set gameOver to false differential = max; //set differential to the max value numGuessesTaken = 0; //set numGuessTaken to zero

}

/** * guess method * 1. Takes in an integer as a parameter representing a new guess. * 2. Compares the new guess with the answer and generates and returns a * String representing an appropriate response. * 3. The response is based on: * a. The relation of the guess and answer (too high, too low or correct). * b. The comparison of difference between the current guess and the answer * and the previous guess and the answer. (warmer, colder) * c. Guess out of range error, if the guess is not between 0 and the max * number (inclusive) (see sample run below) * d. User has taken too many guess because numGuessesTaken is greater than * maxGuessesAllowed. If this is the case set isGameOver to true. */ public String guess(int newGuess) //takes integer as parameter representing new newGuess { String response = ""; //String response returns String if(newGuess max)//compare newGuess with the answer is below zero and above max { return "Out of range error";//return out of range error } if(newGuess > answer)//if newGuess greater than answer { response = String.format("Too High! "); //response is too high numGuessesTaken++; //take numGuessesTaken and increment } if(newGuess differential)//absolute value of newGuess minus answer is greater than differential { response = response + String.format("Getting Colder!"); //response is response plus "Getting colder" } else { response = response + String.format("Getting Warmer!"); //response is response plus "Getting warmer" } differential = Math.abs(newGuess - answer); //differential is absolute value of newGuess minus answer return response; //return response }

/** * v. isGameOver method - returns the state of game. * 1. true if game is over * 2. false if still in progress. * @return */ public boolean isGameOver() { return gameOver; }

/** * Accessor and mutator methods for all instance fields except the Random * number generator. Use the Accessor or mutator methods within the other * methods of the class rather than directly accessing the instance fields. * For example, use mutator methods in the parameterized constructor to modify * instance variables. */ //Accessor for getAnswer public int getAnswer() { return answer; }

//Accessor for differential public double getDifferential() { return differential; }

//Accessor for max public double getMax() { return max; }

//Accessor for maxGuessesAllowed public int getMaxGuessesAllowed() { return maxGuessesAllowed; }

//Accessor for numGuessesTaken public int getNumGuessesTaken() { return numGuessesTaken; }

//mutator GameOver public void setGameOver(boolean gameOver) { this.gameOver = gameOver; }

//mutator for answer public void setAnswer(int answer) { this.answer = answer;

}

//mutator for differential public void setDifferential(int differential) { this.differential = differential; }

//mutator for max public void setMax(int max) { this.max = max;

}

//mutator for maxGuessesAllowed public void setMaxGuessesAllowed(int maxGuessesAllowed) { this.maxGuessesAllowed = maxGuessesAllowed; }

//mutator for numGuessesTaken public void setNumGuessesTaken(int numGuessesTaken) { this.numGuessesTaken = numGuessesTaken; } }

Tester:

import java.util.Scanner;

public class GuessingGameTester {

public static void main(String[] args) { System.out.println("Welcome to the Guessing Game");

Scanner keyboard = new Scanner(System.in); char guess = 'y'; while(guess != 'n' && guess != 'N')//while guess is not "no" { System.out.println("Enter maximum number: "); int max; max = keyboard.nextInt(); GuessingGame game = new GuessingGame(max); System.out.println("Enter number of guesses allowed: "); int allowedGuess = keyboard.nextInt(); game.newGame(allowedGuess);

while(game.isGameOver() == false) { System.out.println("Enter your guess, remember it must be between 0 and " + max); int guessNew = keyboard.nextInt(); System.out.println(game.guess(guessNew)); } System.out.println("Would you like to play again? Enter Y for yes, N for no."); guess = keyboard.next().charAt(0); guess = 'n'; keyboard.close(); } } }

Sample Output: Welcome to the Guessing Game Enter the maximum number 100 answer is: 8 (Included for testing only, should not be display in final program) Enter the number of guess allowed Enter your guess, remember it must be between 0 and 100 50 Too High Getting Warmer Enter your guess, remember it must be between 0 and 100 25 Too High Getting Warmer Enter your guess, remember it must be between 0 and 100 12 Too High Getting Warmer Enter your guess, remember it must be between 0 and 100 Too low Getting Warmer Enter your guess, remember it must be between 0 and 100 Congratulation Would you like to play again, enter Y for yes, N for no

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

3 > O Actual direct-labour hours Standard direct-labour hours...

Answered: 1 week ago