How do I write the following methods for a RandGuessGAme class in java. A Class
ID: 3878244 • Letter: H
Question
How do I write the following methods for a RandGuessGAme class in java. A 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.
please write the following methods in java with the following Javadoc for this class. Thanks in advance
- toggleHidden public void toggleHidden) Toggles the value of hideMiddleVals. If it is currently true, sets it to false, and vice- versa toString public java.lang.String toString() Returns a String containing the values in the "numbers" array on a single line, separated by spaces with the middle values hidden or all visible based on value of "hideMiddleValue" data member. There is a trailing space on the end, so an example String returned may be: "5 X X X 67 ". NOTE: This does not output to System.out, it generates and returns a String Overrides: toString in class java.lang.object Returns: A String with the values of the numbers array. Middle values are hidden if hideMiddleValue is true.Explanation / Answer
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.stream.Collectors;
public class RandGuessGame {
private boolean hideMiddleValue = true;
private List<Integer> numbers = new LinkedList<>();
private char guess;
private static final int MAX_VALUE = 10;
// As you haven't mentioned what should be the possible average value, here i am assuming the values as the average of 1 and the MAX_VALUE
// ie, (n(n+1) / 2) / n = (n+1)/2
private static final int POS_AVG_VALUE = (MAX_VALUE + 1 / 2);
public RandGuessGame() {
// Initialize 5 ramdom values between 1 and max_value to numbers arraylist
Random r = new Random();
for (int i = 0; i < 5; i++) {
numbers.add(r.nextInt(MAX_VALUE - 1) + 1);
}
}
public void toogleHidden() {
hideMiddleValue = !hideMiddleValue;
}
@Override
public String toString() {
return hideMiddleValue ? numbers.parallelStream().map(number -> (numbers.indexOf(number) == 0 || numbers.indexOf(number) == 4) ? number+"" : "X").collect(Collectors.toList()).toString().replace("[", "").replace("]", "") : numbers.toString().replace("[", "").replace("]", "");
}
public boolean validatePlayerGuess(char guessIn) {
if (getResult().charAt(0) == guessIn) {
guess = guessIn;
return true;
}
return false;
}
public String getResult() {
int sum = numbers.stream().mapToInt(Integer::intValue).sum();
//if the check is true, it returns Y:sum else N:sum
return sum > POS_AVG_VALUE ? "Y:" + sum : "N:" + sum;
}
public static void main(String[] args) {
RandGuessGame guessGame = new RandGuessGame();
System.out.println(guessGame.toString());
System.out.println("Enter Your Guess: Y/N");
Scanner scanner = new Scanner(System.in);
System.out.println(guessGame.validatePlayerGuess(scanner.next().charAt(0)));
}
}
Sample Output:
2, X, X, X, 1
Enter Your Guess: Y/N
Y
true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.