Question

Create a JAVA project named IA01 that does the following: Display a randomly-determined even number to...

Create a JAVA project named IA01 that does the following:

  • Display a randomly-determined even number to the user
    • The number should be between 2 and 10 inclusive
  • Prompt the user to enter the value of half that number
    • You can assume the user will enter valid numbers, not letters or gibberish
    • They only get one chance to answer per number
  • Tell the user if they are right or wrong
  • Ask the user if they want to try another even number
    • You can assume the user will type either "Y" or "N"
  • When the user answers Y, then repeat the steps with another even number
  • When the user answers N, then the game is over
  • When the game is over, display the following to the user:
    • How many answers were correct
    • How many answers were incorrect
    • The percentage of correct answers
      • Just use regular integer division
      • e.g., 2 out of 3 correct would result in 66%
    • The lowest even number that was chosen by the program
    • The highest even number that was chosen by the program

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

Given code and suggestions.

Build it up gradually,

    Phase 1 -- Figure out how to get an even number between 2 and 10
        1A) Print out a random number

        import java.util.Random;

        public class Main {
          public static void main(String[] args) {
            Random random = new Random();
            int randomInt = random.nextInt();
            System.out.println(randomInt);
          }
        }

        1B) Print out a random number between 0 and 4

        import java.util.Random;

        public class Main {
          public static void main(String[] args) {
            Random random = new Random();
            int randomInt = random.nextInt(5);
            System.out.println(randomInt);
          }
        }

        1C) Print out a random number between 1 and 5

        import java.util.Random;

        public class Main {
          public static void main(String[] args) {
            Random random = new Random();
            int randomInt = random.nextInt(5) + 1;
            System.out.println(randomInt);
          }
        }

        1D) Print out a random number between 2 and 10

        import java.util.Random;

        public class Main {
          public static void main(String[] args) {
            Random random = new Random();
            int randomInt = 2 * (random.nextInt(5) + 1);
            System.out.println(randomInt);
          }
        }

        1E) Comment the code

        import java.util.Random;

        public class Main {
          public static void main(String[] args) {

            Random random = new Random();
            
            /*
             * To get a random integer between 2 and 10, we get a random int
             * between 1 and 5 (inclusive), then double it.
             *
             * Since Random.nextInt(5) returns a random integer between 0 and 4
             * (inclusive), we add 1 to it. That gets us between 1 and 5.
             *
             * Doubling that result will give us the random integer between 2 and 10.
             */
            int randomInt = 2 * (random.nextInt(5) + 1);
            System.out.println(randomInt);
          }
        }

From this point on, I'll describe the phase, and show output, but not provide
any Java code...

    Phase 2 -- Keep printing a new random number until the user enters "N"

    10
    Another number? Y
    4
    Another number? Y
    6
    Another number? Y
    6
    Another number? N

    Game over

    Phase 3 -- Prompt the user for the answer, and show it to them
        You can assume the user types in positive integers
        In this phase, it doesn't matter if they're right or wrong

      What is half of 2? 123
      You answered: 123
      Another number? Y

      What is half of 2? 9
      You answered: 9
      Another number? Y

      What is half of 4? 5
      You answered: 5
      Another number? N

      Game over

    Phase 4 -- Tell the user if their answer is correct

    What is half of 2? 1
    You answered: 1
    Correct!
    Another number? Y

    What is half of 6? 4
    You answered: 4
    Wrong!
    Another number? N

    Game over

    Phase 5 -- Tell the user how many numbers they answered

    What is half of 8? 2
    You answered: 2
    Wrong!
    Another number? Y

    What is half of 4? 2
    You answered: 2
    Correct!
    Another number? Y

    What is half of 4? 1
    You answered: 1
    Wrong!
    Another number? N

    Game over
    You answered 3 questions

    Phase 6 -- Tell the user how many were right

    What is half of 6? 3
    You answered: 3
    Correct!
    Another number? Y

    What is half of 2? 1
    You answered: 1
    Correct!
    Another number? Y

    What is half of 4? 3
    You answered: 3
    Wrong!
    Another number? N

    Game over
    You answered 3 questions
    2 were right

    Phase 7 -- Tell the user how many were wrong

    What is half of 10? 5
    You answered: 5
    Correct!
    Another number? Y

    What is half of 8? 3
    You answered: 3
    Wrong!
    Another number? Y

    What is half of 8? 4
    You answered: 4
    Correct!
    Another number? Y

    What is half of 10? 5
    You answered: 5
    Correct!
    Another number? N

    Game over
    You answered 4 questions
    3 were right
    1 were wrong

    Phase 8 -- Tell the user the percentage correct

    What is half of 8? 4
    You answered: 4
    Correct!
    Another number? Y

    What is half of 6? 3
    You answered: 3
    Correct!
    Another number? Y

    What is half of 8? 1
    You answered: 1
    Wrong!
    Another number? N

    Game over
    You answered 3 questions
    2 were right
    1 were wrong
    You got 66% right

    Phase 9 -- Tell the user the highest random number they were shown

    What is half of 8? 4
    You answered: 4
    Correct!
    Another number? Y

    What is half of 6? 3
    You answered: 3
    Correct!
    Another number? Y

    What is half of 8? 1
    You answered: 1
    Wrong!
    Another number? N

    Game over
    You answered 3 questions
    2 were right
    1 were wrong
    You got 66% right
    The highest random number you were given: 8

    Phase 10 -- Tell the user the lowest random number they were shown

    What is half of 8? 4
    You answered: 4
    Correct!
    Another number? Y

    What is half of 6? 3
    You answered: 3
    Correct!
    Another number? Y

    What is half of 8? 1
    You answered: 1
    Wrong!
    Another number? N

    Game over
    You answered 3 questions
    2 were right
    1 were wrong
    You got 66% right
    The highest random number you were given: 8
    The lowest random number you were given: 6

Homework Answers

Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Main.java

import java.util.Random;

import java.util.Scanner;

public class Main {

      public static void main(String[] args) {

            // random number generator

            Random random = new Random();

            // scanner to read inputs

            Scanner scanner = new Scanner(System.in);

            // loop controller variable

            String choice = "Y";

            // initializing other required variables, initializing highestRandom to

            // 0, so that any number we generate (between 2-10) will be greater than

            // this number, similarly, setting lowestRandom to 11, because any

            // number we generate will be smaller than 11

            int num, right = 0, wrong = 0, highestRandom = 0, lowestRandom = 11;

            do {

                  // generating a random even number between 2 and 10

                  int randomInt = 2 * (random.nextInt(5) + 1);

                  // displaying number, asking user to enter half

                  System.out.print("What is half of " + randomInt + "? ");

                  // reading number

                  num = scanner.nextInt();

                  // displaying entered value

                  System.out.println("You answered: " + num);

                  // checking if answer user entered is correct

                  if (num == (randomInt / 2)) {

                        System.out.println("Correct!");

                        // updating right answers count

                        right++;

                  } else {

                        System.out.println("Wrong!");

                        // updating wrong answers count

                        wrong++;

                  }

                  // if this generated number is larger than current highestRandom,

                  // updating highestRandom

                  if (randomInt > highestRandom) {

                        highestRandom = randomInt;

                  }

                  // if this generated number is smaller than current lowestRandom,

                  // updating lowestRandom

                  if (randomInt < lowestRandom) {

                        lowestRandom = randomInt;

                  }

                  // asking user to prompt if he wants to generate another number

                  System.out.print("Another number? ");

                  choice = scanner.next();

            } while (choice.equalsIgnoreCase("Y"));

            // printing game over message upon loop exit

            System.out.println("Game over");

            // finding percentage as double. this is important to cast as double, or

            // else, the percentage will be assigned 0 if we perform integer

            // division

            double percentage = ((double) right / (right + wrong)) * 100;

            //now casting to integer to get rid of fractional part

            int percentageInteger = (int) percentage;

            //displaying stats

            System.out.println("You answered " + (right + wrong) + " questions");

            System.out.println(right + " were right");

            System.out.println(wrong + " were wrong");

            System.out.println("You got " + percentageInteger + "% right");

            System.out.println("The highest random number you were given: "

                        + highestRandom);

            System.out.println("The lowest random number you were given: "

                        + lowestRandom);

      }

}

