I am attempting to complete the following JAVA assignment. I am taking the addit
ID: 3707293 • Letter: I
Question
I am attempting to complete the following JAVA assignment.
I am taking the additional step of trying to get this working in one document before I use the assigned constructor method file (code follows assignment listed below).
I have also listed the code for the provided file after the listing for the assignment.
Problems I'm currently having with the code (trying to get this working in one file before I move on):
1.) When a correct guess is made the line "Please enter a number between 1 and 10: " is repeated, and another input must be made (doesn't have to be the right guess again) before the program will continue.
2.) After an error is made and a negative number is entered, it doesn't register as an unacceptable value.
JGrasp example output follows:
JGrasp output:
----jGRASP exec: java GameOptions
Please enter a number between 1 and 10: 5
Your guess is too low
Please enter a number between 1 and 10: h
Error - Please enter a numerical value: -78
Your guess is too low
Please enter a number between 1 and 10: -78
Error. Please enter a number between 1 and 10
Please enter a number between 1 and 10: 4
Your guess is too low
Please enter a number between 1 and 10: 9
Your guess is too high
Please enter a number between 1 and 10: 8
Your guess is too high
Please enter a number between 1 and 10: 7
Your guess is too high
Please enter a number between 1 and 10: 6
Please enter a number between 1 and 10: 6
You win!
The number was 6
It took you 7 tries
The user chose 6
Would you like to continue? (Y/N): n
----jGRASP: operation complete.
In summary, I need help to
A.) Complete a working version of my current program
B.) Complete the program as assigned
I really have very little grasp of OOP and how to send-reveive info between methods so any explanation of what's going on in "B" would be greatly appreciated as well. --Thanks in advance.
## ASSIGNMENT FOLLOWS ##
In this project you will create a Pick a Number game that prompts a user for a number between one and 10. The user should be notified if his or her choice is too high, too low, or exactly right. You will need to keep track of the number of guesses, and once the user gets the right answer, display how many guesses it took. Do not count invalid entries as guesses. Finally, make sure only valid input is accepted. Any String input should be handled appropriately, and no values less than one or greater than 10 should be accepted.
Because professional programming environments often involve working in teams, being able to work on specific pieces or parts of a program is crucial. Object oriented programming makes this all possible. This week's material is focused on objects and classes, so you will be given half of the program that contains the main structure of the game. It will be up to you to build the second class and group of methods that the program is calling for.
The main driver class you are given below will contain the name of the second class you need to create as well as the names of the methods that should be a part of that class. You can assume that the driver class information is correct.
Attachment: NumberGame.java
Below you will find an example of what the output should look like:
Guess the Number Game
Please choose a number between 1 and 10: 10
Too high. Try again.
Please choose a number between 1 and 10: 5
Too low. Try again.
Please choose a number between 1 and 10: 8
You got it! Number of tries: 3
Would you like to play again? (Y/N): k
Invalid entry. Please enter Y or N: y
Please choose a number between 1 and 10: 11
Invalid value. Please enter a number between 1 and 10: 10
Too high. Try again.
Please choose a number between 1 and 10: -1
Invalid value. Please enter a number between 1 and 10: 6
Too high. Try again.
Please choose a number between 1 and 10: 3
Too high. Try again.
Please choose a number between 1 and 10: 1
You got it! Number of tries: 4
Would you like to play again? (Y/N): y
Please choose a number between 1 and 10: fish
Error - Numerical values only.
Please choose a number between 1 and 10: 8
Too high. Try again.
Please choose a number between 1 and 10: 3
Too low. Try again.
Please choose a number between 1 and 10: 5
Too low. Try again.
Please choose a number between 1 and 10: 6
You got it! Number of tries: 4
Would you like to play again? (Y/N): n
Thanks for playing.
When you have verified proper functionality of your program, complete three test cases that demonstrate your program's ability to handle both correct and invalid data. You should test for exceptions and invalid data in your numerical choices as well as your statement asking the user if he or she would like to play again.
Take a screenshot of each test case to submit with this assignment. Then attach your GameOptions.java file, pseudocode document, and screenshots to this assignment and submit it for grading.
START -- NumberGame.java
//Driver class for Programming Project 11
import java.util.Scanner;
public class NumberGame
{
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
GameOptions opt = new GameOptions(); // Your created class
int userChoice = -1234;
int answer = -1234;
boolean keepPlaying = true;
System.out.println("Guess the Number Game ");
while (keepPlaying == true) {
answer = (int) (Math.random() * 10)+1;
//Create a getChoice method in your class and make sure it accepts a Scanner argument
userChoice = opt.getChoice(input);
//Create a checkAnswer method in your class. Make sure it accepts two integer arguments and a Scanner argument
opt.checkAnswer(userChoice, answer, input);
// Create a continueGame method in your class and make sure it accepts a Scanner argument
keepPlaying = opt.continueGame(input);
}
System.out.println("Thanks for playing.");
}
}
END -- NumberGame.java
######################
My code:
import java.util.*;
import java.util.Scanner;
import java.util.InputMismatchException;
public class GameOptions{
public static void main (String []args){
Scanner input = new Scanner(System.in);
boolean keepPlaying = true;
int someNumber = 9786;
Random rand = new Random();
int numberOfTries = 0;
boolean win = false;
while (keepPlaying == true){
someNumber = getChoice(input);
int numberToGuess = (rand.nextInt(10)+1);
while (win == false){
if (someNumber == numberToGuess){
win = true;
}
else if (someNumber < numberToGuess){
System.out.println("Your guess is too low");
}
else if (someNumber > numberToGuess){
System.out.println("Your guess is too high");
}
someNumber = getChoice(input);
numberOfTries++;
}//END while - WIN
System.out.println("You win!");
System.out.println("The number was " + numberToGuess);
System.out.println("It took you " + numberOfTries + " tries");
System.out.println("The user chose " + someNumber);
win = false;//added
someNumber = 0; //added
numberOfTries = 0; //added
keepPlaying = contGame(input);
//keepPlaying = opt.contGame(input);//we'll be calling this from opt later
}//END WHILE - KEEP PLAYING
}
//userChoice take scanner and return int userChoice
public static int getChoice(Scanner in){//doesn't need to be static, but here will give an error if not
Scanner input = new Scanner (System.in);
boolean runWhile = true;
boolean thereIsAProblem = true;
boolean thereIsAnotherProblem = true;
String keepGoing = "Y";
int userInput = -999;
// GET INPUT AND ERROR CHECK HERE
//================================================
//userInput = 0;
while (thereIsAProblem) {
try{
thereIsAProblem = false;//moved here from above
System.out.print("Please enter a number between 1 and 10: ");
userInput = input.nextInt();
if ((userInput < 1)||(userInput > 10)) {
System.out.println("Error. Please enter a number between 1 and 10");
thereIsAProblem = true;
} //end if loop
else if ((userInput > 0)&&(userInput < 11)){
thereIsAProblem = false;
} //end else if
//If we make it here, we have good data.
} // end try
catch(InputMismatchException e){
System.out.print("Error - Please enter a numerical value: ");
input.nextLine();
} // end catch
} // end while - THERE IS A PROBLEM
input.nextLine();
return userInput;
}
//================================================
public static boolean contGame(Scanner in){
String userResponse = "";
System.out.print("Would you like to continue? (Y/N): ");
userResponse = in.nextLine();
while (!userResponse.equalsIgnoreCase("Y") && !userResponse.equalsIgnoreCase("N")){
System.out.print("Invalid Entry (Y or N only): ");
userResponse = in.nextLine();
}
//once out of the while it means we have valid input
if (userResponse.equalsIgnoreCase("y")){
return true;//the value we're returning
}
else {
return false;
}
}
}
END -- My code
Explanation / Answer
Given below is the modified code as per the requirement.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
import java.util.Scanner;
import java.util.InputMismatchException;
public class GameOptions{
public void checkAnswer(int userChoice, int answer, Scanner input) {
int numberOfTries = 1;
while(true)
{
if(userChoice < answer)
System.out.println("Your guess is too low");
else if (userChoice > answer){
System.out.println("Your guess is too high");
}
else
break;
userChoice = getChoice(input);
numberOfTries++;
}//END while
System.out.println("You win!");
System.out.println("The number was " + answer);
System.out.println("It took you " + numberOfTries + " tries");
}
//userChoice take scanner and return int userChoice
public int getChoice(Scanner in){
int userInput = -999;
// GET INPUT AND ERROR CHECK HERE
//================================================
//userInput = 0;
while (true) {
try{
System.out.print("Please enter a number between 1 and 10: ");
userInput = in.nextInt();
if ((userInput < 1)||(userInput > 10)) {
System.out.println("Error. Please enter a number between 1 and 10");
} //end if loop
else if ((userInput > 0)&&(userInput < 11)){
return userInput;
} //end else if
//If we make it here, we have good data.
} // end try
catch(InputMismatchException e){
System.out.println("Error - Please enter a numerical value: ");
in.nextLine();
} // end catch
} // end while - THERE IS A PROBLEM
}
//================================================
public boolean continueGame(Scanner in){
String userResponse = "";
System.out.print("Would you like to continue? (Y/N): ");
userResponse = in.next();
while (!userResponse.equalsIgnoreCase("Y") && !userResponse.equalsIgnoreCase("N")){
System.out.print("Invalid Entry (Y or N only): ");
userResponse = in.next();
}
//once out of the while it means we have valid input
if (userResponse.equalsIgnoreCase("y")){
return true;//the value we're returning
}
else {
return false;
}
}
}
output
------
Guess the Number Game
Please enter a number between 1 and 10: 5
Your guess is too low
Please enter a number between 1 and 10: 10
Your guess is too high
Please enter a number between 1 and 10: 7
Your guess is too high
Please enter a number between 1 and 10: 6
You win!
The number was 6
It took you 4 tries
Would you like to continue? (Y/N): y
Please enter a number between 1 and 10: the
Error - Please enter a numerical value:
Please enter a number between 1 and 10: -1
Error. Please enter a number between 1 and 10
Please enter a number between 1 and 10: 12
Error. Please enter a number between 1 and 10
Please enter a number between 1 and 10: 8
Your guess is too high
Please enter a number between 1 and 10: 4
Your guess is too low
Please enter a number between 1 and 10: 6
Your guess is too high
Please enter a number between 1 and 10: 5
You win!
The number was 5
It took you 4 tries
Would you like to continue? (Y/N): y
Please enter a number between 1 and 10: 5
Your guess is too low
Please enter a number between 1 and 10: 8
Your guess is too high
Please enter a number between 1 and 10: 7
Your guess is too high
Please enter a number between 1 and 10: 9
Your guess is too high
Please enter a number between 1 and 10: 6
You win!
The number was 6
It took you 5 tries
Would you like to continue? (Y/N): n
Thanks for playing.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.