Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.81 KB | None | 0 0
  1. package miniproject9;
  2.  
  3.  
  4. //Author : Zeynel Tok
  5. import java.util.*;
  6. import java.io.*;
  7. public class Miniproject9
  8. {
  9. public static void main(String[] p) throws IOException
  10. {
  11. QuestionBank qb = new QuestionBank();
  12. setAllQuestionsAndAnswers(qb);
  13. askQuestion(qb);
  14.  
  15. }
  16.  
  17. public static void setAllQuestionsAndAnswers(QuestionBank qb)
  18. {
  19. String[] question = {"How many teams did Shaquille O'Neal play for ? \n A: 3\n B: 4\n C: 5\n D: 6",
  20. "Which duo have the second highest scoring average in a season? \n A: Jordan & Pippen 1995-1996\n B: Curry & Thompson 2015-2016\n C: Kobe & Shaq 2002-2003\n D: Durant & Westbrook 2015-2016",
  21. "Who won the scoring leader of the first edition of the NBA back in 1946-1947? \n A: Max Zaslofsky\n B: Paul Arizin\n C: Joe Fulks\n D: George Mikan",
  22. "The big O was the first player to average a triple double in the 1961-1962 NBA season. What was their record that season? \n A: 43-37\n B: 45-35\n C: 50-30\n D: 66-14",
  23. "Who has the most career triple double in the NBA?\n A: Oscar Robertson\n B: Kareem Abdul-Jabbar\n C: Russell Westbrook\n D: Jason Kidd",
  24. "In 1997, the Washington Bullets changed their name to the Washington Wizards. Who did they draft in the Second round that year? \n A: Gordon Malone \n B: Ed Elisma \n C: God Shammgod \n D: Cedric Handerson",
  25. "How many years did Kobe Bryant wore #8 for?\n A: 10\n B: 7\n C: 8\n D: 11",
  26. "In the 2015-2016 NBA season, The Golden State Warriors set a record of the best regular season record with a 73-9 record. Who was the 6th team they lost to? \n A: LA Clippers\n B: LA Lakers\n C: Detroit Pistons\n D: Dallas Mavericks",
  27. "In the 1961-1962 NBA Season, Wilt Chamberlain scored 100 points against the New York Knicks. Who did the Knicks play in the following fixture?\n A: Philadelphia Warriors\n B: LA Lakers\n C: St.Louis Hawks\n D: Detroit Pistons",
  28. "Who was the second tallest player to play for Houston Rockets?\n A: Yao Ming\n B: Hakeem Olajuwon\n C: Chuck Nevitt\n D: Ralph Sampson"};
  29.  
  30. String[] answer = {"D","C", "C", "A", "A", "C", "A", "B", "A", "C"};
  31.  
  32. for(int i=0;i<10;i++)
  33. {
  34. qb.setQuestion(question[i], i);
  35. qb.setAnswer(answer[i], i);
  36. }
  37. }
  38.  
  39. public static void askQuestion(QuestionBank qb) throws IOException
  40. {
  41. int i;
  42. int score;
  43. String[] usedQuestions = new String[10];
  44. String[] markTableQuestions = new String[10];
  45. String[] markTableMarks = new String[10];
  46. Scanner s = new Scanner(System.in);
  47. System.out.println("Whenever you want to save your quiz remember to type save. \n If you want to start a new game type new \n Otherwise savdata will be searched");
  48. if (s.nextLine().equals("new"))
  49. {
  50. i=0;
  51. score=0;
  52. for (int y=0;y<10;y++)
  53. {
  54. usedQuestions[y] = null;
  55. markTableQuestions[y] = null;
  56. markTableMarks[y] = null;
  57. }
  58. }
  59. else
  60. {
  61. i=loadI();
  62. score=loadScore();
  63. usedQuestions=loadUsedQuestions();
  64. markTableQuestions=loadMarkTableQuestions();
  65. markTableMarks=loadMarkTableMarks();
  66. }
  67.  
  68. String userAnswer = "";
  69. String[] userAnswers = new String[10];
  70.  
  71.  
  72. while (i<10)
  73. {
  74. String question = "";
  75. int x = 0;
  76. int m = 0;
  77. boolean contains = true;
  78. while (contains == true )
  79. {
  80. x = (int)(Math.random() * 10);
  81. question = qb.getQuestion(x);
  82. contains = contains(usedQuestions, question);
  83. }
  84.  
  85. String correctAnswer = qb.getAnswer(x);
  86. System.out.println("");
  87. System.out.println(question);
  88.  
  89. for (int z=1;z<=2;z++)
  90. {
  91. userAnswer = s.nextLine();
  92. if (userAnswer.equalsIgnoreCase("save"))
  93. {
  94. try {
  95. save(i, usedQuestions, score, markTableQuestions, markTableMarks);
  96. } catch (FileNotFoundException e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. if (userAnswer.equalsIgnoreCase(correctAnswer))
  101. {
  102. System.out.println("Correct Answer!");
  103. m = mark();
  104. score = score + m;
  105. usedQuestions[i] = question;
  106. break;
  107.  
  108. }
  109. else if (z==2)
  110. {
  111. System.out.println("Sorry, the correct answer was: " + correctAnswer);
  112. usedQuestions[i] = question;
  113. }
  114. else
  115. {
  116. System.out.println("You are Wrong, try " +z +" out of 2");
  117.  
  118. }
  119.  
  120. }
  121. markTable(markTableQuestions, markTableMarks, question, m, i);
  122. userAnswers[i] = userAnswer;
  123. i++;
  124. }
  125. System.out.println("You got " +score+ " points");
  126. System.out.println("Would you like to see your mark table? Y/N ");
  127. String choice = s.nextLine();
  128. if (choice.equalsIgnoreCase("Y"))
  129. {
  130. sortMarkTable(markTableQuestions, markTableMarks);
  131. for(int a=0;a<10;a++)
  132. {
  133. System.out.println(markTableQuestions[a]);
  134. System.out.println("");
  135. System.out.println("For this question, your answer was " + userAnswers[a] + " which scored you " + markTableMarks[a] + " marks");
  136. System.out.println("");
  137. }
  138. }
  139. else
  140. {
  141. System.out.println("Thank you for playing!");
  142. }
  143. s.close();
  144. }
  145.  
  146. public static int mark()
  147. {
  148. int mark = (int)(Math.random() * 6 + 1);
  149.  
  150. return mark;
  151. }
  152.  
  153. public static boolean contains(String[] usedQuestions, String question)
  154. {
  155. for (String s: usedQuestions)
  156. {
  157. if (question == s)
  158. {
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164.  
  165. public static void markTable(String[] markTableQuestions, String[] markTableMarks, String question, int m, int i)
  166. {
  167. while (i<=10)
  168. {
  169. markTableQuestions[i] = question;
  170. markTableMarks[i] = Integer.toString(m);
  171. break;
  172. }
  173.  
  174. }
  175.  
  176. public static void sortMarkTable(String[] markTableQuestions, String[] markTableMarks)
  177. {
  178. String tempMark;
  179. String tempQuestion;
  180. for (int i=0;i<markTableMarks.length;i++)
  181. {
  182. for (int j=i;j>0;j--)
  183. {
  184. if(Integer.parseInt(markTableMarks[j]) > Integer.parseInt(markTableMarks[j-1]))
  185. {
  186. tempMark = markTableMarks[j];
  187. tempQuestion = markTableQuestions[j];
  188. markTableMarks[j] = markTableMarks[j-1];
  189. markTableQuestions[j] = markTableQuestions[j-1];
  190. markTableMarks[j-1] = tempMark;
  191. markTableQuestions[j-1] = tempQuestion;
  192.  
  193. }
  194. }
  195.  
  196. }
  197.  
  198. }
  199.  
  200. public static void save(int i, String[] usedQuestions, int score, String[] markTableQuestions, String[] markTableMarks) throws FileNotFoundException
  201. {
  202. System.out.println("Saving now...");
  203. PrintWriter saveI = new PrintWriter("C:\\QuizSave\\i.txt");
  204. saveI.println(i);
  205. saveI.close();
  206.  
  207. PrintWriter saveUsedQuestions = new PrintWriter("C:\\QuizSave\\usedQuestions.txt");
  208. for (int e=0;e<usedQuestions.length;e++)
  209. {
  210. saveUsedQuestions.println(usedQuestions[e]);
  211. }
  212. saveUsedQuestions.close();
  213. System.out.println("Successfully saved questions");
  214.  
  215. PrintWriter saveScore = new PrintWriter("C:\\QuizSave\\score.txt");
  216. saveScore.println(score);
  217. saveScore.close();
  218.  
  219. PrintWriter saveMarkTableQuestions = new PrintWriter("C:\\QuizSave\\markTableQuestions.txt");
  220. for (int e=0;e<markTableQuestions.length;e++)
  221. {
  222. saveMarkTableQuestions.println(markTableQuestions[e]);
  223. }
  224. saveMarkTableQuestions.close();
  225.  
  226. PrintWriter saveMarkTableMarks = new PrintWriter("C:\\QuizSave\\markTableMarks.txt");
  227. for (int e=0;e<markTableMarks.length;e++)
  228. {
  229. saveMarkTableMarks.println(markTableMarks[e]);
  230. }
  231. saveMarkTableMarks.close();
  232. System.out.println("Successfully saved marks");
  233.  
  234. System.out.println("Saving is now complete. \n Find your savedata in your QuizSave folder. \n Thanks for playing!");
  235.  
  236. System.exit(0);
  237.  
  238. }
  239.  
  240. public static int loadI() throws IOException
  241. {
  242. BufferedReader loadI = new BufferedReader(new FileReader("C:\\QuizSave\\i.txt"));
  243. String line;
  244. line = loadI.readLine();
  245. int i = Integer.parseInt(line);
  246. return i;
  247.  
  248. }
  249.  
  250. public static int loadScore() throws IOException
  251. {
  252. BufferedReader loadScore = new BufferedReader(new FileReader("C:\\QuizSave\\score.txt"));
  253. String line = loadScore.readLine();
  254. int score = Integer.parseInt(line);
  255. return score;
  256. }
  257. public static String[] loadUsedQuestions() throws IOException
  258. {
  259. String line;
  260. BufferedReader loadUsedQuestions = new BufferedReader(new FileReader("C:\\QuizSave\\usedQuestions.txt"));
  261. List<String> tempUsedQuestions = new ArrayList<String>();
  262. while ((line=loadUsedQuestions.readLine()) !=null)
  263. {
  264. tempUsedQuestions.add(line);
  265. }
  266. String[] usedQuestions = tempUsedQuestions.toArray(new String[10]);
  267. loadUsedQuestions.close();
  268. return usedQuestions;
  269. }
  270. public static String[] loadMarkTableQuestions() throws IOException
  271. {
  272. String line;
  273. BufferedReader loadMarkTableQuestions = new BufferedReader(new FileReader("C:\\QuizSave\\markTableQuestions.txt"));
  274. List<String> tempMarkTableQuestions = new ArrayList<String>();
  275. while ((line=loadMarkTableQuestions.readLine()) !=null)
  276. {
  277. tempMarkTableQuestions.add(line);
  278. }
  279. String[] markTableQuestions = tempMarkTableQuestions.toArray(new String[10]);
  280. loadMarkTableQuestions.close();
  281. return markTableQuestions;
  282. }
  283. public static String[] loadMarkTableMarks() throws IOException
  284. {
  285. String line;
  286. BufferedReader loadMarkTableMarks = new BufferedReader(new FileReader("C:\\QuizSave\\markTableMarks.txt"));
  287. List<String> tempMarkTableMarks = new ArrayList<String>();
  288. while ((line=loadMarkTableMarks.readLine()) !=null)
  289. {
  290. tempMarkTableMarks.add((line));
  291. }
  292. String[] markTableMarks = tempMarkTableMarks.toArray(new String[10]);
  293. loadMarkTableMarks.close();
  294. return markTableMarks;
  295. }
  296.  
  297.  
  298. }
  299.  
  300. class QuestionBank
  301. {
  302.  
  303. String[] question = new String[10];
  304. String[] answer = new String[10];
  305.  
  306.  
  307.  
  308.  
  309. public String getQuestion(int i) {
  310. return question[i];
  311. }
  312.  
  313. public void setQuestion(String question, int i) {
  314. this.question[i] = question;
  315. }
  316.  
  317. public String getAnswer(int i) {
  318. return answer[i];
  319. }
  320.  
  321. public void setAnswer(String answer, int i) {
  322. this.answer[i] = answer;
  323. }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement