jaredec18

Untitled

Sep 11th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. Screenshots of the code with an explanation:
  2. https://media.cheggcdn.com/media/139/139f7b3d-05bb-487c-a40f-5247a50bcdd3/phpZdy2mZ.png
  3.  
  4. https://media.cheggcdn.com/media/dfe/dfeaec47-0b36-4831-b89f-affb39b81a52/phpEcp1UE.png
  5.  
  6. https://media.cheggcdn.com/media/864/864eb7d9-7a78-4ccc-9d59-7d8b6b028e58/phpEuHqlI.png
  7.  
  8. https://media.cheggcdn.com/media/6d6/6d60ed69-de94-4d70-ad1b-b5dd741e6abf/phpsxJ84e.png
  9.  
  10. https://media.cheggcdn.com/media/69e/69ea753c-2353-4fe3-be6b-2551697667b1/phpijylbP.png
  11.  
  12. I have Created an extra class to Test our code it's called RamdomGuessGameTest.java it's screenshot:
  13.  
  14. https://media.cheggcdn.com/media/353/353f145b-531d-4d67-9b7d-6ff7571cbf9c/phptEjUtA.png
  15.  
  16. The Output Obtained by using the RamdomGuessGameTest.java :
  17.  
  18. https://media.cheggcdn.com/media/866/866ac81c-ccad-4957-9f82-adb464dcc507/php6x0sPA.png
  19.  
  20. The Code Used in RandGuessGame.java:
  21.  
  22.  
  23. /**
  24. * Class for a simple, randomized guessing game. Five integer values between 1 and MAX_VALUE (inclusive) will be
  25. * generated. Only the first and last will be shown to the player. The player must then guess if the sum
  26. * of all of the numbers is greater than the possible average or not.
  27. *
  28. */
  29. public class RandGuessGame
  30. {
  31.  
  32. //Declare data members
  33. public static final int ARR_SIZE = 5; //Initializing the ARR_SIZE to 5.
  34. private int arraySum;
  35. private char guess;
  36. private int guessTarget;
  37. private boolean hideMiddleVals;
  38. private static final int MAX_VALUE = 100; //Initializing the MAX_VALUE to 100.
  39. private int[] numbers;
  40. private java.util.Random rand;
  41.  
  42. //Create Constructor
  43. /**
  44. * Constructor for the RandGuessGame. Creates "numbers" array of size ARR_SIZE to store random values, sets
  45. * arraySum to zero, and calculates value of "guessTarget" by multiplying the amount of numbers by half of "MAX_VALUE".
  46. * The passed Random object is assigned to the class' Random member. hideMiddleVals is defaulted to "true".
  47. *
  48. * @param randIn - The random number generator to be used for this instance of the game.
  49. */
  50. public RandGuessGame(java.util.Random randIn) //Initializing the member variable to default values as directed.
  51. {
  52. this.numbers = new int[ARR_SIZE]; //creating an array of integer of ARR_SIZE
  53. this.arraySum = 0;
  54. this.guessTarget = ARR_SIZE*(MAX_VALUE/2);
  55. this.rand = randIn;
  56. this.hideMiddleVals = true;
  57. }
  58.  
  59. //Write member methods
  60.  
  61. /**
  62. * Populates the "numbers" array with random numbers between 1 and "MAX_VALUE".
  63. */
  64. public void populateArray()
  65. {
  66. for(int i=0;i<ARR_SIZE;i++) //Populating the array of integer with random values.
  67. {
  68. this.numbers[i] = rand.nextInt(MAX_VALUE+1); //we have done MAX_VALUE+1 cause we have to include MAX_VALUE also.
  69. }
  70. }
  71.  
  72. /**
  73. * Toggles the value of hideMiddleVals. If it is currently true, sets it to false, and vice-versa.
  74. */
  75. public void toggleHidden() //We are changing hideMiddlVals to true if it's false and to false if it's true.
  76. {
  77. if(this.hideMiddleVals){
  78. this.hideMiddleVals = false;
  79. }else{
  80. this.hideMiddleVals = true;
  81. }
  82. }
  83.  
  84. /**
  85. * Returns a String containing the values in the "numbers" array on a single line, separated by spaces with the
  86. * middle values hidden or all visible based on value of "hideMiddleValue" data member. There is a trailing space
  87. * on the end, so an example String returned may be: "5 X X X 67 ". NOTE: This does not output to System.out,
  88. * it generates and returns a String.
  89. *
  90. * @overrides toString in class java.lang.Object
  91. * @return A String with the values of the numbers array. Middle values are hidden if hideMiddleValue is true.
  92. */
  93. public java.lang.String toString() //converting the array to string from.
  94. {
  95. String output = ""+this.numbers[0]+" "; //first element.
  96. if(this.hideMiddleVals) //if hideMiddleVals is true we will make all the elements accept the first and last to X.
  97. {
  98. for(int i=1;i<ARR_SIZE-1;i++)
  99. {
  100. output+=("X ");
  101. }
  102. output+=(this.numbers[ARR_SIZE-1]+" "); //Last element.
  103.  
  104. }else{
  105. for(int i=1;i<ARR_SIZE;i++) //if the hideMiddleVasls is false.
  106. {
  107. output+=(this.numbers[i]+" ");
  108. }
  109. }
  110. return output;
  111. }
  112. /**
  113. * Accepts a user's guess for the game. Validates that it is either the character 'Y' or 'N'. If it is a valid guess,
  114. * sets the guess data member to the passed value and returns "true". If it is not valid it does not change
  115. * the value of guess and returns false.
  116. *
  117. * @param guessIn - The player's guess.
  118. * @return True if the passed guess is valid, false if it is not.
  119. */
  120. public boolean validatePlayerGuess(char guessIn)
  121. {
  122. if(guessIn=='Y' || guessIn=='N') { //checking if guess by the user is 'Y' or 'N'.
  123. this.guess = guessIn;
  124. return true;
  125. }else {
  126. return false;
  127. }
  128. }
  129. /**
  130. * Checks to see if player's guess was correct, and constructs and returns a String that reports if they are correct or
  131. * incorrect, and appends the correct sum of the array.
  132. * @return A String reporting the results of the game.
  133. */
  134. public java.lang.String getResult()
  135. {
  136. this.getArraySum();
  137. if(this.arraySum>this.guessTarget) //checking if the sum of array is greater than the guessTarget.
  138. {
  139. if(this.guess=='Y') //if user enters 'Y' it's correct and we return the following string.
  140. {
  141. return "You guessed correctly! The sum was "+this.arraySum+"!";
  142. }else{
  143. return "You guessed wrong! The sum was "+this.arraySum+"!";
  144. }
  145. }else{ //Complete opposite condition.
  146. if(this.guess=='Y')
  147. {
  148. return "You guessed wrong! The sum was "+this.arraySum+"!";
  149. }else{
  150. return "You guessed correctly! The sum was "+this.arraySum+"!";
  151. }
  152. }
  153. }
  154.  
  155. /**
  156. * Retrieves the numbers array. Used for testing, do not change.
  157. * @return The numbers array.
  158. */
  159. public int[] getNumbers()
  160. {
  161. return numbers;
  162. }
  163.  
  164. /**
  165. * Retrieves the sum of the numbers in the array. Used for testing, do not change.
  166. * @return The value of arraySum.
  167. */
  168. public int getArraySum() //returning the sum of the elements of the array.
  169. {
  170. for(int i=0;i<ARR_SIZE;i++)
  171. {
  172. this.arraySum+=this.numbers[i];
  173. }
  174. return arraySum;
  175. }
  176. }
  177.  
  178.  
  179. The Code Used for RamdomGuessGameTest.java:
  180.  
  181.  
  182. import java.util.Scanner;
  183.  
  184. public class RandomGuessGameTest {
  185. public static void main(String[] args) {
  186. Scanner scanner = new Scanner(System.in);
  187. RandGuessGame game = new RandGuessGame(new java.util.Random());
  188. game.populateArray();
  189. System.out.println("Player Guess Corretly");
  190. System.out.println(game.toString());
  191. while(true)
  192. {
  193. System.out.println("Is the number greater than "+250+"?");
  194. System.out.print("(Y or N): ");
  195. char guess = scanner.next().charAt(0);
  196. if(game.validatePlayerGuess(guess))
  197. {
  198. System.out.println(game.getResult());
  199. break;
  200. }
  201. }
  202. game.toggleHidden();
  203. System.out.println(game.toString());
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment