Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am attempting to complete the following assignment. I am taking the additional

ID: 3707289 • Letter: I

Question

I am attempting to complete the following 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

Thank you!

Explanation / Answer

NumberGame.java

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;
             System.out.println("ASNWER:"+answer);
             //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.");
   }

}

GameOptions.java

package test;

import java.util.Scanner;

public class GameOptions {

   int attempt=0;
   public int getChoice(Scanner input) {
       System.out.println("Pease neter number between 1 and 10:");
       String str=input.nextLine();
       //check whether given input is integer or not
       if(!str.matches("-?\d+")) {
           System.out.println("Error.Numerical Values Only");
           //if input is not integer then ask user for proper numeric input
           return getChoice(input);
       }else {
           int no=Integer.parseInt(str);
           //check whether given input is between 1 and 10
           if(no<1 || no>10) {
           //if input is not between 1 and 10, ask user to enter input again
               System.out.println("Please Enter No between 1 and 10");
               return getChoice(input);
           }else {
               return no;
           }
       }
   }

   public boolean checkAnswer(int userChoice, int answer, Scanner input) {
       if(userChoice<answer) { //if userinput is less than answer show appropriate messgge and ask user for input
           System.out.println("Too Low.Try Again");
           attempt=attempt+1;
           int no=getChoice(input);
           checkAnswer(no, answer, input);
       }else if(userChoice>answer) { //if input is greater than answer show appropriate messgge and ask user for input
           System.out.println("Too High.Try Again");
           attempt=attempt+1;
           int no=getChoice(input);
           checkAnswer(no, answer, input);
       }else if(userChoice==answer){ //if userinput is equal to answer make attempt variable to 0
           System.out.println("You Got it! Number Of Tries:"+attempt);
           attempt=0;
           return true;
       }
       return false;
   }

   //If userinput won the current game then it will ask user if he want play again
   public boolean continueGame(Scanner input) {
       System.out.println("Would you like to continues?(Y/N):");
       String contgame=input.nextLine();
       if(contgame.equalsIgnoreCase("y")) {
           return true;
       }else if(contgame.equalsIgnoreCase("n")) {
           return false;
       }else {
           System.out.println("Invalid Input.Try Again");
           continueGame(input);
       }
       return false;
   }

}

Explanation:

We can see here we have given a class NumberGame which takes input from user through getChoice(input) method and pass it to the checkAnswer(userchoice,answer,input) method. We keep getting input from user and keep checking until we get it right and once answer is right we ask user if he want to continue to play the game using continueGame(input) method. So when checkanswer return true then only we going to call continueGame method till that time we keep getting input from user and keep checking.

NumberGame class is entry point which calls method present in GameOptions class

Gameoptions have following three methods:

Getchoice(input): This method return int and passes it to checkanswer method, so this method should return valid integer value. To make sure it return valid integer we keep calling getchoice method recursively until we get the correct input from user i.e valid integer between 1 and 10

checkAnswer(userChoice,answer,input): This method purpose is to check whether input provided by user is high or low. This method keeps getting input from user until it provides correct guess. Once the guess is correct it return true otherwise it keeps calling checkanswer method.

continueGame(input): This method task is to ask whether user want to keep playing game or he want to discontinue.