Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Screenshots of the code with an explanation:
- https://media.cheggcdn.com/media/139/139f7b3d-05bb-487c-a40f-5247a50bcdd3/phpZdy2mZ.png
- https://media.cheggcdn.com/media/dfe/dfeaec47-0b36-4831-b89f-affb39b81a52/phpEcp1UE.png
- https://media.cheggcdn.com/media/864/864eb7d9-7a78-4ccc-9d59-7d8b6b028e58/phpEuHqlI.png
- https://media.cheggcdn.com/media/6d6/6d60ed69-de94-4d70-ad1b-b5dd741e6abf/phpsxJ84e.png
- https://media.cheggcdn.com/media/69e/69ea753c-2353-4fe3-be6b-2551697667b1/phpijylbP.png
- I have Created an extra class to Test our code it's called RamdomGuessGameTest.java it's screenshot:
- https://media.cheggcdn.com/media/353/353f145b-531d-4d67-9b7d-6ff7571cbf9c/phptEjUtA.png
- The Output Obtained by using the RamdomGuessGameTest.java :
- https://media.cheggcdn.com/media/866/866ac81c-ccad-4957-9f82-adb464dcc507/php6x0sPA.png
- The Code Used in RandGuessGame.java:
- /**
- * 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
- {
- //Declare data members
- public static final int ARR_SIZE = 5; //Initializing the ARR_SIZE to 5.
- private int arraySum;
- private char guess;
- private int guessTarget;
- private boolean hideMiddleVals;
- private static final int MAX_VALUE = 100; //Initializing the MAX_VALUE to 100.
- private int[] numbers;
- private java.util.Random rand;
- //Create Constructor
- /**
- * 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".
- * The passed Random object is assigned to the class' Random member. hideMiddleVals is defaulted to "true".
- *
- * @param randIn - The random number generator to be used for this instance of the game.
- */
- public RandGuessGame(java.util.Random randIn) //Initializing the member variable to default values as directed.
- {
- this.numbers = new int[ARR_SIZE]; //creating an array of integer of ARR_SIZE
- this.arraySum = 0;
- this.guessTarget = ARR_SIZE*(MAX_VALUE/2);
- this.rand = randIn;
- this.hideMiddleVals = true;
- }
- //Write member methods
- /**
- * Populates the "numbers" array with random numbers between 1 and "MAX_VALUE".
- */
- public void populateArray()
- {
- for(int i=0;i<ARR_SIZE;i++) //Populating the array of integer with random values.
- {
- this.numbers[i] = rand.nextInt(MAX_VALUE+1); //we have done MAX_VALUE+1 cause we have to include MAX_VALUE also.
- }
- }
- /**
- * Toggles the value of hideMiddleVals. If it is currently true, sets it to false, and vice-versa.
- */
- public void toggleHidden() //We are changing hideMiddlVals to true if it's false and to false if it's true.
- {
- if(this.hideMiddleVals){
- this.hideMiddleVals = false;
- }else{
- this.hideMiddleVals = true;
- }
- }
- /**
- * 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
- * @return A String with the values of the numbers array. Middle values are hidden if hideMiddleValue is true.
- */
- public java.lang.String toString() //converting the array to string from.
- {
- String output = ""+this.numbers[0]+" "; //first element.
- if(this.hideMiddleVals) //if hideMiddleVals is true we will make all the elements accept the first and last to X.
- {
- for(int i=1;i<ARR_SIZE-1;i++)
- {
- output+=("X ");
- }
- output+=(this.numbers[ARR_SIZE-1]+" "); //Last element.
- }else{
- for(int i=1;i<ARR_SIZE;i++) //if the hideMiddleVasls is false.
- {
- output+=(this.numbers[i]+" ");
- }
- }
- return output;
- }
- /**
- * Accepts a user's guess for the game. Validates that it is either the character 'Y' or 'N'. If it is a valid guess,
- * sets the guess data member to the passed value and returns "true". If it is not valid it does not change
- * the value of guess and returns false.
- *
- * @param guessIn - The player's guess.
- * @return True if the passed guess is valid, false if it is not.
- */
- public boolean validatePlayerGuess(char guessIn)
- {
- if(guessIn=='Y' || guessIn=='N') { //checking if guess by the user is 'Y' or 'N'.
- this.guess = guessIn;
- return true;
- }else {
- return false;
- }
- }
- /**
- * Checks to see if player's guess was correct, and constructs and returns a String that reports if they are correct or
- * incorrect, and appends the correct sum of the array.
- * @return A String reporting the results of the game.
- */
- public java.lang.String getResult()
- {
- this.getArraySum();
- if(this.arraySum>this.guessTarget) //checking if the sum of array is greater than the guessTarget.
- {
- if(this.guess=='Y') //if user enters 'Y' it's correct and we return the following string.
- {
- return "You guessed correctly! The sum was "+this.arraySum+"!";
- }else{
- return "You guessed wrong! The sum was "+this.arraySum+"!";
- }
- }else{ //Complete opposite condition.
- if(this.guess=='Y')
- {
- return "You guessed wrong! The sum was "+this.arraySum+"!";
- }else{
- return "You guessed correctly! The sum was "+this.arraySum+"!";
- }
- }
- }
- /**
- * Retrieves the numbers array. Used for testing, do not change.
- * @return The numbers array.
- */
- public int[] getNumbers()
- {
- return numbers;
- }
- /**
- * Retrieves the sum of the numbers in the array. Used for testing, do not change.
- * @return The value of arraySum.
- */
- public int getArraySum() //returning the sum of the elements of the array.
- {
- for(int i=0;i<ARR_SIZE;i++)
- {
- this.arraySum+=this.numbers[i];
- }
- return arraySum;
- }
- }
- The Code Used for RamdomGuessGameTest.java:
- import java.util.Scanner;
- public class RandomGuessGameTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- RandGuessGame game = new RandGuessGame(new java.util.Random());
- game.populateArray();
- System.out.println("Player Guess Corretly");
- System.out.println(game.toString());
- while(true)
- {
- System.out.println("Is the number greater than "+250+"?");
- System.out.print("(Y or N): ");
- char guess = scanner.next().charAt(0);
- if(game.validatePlayerGuess(guess))
- {
- System.out.println(game.getResult());
- break;
- }
- }
- game.toggleHidden();
- System.out.println(game.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment