The game will randomly generate five numbers between 1 and 100, show the player
ID: 2247788 • Letter: T
Question
The game will randomly generate five numbers between 1 and 100, show the player only the first and last, and they must guess if the sum of all numbers is greater than 250.
import java.util.Scanner;
import java.util.Random;
/**
* Class for a simple, randomized guessing game. Five integer values between 1 and MAX_VALUE (inclusive) will be
* generated. Only the first and last will be shown to the player. The player must then guess if the sum
* of all of the numbers is greater than the possible average or not.
*
*/
public class RandGuessGame
{
/**
* Maximum value of randomly generated values.
*/
private static final int MAX_VALUE = 100;
/**
* Number of randomly generated numbers.
*/
private static final int ARR_SIZE = 5;
/**
* Stores randomly generated numbers for game.
*/
private int[] numbers;
/**
* Number the player must guess against, calculated with MAX_VALUE and ARR_SIZE.
*/
private int guessTarget;
/**
* Stores player's guess.
*/
private char guess;
/**
* Stores sum of random values.
*/
private int arraySum;
/**
* Constructor for the RandGuessGame. Creates "numbers" array of size ARR_SIZE to store random values,
* sets arraySum to zero, and calculates value of "guessTarget" by multiplying the amount of numbers by
* half of "MAX_VALUE".
*/
//RandGuessGame Constructor goes here
/**
* Populates the "numbers" array with random numbers between 1 and "MAX_VALUE".
*/
//populateArray method goes here
/**
* Outputs the values in the "numbers" array on a single line, separated by spaces with values hidden
* or all visible based on value of "hidden" parameter.
* @param hidden If true passed will hide middle elements by outputting 'X' instead of the number,
* otherwise will output all elements as normal.
*/
//outputArray method goes here
/**
* Retrieves player's guess using a Scanner. Reads a character and stores into "guess" instance variable.
* Validates that only 'Y' or 'N' are accepted as input. Will continually prompt the user for a guess
* until a 'Y' or 'N' is given.
*/
//playerGuess method goes here
/**
* Checks to see if player's guess was correct and then reports if they are correct or incorrect, then outputs
* sum of array.
*/
//getResult method goes here
}
EXAMPLE OUTPUT
Run 1: Player guesses correctly
54 X X X 83
Is the sum of the numbers greater than 250?
(Y or N): Y
You guessed correctly! The sum was 314!
54 3 77 97 83
Run 2: Player guesses incorrectly
78 X X X 37
Is the sum of the numbers greater than 250?
(Y or N): N
You guessed wrong! The sum was 305!
78 100 49 41 37
Run 3: Player provides incorrect input multiple times
33 X X X 40
Is the sum of the numbers greater than 250?
(Y or N): y
(Y or N): n
(Y or N): P
(Y or N): z
(Y or N): N
You guessed correctly! The sum was 233!
33 67 78 15 40
Explanation / Answer
Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.
_____________
RandGuessGame.java
import java.util.Scanner;
import java.util.Random;
/**
* Class for a simple, randomized guessing game. Five integer values between 1
* and MAX_VALUE (inclusive) will be generated. Only the first and last will be
* shown to the player. The player must then guess if the sum of all of the
* numbers is greater than the possible average or not.
*
*/
public class RandGuessGame {
/**
* Maximum value of randomly generated values.
*/
private static final int MAX_VALUE = 100;
/**
* Number of randomly generated numbers.
*/
private static final int ARR_SIZE = 5;
/**
* Stores randomly generated numbers for game.
*/
private int[] numbers;
/**
* Number the player must guess against, calculated with MAX_VALUE and
* ARR_SIZE.
*/
private int guessTarget;
/**
* Stores player's guess.
*/
private char guess;
/**
* Stores sum of random values.
*/
private int arraySum;
/**
* Constructor for the RandGuessGame. Creates "numbers" array of size
* ARR_SIZE to store random values, sets arraySum to zero, and calculates
* value of "guessTarget" by multiplying the amount of numbers by half of
* "MAX_VALUE".
*/
public RandGuessGame() {
numbers = new int[ARR_SIZE];
arraySum = 0;
Random r = new Random();
/**
* Populates the "numbers" array with random numbers between 1 and
* "MAX_VALUE".
*/
for (int i = 0; i < ARR_SIZE; i++) {
numbers[i] = r.nextInt((100 - 1) + 1) + 1;
}
}
/**
* Outputs the values in the "numbers" array on a single line, separated by
* spaces with values hidden or all visible based on value of "hidden"
* parameter.
*
* @param hidden
* If true passed will hide middle elements by outputting 'X'
* instead of the number, otherwise will output all elements as
* normal.
*/
// outputArray method goes here
public void outputArray(boolean hidden) {
if (hidden == true) {
System.out.print(numbers[0] + " ");
for (int i = 1; i < ARR_SIZE - 1; i++) {
System.out.print(numbers[i] + " ");
}
System.out.print(numbers[ARR_SIZE - 1]);
} else {
System.out.print(numbers[0] + " ");
for (int i = 1; i < ARR_SIZE - 1; i++) {
System.out.print("X ");
}
System.out.print(numbers[ARR_SIZE - 1]);
}
}
/**
* Retrieves player's guess using a Scanner. Reads a character and stores
* into "guess" instance variable. Validates that only 'Y' or 'N' are
* accepted as input. Will continually prompt the user for a guess until a
* 'Y' or 'N' is given.
*/
// playerGuess method goes here
public void playerGuess() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("(Y or N) :");
guess = sc.next(".").charAt(0);
if (guess == 'Y' || guess == 'N')
break;
else
continue;
}
}
/**
* Checks to see if player's guess was correct and then reports if they are
* correct or incorrect, then outputs sum of array.
*/
// getResult method goes here
public void getResult() {
for (int i = 0; i < ARR_SIZE; i++) {
arraySum += numbers[i];
}
if (arraySum > 250 && guess == 'Y') {
System.out.println("You guessed correctly! The sum was " + arraySum + "!");
} else {
System.out.println("You guessed wrong! The sum was " + arraySum + "!");
}
outputArray(true);
}
}
______________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variable
char guess;
/* Creating an Scanner class object which is used
* to get the inputs entered by the user
*/
Scanner sc = new Scanner(System.in);
//Creating An RandomGuessGame class object
RandGuessGame rgg = new RandGuessGame();
//Calling the methods
rgg.outputArray(false);
System.out.println(" Is the sum of the numbers greater than 250? ");
rgg.playerGuess();
rgg.getResult();
}
}
____________________
Output:
Run:1
55 X X X 4
Is the sum of the numbers greater than 250?
(Y or N) :Y
You guessed wrong! The sum was 172!
55 86 8 19 4
_____________
Run:2
84 X X X 25
Is the sum of the numbers greater than 250?
(Y or N) :y
(Y or N) :n
(Y or N) :p
(Y or N) :z
(Y or N) :N
You guessed wrong! The sum was 321!
84 81 98 33 25
Run:3
66 X X X 45
Is the sum of the numbers greater than 250?
(Y or N) :Y
You guessed correctly! The sum was 257!
66 52 50 44 45
_________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.