Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this Java project. The code must be in Java thanks. Create a class named Console, and move all the methods that

I need help with this Java project. The code must be in Java thanks.

Create a class named Console, and move all the methods that retrieve and validate user input to that class. These methods can remain static.

Create a class named Game, and move all the methods that display messages and handle user guesses to that class. Adjust these methods so they aren't static, and use instance variables of the Game class to keep track of numbers, guesses, and so on.

Update the application to use these classes and their methods. Make sure the application functions the same as it did before.

Here are the criteria that needs to be done.

This criterion is linked to a Learning OutcomeConsole class

Create a class named Console

----------------------------------------------------------------------------------

Move retrieve methods to Console

Move all the methods that retrieve user input to Console

----------------------------------------------------------------------------------

This criterion is linked to a Learning OutcomeMove 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

----------------------------------------------------------------------------------

Move display methods to Game

Move all the methods that display messages to the Game class

----------------------------------------------------------------------------------

This criterion is linked to a Learning OutcomeMove 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

----------------------------------------------------------------------------------

This criterion is linked to a Learning OutcomeGame instance variables

Use instance variables of the Game class to keep track of numbers, guesses, and so o

Starting code

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; } }

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

T Sql Window Functions For Data Analysis And Beyond

Authors: Itzik Ben Gan

2nd Edition

0135861446, 978-0135861448

More Books

Students also viewed these Databases questions