Question

JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with...

JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with individual digits from 0 to 9 inclusive. (i.e. 12345, 09382, 33044, etc.) The player then tries to guess the number. With each guess the program displays two numbers, the first is the number of correct digits that are in the proper position and the second number is the sum of the correct digits. When the user enters the correct five-digit number the program returns a statement that tells them that the user found the correct number, and how many tries it took the user to find the number.
The game was originally written as a standalone program.
• Alter the class to allow it to be instantiated as a GuessFive class object.
• Create a new Score class that has instance variables for a String name and an int score.
• Alter the class to include a best-score list, as an array of Score objects,(Score[]) that holds the top five scores in ascending order. (low score is the best score) This list must be available to all instances of the class. Display the best-score list, name and score, at the end of each successful game along with the user’s score. If there is space on the scoreboard, new scores are added to the scoreboard. If the scoreboard is full a new score is added to the score board if it beats one of the scores on the scoreboard, and the worst score is pushed off the scoreboard.
• Create a driver program to test your conversion, that instantiates and runs at least 6 instances of the GuessFive class.
Note: Play the game a couple of times to get a feel for how it works before you try to modify it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.*;
class Score{
    public String Name;
    public int Score;
}
class GuessFive{
    public int random(){
    Random rand = new Random(); 
    int i=0;
    int randNum=0;
    while(i==0){
    randNum = rand.nextInt(100000); 
    if(randNum>9999){
        break;
    }
    }
    return randNum;
    }
public static void main(String args[]) {
    Scanner sc=new Scanner(System.in);
    GuessFive obj=new GuessFive();
     int ran=obj.random();
     System.out.println(ran);
     System.out.println("Welcome to Guess Five");
     int i=0;
     System.out.print("Enter your name:");
     String s=sc.nextLine();
     int num=0;
     int tries=0;
     while(i==0){
     System.out.println("Guess a five digit number");
     num=sc.nextInt();
     if(num>9999&&num<100000){
         break;
     }
     else{
          System.out.println("Invalid input");
        
     }
     }
     int temp1=ran,temp2=num;
     int arr1[]=new int[5];
     int k=4;;
     while(ran>0){
         int temp=ran%10;
         arr1[k]=temp;
         k--;
         ran/=10;
     }
     int r=0;
     while(r==0){
     int count=0;
     int arr2[]=new int[5];
     k=4;
     while(num>0){
         int temp=num%10;
         arr2[k]=temp;
         k--;
         num/=10;
     }
     List<Integer> l=new ArrayList<Integer>();
     if(temp1==temp2){
         System.out.println(s+"You have found the correct number");
         break;
     }
     else{
     for(int x=0;x<5;x++){
         for(int y=0;y<5;y++){
             if(arr1[x]==arr2[y]&&x==y){
                 l.add(arr1[x]);
                 count++;
             }
         }
     }
     if(count==5){
          System.out.println(s+" You have found the correct number");
          System.out.println("Number of tries it took to find the correct number is:"+(tries+1));
          break;
     }
     else{
         tries++;
         System.out.println("The number of correct digits that are in the proper position: "+count);
         int sum=0;
          for(int n=0;n<l.size();n++){
              sum=sum+l.get(n);
          }
          System.out.println("\nSum of the correct digits that are in the proper position is: "+sum);
           System.out.print("Try again!\nGuess a 5 digit number:");
           num=sc.nextInt();
     }
     }
    }
    }
}

Thank you! if you have any queries post it below in the comment section I will try my best to resolve your queries and I will add it to my answer if required. Please give upvote if you like it.

Add a comment
Know the answer?
Add Answer to:
JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this activity you will create a game call Guess That Number! The program has an...

    In this activity you will create a game call Guess That Number! The program has an unlimited number of rounds. In each round the program selects a random number that the user has to guess. In Round 1 the user has to guess a number less than 5 within 5 tries. If the correct number is not guessed in 5 tries than the game is over. If the correct number is guessed than the user moves onto the next round...

  • Code in JAVA.................... Guess the Number In this activity, you will write a REST server to...

    Code in JAVA.................... Guess the Number In this activity, you will write a REST server to facilitate playing a number guessing game known as "Bulls and Cows". In each game, a 4-digit number is generated where every digit is different. For each round, the user guesses a number and is told the exact and partial digit matches. An exact match occurs when the user guesses the correct digit in the correct position. A partial match occurs when the user guesses...

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • I need help with this assignment. Please include comments throughout the program. Thanks Develop an algorithm...

    I need help with this assignment. Please include comments throughout the program. Thanks Develop an algorithm and write the program in java for a simple game of guessing at a secret five-digit code. When the user enters a guess at the code, the program returns two values: the number of digits in the guess that are in the correct position and the sum of those digits. For example, if the secret code is 13720, and the user guesses 83521, the...

  • Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts...

    Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts the user for a guess. Use the pseudorandom number generator, the Random class. When the user guesses the correct number, say so, and display how many guesses the user required to guess correctly. Allow the user to play multiple times by asking the user if the user wants to continue playing.      Guess my number between 1 and 1000 => 500      Too high...

  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it)...

    Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it) Program should be able to guess correctly in 7 tries or lower. Initialize a variable that represents the lowest possible number to 0 (what type should this be?) Initialize a variable that represent the highest possible number to 100 (what type should this be?) Initialize a Boolean variable that represents if we’ve achieved the correct guess to false Initialize a variable in which to...

  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. The phrase is hidden-- all letters are replaced with asterisks. Spaces and punctuation are left unhidden. So if the phrase is "Joe Programmer", the initial partially hidden phrase is: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT