liangm20

Unit 5 Test (Questions 3,4,5)

Nov 9th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.65 KB | None | 0 0
  1. Michelle Liang
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9. Question 3 (user tries to guess a random number generated by the computer) Pseudocode:
  10.  
  11. 1. Create variables for the amount of guesses per game, the total amount of guesses from all games played, the amount of games played, the random number that the computer generates, and a scanner to ask the user for their guess. Tell the user what the random number generated is in between (in this case, 1-15), and ask the user to guess that number.
  12. 2. If the user's guess is too low, print out "too low" and ask the user if he/she wants to guess again. If too high, print out "too high" and ask the user if he/she wants to guess again. If the user's guess equals the random number, print out a congratulations, with the amount of guesses needed to guess the number during that game.
  13. 3. Then, ask the user if they want to play another game. If the user says yes, then reset the guess counter and play the game again. If the user declines, print out a summary of how many games the user played, how many guesses they made in total from all games played, and the average amount of guesses that the user made per game.
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. Question 3 Code:
  22.  
  23. //this is Michelle's code
  24. //11/9/16
  25. import java.util.*;
  26. public class question3
  27. {
  28. public static void main (String [] args)
  29. {
  30. int guess=0; //variable that counts number of guesses per game
  31. double plays=0; //variable that counts number of games played; double beceause used for the calculation of the average which is double
  32. double totalguess=0; //variable that counts grand total of guesses from all games combined; double beceause used for the calculation of the average which is double
  33. Random rand = new Random();
  34. int random = rand.nextInt(15)+1; //creating new random number from 1-15
  35. Scanner scan = new Scanner(System.in);
  36. System.out.println("Welcome to Michelle's guessing game! Guess a number between 1 and 15.");
  37. int user = scan.nextInt(); //variable for user input
  38. guessing(user,random,guess,plays,totalguess);
  39. }
  40. public static void guessing(int user, int random, double guess,double plays,double totalguess) //method that determines what happens if user input equals/is less than/is greater than the actual number
  41. {
  42. if (user==random) //what happens if user input equals random number
  43. {
  44. plays=plays+1; //counting number of games played
  45. guess = guess+1; //counting number of guesses
  46. totalguess=totalguess+guess; //counting total number of guesses from all games combined
  47. Scanner scan4 = new Scanner(System.in);
  48. System.out.println("Congratulations! You guessed the number " + random + " in " + guess + " tries!");
  49. System.out.println("Do you want to play the game again? The computer will generate another random number from 1-15.");
  50. String x = scan4.next();
  51. if(x.equalsIgnoreCase("Y") || x.equalsIgnoreCase("YES")) //if user wants to play another game
  52. {
  53. Random rand = new Random();
  54. int random2 = rand.nextInt(15)+1; //generating another random number from 1-15
  55. Scanner scan = new Scanner(System.in);
  56. System.out.println("Welcome to Michelle's guessing game! Guess a number between 1 and 15.");
  57. guess=0; //resetting number of guesses per game
  58. int user2 = scan.nextInt(); //another variable for user input
  59. guessing(user2,random2,guess,plays,totalguess);
  60. }
  61. else //if user doesn't want to play another game, prints summary and statistics
  62. {
  63. System.out.print("You played " + plays + " games, with a grand total of " + totalguess + " guesses, and an average of " + totalguess/plays + " guesses per game.");
  64. }
  65. }
  66. if (user>random) //what happens if user input is greater than random number
  67. {
  68. guess = guess+1; //counting number of guesses per game
  69. System.out.println("Your guess was too high.");
  70. ask(user,random,guess,plays,totalguess);
  71. }
  72. if(user<random) //what happens if user input is less than random number
  73. {
  74. guess=guess+1; //counting number of guesses per game
  75. System.out.println("Your guess was too low.");
  76. ask(user,random,guess,plays,totalguess);
  77. }
  78. }
  79. public static void ask(int user, int random,double guess,double plays,double totalguess) //method that asks user if they want to guess again
  80. {
  81. Scanner scan2 = new Scanner(System.in);
  82. System.out.println("Do you want to guess again? Type Y or N.");
  83. String s = scan2.next();
  84. if(s.equalsIgnoreCase("Y") || s.equalsIgnoreCase("yes"))
  85. {
  86. Scanner scan3 = new Scanner(System.in);
  87. System.out.println("Guess another number from 1-15."); //allows user to guess again
  88. user = scan3.nextInt();
  89. guessing(user,random,guess,plays,totalguess);
  90. }
  91. else
  92. {
  93. System.out.println("You played " + plays + " games, with a grand total of " + totalguess + " guesses, and an average of " + totalguess/plays + " guesses per game."); //if the user doesn't want to guess again
  94. }
  95. }
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. Question 4 (computer tries to guess the number that the user has in mind) Pseudocode:
  105.  
  106. 1. Create variables for a random number that the computer generates per game and the amount of guesses that the computer made per game. Ask the user to think of a number.
  107. 2. Then, the computer will ask the user if the random number generated is the number the user is thinking of.
  108. 3. If the random number does not equal the user's, then ask the user if it was too high or too low. If too high, then the computer generates another random number that is lower than its previous guess and repeats step 2. If too low, then the computer generates another random number that is higher than its previous guess and repeats step 2. This goes on until the random number generated equals the user's number.
  109. 4. If the random number equals the user's, print out a congratulations, and the total amount of times the computer had to guess before getting the user's number.
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. Question 4 Code:
  118.  
  119. //this is Michelle's code
  120. //11/9/16
  121. import java.util.*;
  122. public class question4
  123. {
  124. public static void main (String [] args)
  125. {
  126. Random rand = new Random();
  127. int comp = rand.nextInt(10)+1; //creating new random number for the computer from 1-10
  128. int guess = 0; //variable that counts number of guesses made by the computer
  129. System.out.println("Welcome to Michelle's guessing game! The computer will try to guess the user's number.");
  130. System.out.println("Think of a number from 1-10.");
  131. System.out.println("Got it? Okay, now the computer will try and guess your number.");
  132. guessing(guess,comp);
  133. }
  134. public static void guessing(int guess,int comp) //method making the computer guess random numbers
  135. {
  136. Scanner scan = new Scanner(System.in);
  137. System.out.println("Is your number " + comp + "? Type Y or N."); //asking user if the random number is the user's number
  138. String s = scan.next();
  139. ask(s,guess,comp);
  140. }
  141. public static void ask(String s, int guess,int comp) //asks user if random number was too high or low
  142. {
  143. if (s.equalsIgnoreCase("N") || s.equalsIgnoreCase("no")) //if the random number is not the user's number
  144. {
  145. guess = guess+1; //counting number of guesses made by the computer
  146. Scanner scan2 = new Scanner(System.in);
  147. System.out.println("Was my answer too high or too low?");
  148. String a = scan2.nextLine();
  149. if(a.equalsIgnoreCase("too high")) //what happens if it's too high
  150. {
  151. Random rand = new Random();
  152. comp = rand.nextInt(comp-1)+1; //if too high, computer then generates a number that'll be lower than its previous guess
  153. guessing(guess,comp);
  154. }
  155. if(a.equalsIgnoreCase("too low")) //what happens if it's too low
  156. {
  157. Random rand = new Random();
  158. comp = rand.nextInt(10-comp)+(comp+1); //if too low, computer then generates a number that'll be higher than its previous guess
  159. guessing(guess,comp);
  160. }
  161. }
  162. else //if random number equals user's number
  163. {
  164. guess=guess+1; //counting number of guesses made by the computer
  165. System.out.println("I got your number of " + comp + " correct in " + guess + " tries!");
  166. }
  167. }
  168. }
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176. Question 5 (rock, paper, scissors) Pseudocode:
  177.  
  178. 1. Have the computer generate a random number from 0-8. If the number is between 0 and 2, inclusive, then the computer generates a string that contains "ROCK." If between 3 and 5, inclusive, then the computer generates a string that contains "PAPER." If between 6 and 8, inclusive, then the computer generates a string that contains "SCISSORS."
  179. 2. Ask the user to choose between rock, paper, or scissors. Compare this with the computer's generated string. Then, create if statements and instances for all 9 possibilities (user=rock, computer=rock; user=rock, computer=paper; user=rock, computer=scissors; user=paper, computer=rock; user=paper, computer=paper; user=paper, computer=scissors; user=scissors, computer=rock; user=scissors, computer=paper; user=scissors, computer=scissors). Tell the user if they won, lost, or tied.
  180. 3. Ask the user if they want to play again. If yes, repeat steps 1 and 2. If no, the program ends.
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188. Question 5 Code:
  189.  
  190. //this is Michelle's code
  191. //11/9/16
  192. import java.util.*;
  193. public class question5
  194. {
  195. public static void main (String[] args)
  196. {
  197. System.out.println("Welcome to Michelle's game of Rock, Paper, and Scissors!");
  198. System.out.println("The computer will now generate a random choice of either rock, paper, or scissors.");
  199. game();
  200. }
  201. public static void game() //method that generates the computer's choice of either rock, paper, or scissors
  202. {
  203. String computer = new String(); //creating new string
  204. Random rand = new Random();
  205. int x = rand.nextInt(9); //generating random number from 0-8
  206. if(x>=0 && x<=2) //if number is between 0 and 2, inclusive, then the computer choose ROCK
  207. {
  208. computer = "ROCK";
  209. }
  210. if(x>=3 && x<=5) //if number is between 3 and 5, inclusive, then the computer chooses PAPER
  211. {
  212. computer = "PAPER";
  213. }
  214. if(x>=6 && x<=8) //if number is between 6 and 8, inclusive, then the computer chooses SCISSORS
  215. {
  216. computer = "SCISSORS";
  217. }
  218. Scanner scan = new Scanner(System.in);
  219. System.out.println("Type in your choice of rock, paper, or scissors. Remember, rock beats scissors, paper beats rock, and scissors beats paper."); //prompts user for their choice
  220. String answer = scan.next(); //user's answer
  221. result(answer,computer);
  222. }
  223. public static void result(String answer, String computer) //determines what happens for all of the possibilites
  224. {
  225. if(answer.equalsIgnoreCase("rock") && computer.equals("SCISSORS")) //if user chooses rock and computer chooses scissors
  226. {
  227. Scanner rock1 = new Scanner(System.in);
  228. System.out.println("Congratulations! The computer's choice was " + computer + ". You beat the computer. Want to play again? Type Y or N.");
  229. String a = rock1.next();
  230. if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
  231. {
  232. game(); //goes back and plays the game again
  233. }
  234. else //ends the program if user wants to quit
  235. {
  236. System.out.print("Okay, goodbye.");
  237. }
  238. }
  239. if(answer.equalsIgnoreCase("rock") && computer.equals("PAPER")) //if user chooses rock and computer chooses paper
  240. {
  241. Scanner rock2 = new Scanner(System.in);
  242. System.out.println("Sorry, the computer's choice was " + computer + ". You lost to the computer. Want to play again? Type Y or N.");
  243. String a = rock2.next();
  244. if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
  245. {
  246. game();
  247. }
  248. else
  249. {
  250. System.out.print("Okay, goodbye.");
  251. }
  252. }
  253. if(answer.equalsIgnoreCase("rock") && computer.equals("ROCK")) //if user chooses rock and computer chooses rock
  254. {
  255. Scanner rock3 = new Scanner(System.in);
  256. System.out.println("Both you and computer chose ROCK. It's a tie. Want to play again? Type Y or N.");
  257. String a = rock3.next();
  258. if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
  259. {
  260. game();
  261. }
  262. else
  263. {
  264. System.out.print("Okay, goodbye.");
  265. }
  266. }
  267. if(answer.equalsIgnoreCase("paper") && computer.equals("SCISSORS")) //if user chooses paper and computer chooses scissors
  268. {
  269. Scanner paper1 = new Scanner(System.in);
  270. System.out.println("Sorry, the computer's choice was " + computer + ". You lost to the computer. Want to play again? Type Y or N.");
  271. String a = paper1.next();
  272. if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
  273. {
  274. game();
  275. }
  276. else
  277. {
  278. System.out.print("Okay, goodbye.");
  279. }
  280. }
  281. if(answer.equalsIgnoreCase("paper") && computer.equals("PAPER")) //if user chooses paper and computer chooses paper
  282. {
  283. Scanner paper2 = new Scanner(System.in);
  284. System.out.println("Both you and computer chose PAPER. It's a tie. Want to play again? Type Y or N.");
  285. String a = paper2.next();
  286. if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
  287. {
  288. game();
  289. }
  290. else
  291. {
  292. System.out.print("Okay, goodbye.");
  293. }
  294. }
  295. if(answer.equalsIgnoreCase("paper") && computer.equals("ROCK")) //if user chooses paper and computer chooses rock
  296. {
  297. Scanner paper3 = new Scanner(System.in);
  298. System.out.println("Congratulations! The computer's choice was " + computer + ". You beat the computer. Want to play again? Type Y or N.");
  299. String a = paper3.next();
  300. if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
  301. {
  302. game();
  303. }
  304. else
  305. {
  306. System.out.print("Okay, goodbye.");
  307. }
  308. }
  309. if(answer.equalsIgnoreCase("scissors") && computer.equals("SCISSORS")) //if user chooses scissors and computer chooses scissors
  310. {
  311. Scanner scissors1 = new Scanner(System.in);
  312. System.out.println("Both you and computer chose SCISSORS. It's a tie. Want to play again? Type Y or N.");
  313. String a = scissors1.next();
  314. if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
  315. {
  316. game();
  317. }
  318. else
  319. {
  320. System.out.print("Okay, goodbye.");
  321. }
  322. }
  323. if(answer.equalsIgnoreCase("scissors") && computer.equals("PAPER")) //if user chooses scissors and computer chooses paper
  324. {
  325. Scanner scissors2 = new Scanner(System.in);
  326. System.out.println("Congratulations! The computer's choice was " + computer + ". You beat the computer. Want to play again? Type Y or N.");
  327. String a = scissors2.next();
  328. if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
  329. {
  330. game();
  331. }
  332. else
  333. {
  334. System.out.print("Okay, goodbye.");
  335. }
  336. }
  337. if(answer.equalsIgnoreCase("scissors") && computer.equals("ROCK")) //if user chooses scissors and computer chooses rock
  338. {
  339. Scanner scissors3 = new Scanner(System.in);
  340. System.out.println("Sorry, the computer's choice was " + computer + ". You lost to the computer. Want to play again? Type Y or N.");
  341. String a = scissors3.next();
  342. if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
  343. {
  344. game();
  345. }
  346. else
  347. {
  348. System.out.print("Okay, goodbye.");
  349. }
  350. }
  351. }
  352. }
Add Comment
Please, Sign In to add comment