/*OUTPUT*/

What is half of 4? 2

You answered: 2

Correct!

Another number? Y

What is half of 2? 1

You answered: 1

Correct!

Another number? y

What is half of 6? 2

You answered: 2

Wrong!

Another number? y

What is half of 6? 3

You answered: 3

Correct!

Another number? n

Game over

You answered 4 questions

3 were right

1 were wrong

You got 75% right

The highest random number you were given: 6

The lowest random number you were given: 2

Know the answer?
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
Similar Questions
Goal: Write a simple number guessing game in java. The game picks a number between bounds...
Goal: Write a simple number guessing game in java. The game picks a number between bounds input by the user, then user has to guess the number. The game will tell you if your guess is too high or too low. When you guess it, the game will tell you how many guesses it took Run multiple games and print statistics (# games, # guesses, avg) when all done. Sample Run: G U E S S I N G G...
In Java please 10.9 Lab 6 In BlueJ, create a project called Lab6 Create a class...
In Java please 10.9 Lab 6 In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as you go Copy the code for LineSegment given below into the class. Take a few minutes to understand what the class does. Besides the mutators and accessors, it has a method called print, that prints the end points of the line segment in the form: (x, y) to (x, y) You will have to write two methods in...
."Ask the user to input a number.  You must use an input dialog box for this input....
."Ask the user to input a number.  You must use an input dialog box for this input. Be sure to convert the String from the dialog box into an integer (int). The program needs to keep track of the smallest number the user entered as well as the largest number entered. Use a Confirm dialog box to ask the user if they want to enter another number. If yes, repeat the process. If no, output the smallest and largest number that...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
JAVA Write a number guessing game that will generate a random number between 1and 20 and...
JAVA Write a number guessing game that will generate a random number between 1and 20 and ask the user to guess that number. The application should start by telling the user what the app does. You should then create the random number and prompt the user to guess it. You need to limit the number of times that the user can guess to 10 times. If the user guesses the number, display some message that tell them that they did...
This is the code I have written for my Java homework assignment but I can't seem...
This is the code I have written for my Java homework assignment but I can't seem to get it to run. Any help would be appreciated! import javax.swing.JOptionPane; import java.io.*; import java.util.Scanner; public class javaGamev5 { public static void main(String[] args) throws IOException { String question = null, answerA = null, answerB = null, answerC = null ; int menuChoice = 0, correctAnswer = 0, points = 0, score = 0, highscore = 0; displayIntro(); do { menuChoice = displayMainMenu();...
##4. What will the following program display? ##def main(): ## x = 1 ## y =...
##4. What will the following program display? ##def main(): ## x = 1 ## y = 3.4 ## print(x, y) ## first printing ## change_us(x, y) ## print(x, y) ##second printing ## ##def change_us(a, b): ## a = 0 ## b = 0 ## print(a, b) ## ##main() ## ##Yes, yes, main() displays ##1 3.4 ##0 0 ##1 3.4 ## The question is: why x and y are still the same while the second printing of (x,y)? ## It seems...
Using your favorite IDE (e.g., Eclipse or NetBeans), create a new Java application (project) named CPS151_Lab3,...
Using your favorite IDE (e.g., Eclipse or NetBeans), create a new Java application (project) named CPS151_Lab3, or, if you are using DrJava, create a folder named CPS151_Lab3. Then, to either your project or folder, add: a main class (if one is not automatically created), a VotingMachine class that models a voting machine. Make sure your new class is in the same package as the main class. Add to the VotingMachine class: private integer data fields for the number of votes...
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1....
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January - December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs to adjust the...
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1....
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs to adjust the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT