Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp { public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc =

Project 7-3 Guessing Game

import java.util.Scanner;

public class GuessNumberApp { public static void main(String[] args) { displayWelcomeMessage();

// create the Scanner object Scanner sc = new Scanner(System.in);

String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage();

// continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber = getIntWithinRange(sc, "Enter number: ", 0, 101);

// display result of guess to user if (guessNumber == number) { displayCorrectGuessMessage(counter); } else { displayGuessAgainMessage(number, guessNumber); } counter++; }

// see if the user wants to continue choice = getChoiceString(sc, "Try again? (y/n): ", "y", "n"); System.out.println(); } System.out.println("Bye - Come back soon!"); System.out.println(); }

private static void displayWelcomeMessage() { System.out.println("Welcome to the Guess the Number Game"); System.out.println("++++++++++++++++++++++++++++++++++++"); System.out.println(); } private static int getRandomNumber() { return (int) (Math.random() * 100) + 1; } private static void displayPleaseGuessMessage() { System.out.println("I'm thinking of a number from 1 to 100."); System.out.println("Try to guess it."); System.out.println(); } private static void displayCorrectGuessMessage(int counter) { System.out.println("You got it in " + counter + " tries."); if (counter <= 3) { System.out.println("Great work! You are a mathematical wizard. "); } else if (counter > 3 && counter <= 7) { System.out.println("Not too bad! You've got some potential. "); } else { System.out.println("What took you so long? Maybe you should take some lessons. "); } } private static void displayGuessAgainMessage(int number, int guessNumber) { int difference = guessNumber - number; if (guessNumber > number) { if (difference > 10) { System.out.println("Way too high! Guess again. "); } else { System.out.println("Too high! Guess again. "); } } else { if (difference < -10) { System.out.println("Way to low! Guess again. "); } else { System.out.println("Too low! Guess again. "); } } }

private static int getInt(Scanner sc, String prompt) { int i = 0; boolean isValid = false; while (!isValid) { System.out.print(prompt); if (sc.hasNextInt()) { i = sc.nextInt(); isValid = true; } else { System.out.println("Error! Invalid integer value. Try again."); } sc.nextLine(); // discard any other data entered on the line } return i; }

private static int getIntWithinRange(Scanner sc, String prompt, int min, int max) { int i = 0; boolean isValid = false; while (!isValid) { i = getInt(sc, prompt); if (i <= min) { System.out.println("Error! Number must be greater than " + min); } else if (i >= max) { System.out.println("Error! Number must be less than " + max); } else { isValid = true; } } return i; }

private static String getRequiredString(Scanner sc, String prompt) { String s = ""; boolean isValid = false; while (!isValid) { System.out.print(prompt); s = sc.nextLine(); if (s.equals("")) { System.out.println("Error! This entry is required. Try again."); } else { isValid = true; } } return s; }

private static String getChoiceString(Scanner sc, String prompt, String s1, String s2) { String s = ""; boolean isValid = false; while (!isValid) { s = getRequiredString(sc, prompt); if (!s.equalsIgnoreCase(s1) && !s.equalsIgnoreCase(s2)) { System.out.println("Error! Entry must be '" + s1 + "' or '" + s2 + "'. Try again."); } else { isValid = true; } } return s; } }

Criteria:

Console class-Create a class named Console
Move retrieve methods to Console- Move all the methods that retrieve user input to Console
Move validate methods to Console- Move all the methods that validate user input to Console
This criterion is linked to a Learning OutcomeConsole static methods-Console class methods remain static
Game class- Create a class named Game
This criterion is linked to a Learning OutcomeMove display methods to Game-Move all the methods that display messages to the Game class
Move guess methods to Game- Move all the methods that handle user guesses to the Game class
Game instance methods- Adjust Game methods so they aren't static
Game instance variables-Use instance variables of the Game class to keep track of numbers, guesses, and so on

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions

Question

What were the issues and solutions proposed by each team?

Answered: 1 week ago

Question

3. Who would the members be?

Answered: 1 week ago