Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package miniproject9;
- //Author : Zeynel Tok
- import java.util.*;
- import java.io.*;
- public class Miniproject9
- {
- public static void main(String[] p) throws IOException
- {
- QuestionBank qb = new QuestionBank();
- setAllQuestionsAndAnswers(qb);
- askQuestion(qb);
- }
- public static void setAllQuestionsAndAnswers(QuestionBank qb)
- {
- String[] question = {"How many teams did Shaquille O'Neal play for ? \n A: 3\n B: 4\n C: 5\n D: 6",
- "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",
- "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",
- "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",
- "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",
- "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",
- "How many years did Kobe Bryant wore #8 for?\n A: 10\n B: 7\n C: 8\n D: 11",
- "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",
- "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",
- "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"};
- String[] answer = {"D","C", "C", "A", "A", "C", "A", "B", "A", "C"};
- for(int i=0;i<10;i++)
- {
- qb.setQuestion(question[i], i);
- qb.setAnswer(answer[i], i);
- }
- }
- public static void askQuestion(QuestionBank qb) throws IOException
- {
- int i;
- int score;
- String[] usedQuestions = new String[10];
- String[] markTableQuestions = new String[10];
- String[] markTableMarks = new String[10];
- Scanner s = new Scanner(System.in);
- 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");
- if (s.nextLine().equals("new"))
- {
- i=0;
- score=0;
- for (int y=0;y<10;y++)
- {
- usedQuestions[y] = null;
- markTableQuestions[y] = null;
- markTableMarks[y] = null;
- }
- }
- else
- {
- i=loadI();
- score=loadScore();
- usedQuestions=loadUsedQuestions();
- markTableQuestions=loadMarkTableQuestions();
- markTableMarks=loadMarkTableMarks();
- }
- String userAnswer = "";
- String[] userAnswers = new String[10];
- while (i<10)
- {
- String question = "";
- int x = 0;
- int m = 0;
- boolean contains = true;
- while (contains == true )
- {
- x = (int)(Math.random() * 10);
- question = qb.getQuestion(x);
- contains = contains(usedQuestions, question);
- }
- String correctAnswer = qb.getAnswer(x);
- System.out.println("");
- System.out.println(question);
- for (int z=1;z<=2;z++)
- {
- userAnswer = s.nextLine();
- if (userAnswer.equalsIgnoreCase("save"))
- {
- try {
- save(i, usedQuestions, score, markTableQuestions, markTableMarks);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- if (userAnswer.equalsIgnoreCase(correctAnswer))
- {
- System.out.println("Correct Answer!");
- m = mark();
- score = score + m;
- usedQuestions[i] = question;
- break;
- }
- else if (z==2)
- {
- System.out.println("Sorry, the correct answer was: " + correctAnswer);
- usedQuestions[i] = question;
- }
- else
- {
- System.out.println("You are Wrong, try " +z +" out of 2");
- }
- }
- markTable(markTableQuestions, markTableMarks, question, m, i);
- userAnswers[i] = userAnswer;
- i++;
- }
- System.out.println("You got " +score+ " points");
- System.out.println("Would you like to see your mark table? Y/N ");
- String choice = s.nextLine();
- if (choice.equalsIgnoreCase("Y"))
- {
- sortMarkTable(markTableQuestions, markTableMarks);
- for(int a=0;a<10;a++)
- {
- System.out.println(markTableQuestions[a]);
- System.out.println("");
- System.out.println("For this question, your answer was " + userAnswers[a] + " which scored you " + markTableMarks[a] + " marks");
- System.out.println("");
- }
- }
- else
- {
- System.out.println("Thank you for playing!");
- }
- s.close();
- }
- public static int mark()
- {
- int mark = (int)(Math.random() * 6 + 1);
- return mark;
- }
- public static boolean contains(String[] usedQuestions, String question)
- {
- for (String s: usedQuestions)
- {
- if (question == s)
- {
- return true;
- }
- }
- return false;
- }
- public static void markTable(String[] markTableQuestions, String[] markTableMarks, String question, int m, int i)
- {
- while (i<=10)
- {
- markTableQuestions[i] = question;
- markTableMarks[i] = Integer.toString(m);
- break;
- }
- }
- public static void sortMarkTable(String[] markTableQuestions, String[] markTableMarks)
- {
- String tempMark;
- String tempQuestion;
- for (int i=0;i<markTableMarks.length;i++)
- {
- for (int j=i;j>0;j--)
- {
- if(Integer.parseInt(markTableMarks[j]) > Integer.parseInt(markTableMarks[j-1]))
- {
- tempMark = markTableMarks[j];
- tempQuestion = markTableQuestions[j];
- markTableMarks[j] = markTableMarks[j-1];
- markTableQuestions[j] = markTableQuestions[j-1];
- markTableMarks[j-1] = tempMark;
- markTableQuestions[j-1] = tempQuestion;
- }
- }
- }
- }
- public static void save(int i, String[] usedQuestions, int score, String[] markTableQuestions, String[] markTableMarks) throws FileNotFoundException
- {
- System.out.println("Saving now...");
- PrintWriter saveI = new PrintWriter("C:\\QuizSave\\i.txt");
- saveI.println(i);
- saveI.close();
- PrintWriter saveUsedQuestions = new PrintWriter("C:\\QuizSave\\usedQuestions.txt");
- for (int e=0;e<usedQuestions.length;e++)
- {
- saveUsedQuestions.println(usedQuestions[e]);
- }
- saveUsedQuestions.close();
- System.out.println("Successfully saved questions");
- PrintWriter saveScore = new PrintWriter("C:\\QuizSave\\score.txt");
- saveScore.println(score);
- saveScore.close();
- PrintWriter saveMarkTableQuestions = new PrintWriter("C:\\QuizSave\\markTableQuestions.txt");
- for (int e=0;e<markTableQuestions.length;e++)
- {
- saveMarkTableQuestions.println(markTableQuestions[e]);
- }
- saveMarkTableQuestions.close();
- PrintWriter saveMarkTableMarks = new PrintWriter("C:\\QuizSave\\markTableMarks.txt");
- for (int e=0;e<markTableMarks.length;e++)
- {
- saveMarkTableMarks.println(markTableMarks[e]);
- }
- saveMarkTableMarks.close();
- System.out.println("Successfully saved marks");
- System.out.println("Saving is now complete. \n Find your savedata in your QuizSave folder. \n Thanks for playing!");
- System.exit(0);
- }
- public static int loadI() throws IOException
- {
- BufferedReader loadI = new BufferedReader(new FileReader("C:\\QuizSave\\i.txt"));
- String line;
- line = loadI.readLine();
- int i = Integer.parseInt(line);
- return i;
- }
- public static int loadScore() throws IOException
- {
- BufferedReader loadScore = new BufferedReader(new FileReader("C:\\QuizSave\\score.txt"));
- String line = loadScore.readLine();
- int score = Integer.parseInt(line);
- return score;
- }
- public static String[] loadUsedQuestions() throws IOException
- {
- String line;
- BufferedReader loadUsedQuestions = new BufferedReader(new FileReader("C:\\QuizSave\\usedQuestions.txt"));
- List<String> tempUsedQuestions = new ArrayList<String>();
- while ((line=loadUsedQuestions.readLine()) !=null)
- {
- tempUsedQuestions.add(line);
- }
- String[] usedQuestions = tempUsedQuestions.toArray(new String[10]);
- loadUsedQuestions.close();
- return usedQuestions;
- }
- public static String[] loadMarkTableQuestions() throws IOException
- {
- String line;
- BufferedReader loadMarkTableQuestions = new BufferedReader(new FileReader("C:\\QuizSave\\markTableQuestions.txt"));
- List<String> tempMarkTableQuestions = new ArrayList<String>();
- while ((line=loadMarkTableQuestions.readLine()) !=null)
- {
- tempMarkTableQuestions.add(line);
- }
- String[] markTableQuestions = tempMarkTableQuestions.toArray(new String[10]);
- loadMarkTableQuestions.close();
- return markTableQuestions;
- }
- public static String[] loadMarkTableMarks() throws IOException
- {
- String line;
- BufferedReader loadMarkTableMarks = new BufferedReader(new FileReader("C:\\QuizSave\\markTableMarks.txt"));
- List<String> tempMarkTableMarks = new ArrayList<String>();
- while ((line=loadMarkTableMarks.readLine()) !=null)
- {
- tempMarkTableMarks.add((line));
- }
- String[] markTableMarks = tempMarkTableMarks.toArray(new String[10]);
- loadMarkTableMarks.close();
- return markTableMarks;
- }
- }
- class QuestionBank
- {
- String[] question = new String[10];
- String[] answer = new String[10];
- public String getQuestion(int i) {
- return question[i];
- }
- public void setQuestion(String question, int i) {
- this.question[i] = question;
- }
- public String getAnswer(int i) {
- return answer[i];
- }
- public void setAnswer(String answer, int i) {
- this.answer[i] = answer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement