Create a JAVA project named IA01 that does the following:
-----------------------------------------------------------------------------------------------------------------------
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
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
Get Answers For Free
Most questions answered within 1 hours.