In: Computer Science
Homework Description: Creating your own quiz
As you likely have noticed, if you retook an ICON reading quiz in this course, you most likely received a different set of questions. This is because the questions are randomly selected from a question bank of possible questions where the number of questions on the quiz is (usually) less than the number of questions in the question bank. In this homework assignment, you will write your own question bank of at least 10 quiz questions (they do not have to be related to programming) and will write a menu-based program with three options: (1) take quiz, (2) view question statistics, and (3) quit. If option (1) from the menu is selected, the user should be presented with a random subset of three of the questions from your question bank (note: you should take care to make sure that none of your questions are repeated). After each question is presented, the user will provide an answer before the program presents the next question. It is up to you how your program informs the user as to how many and/or which questions he/she got correct on the quiz (e.g., displaying a message at the end of each question and/or at the end of the quiz). If option (2) from the menu is selected, the program should display (based on all quizzes that have been taken while your program has been running), for each question, the number of times the question has been asked and the number of times the question has been correct. To display this information, the formatting is up to you, but one option would be to display a fraction x/yx/y where xx is the number of correct answers and yy is the number of times the question has been asked. You should also display each question as the user doesn’t know the order of the questions in the question bank. Your menu-based program should continue to run until the user selects option 3 (note, you should use a blocking-while loop, or something similar, to make sure that the user enters a valid choice of 1, 2, or 3.)
Implementation hints
In creating your question bank, it is recommended that you create two arrays: one array (of strings) to store the quiz questions and one array (of integers) to store the correct quiz answers. All of your quiz answers should consistently be of the same type and it is recommended that you use integers to store the quiz answers. For example, the following code illustrates creating a question bank with only two questions (remember that you need at least 10 questions):
string questions[] = {"How many days are in January?", "How many days are in February (non-leap year)?"}; int answers[] = { 31, // January 28 }; // February (non-leap year)
Note, since questions that ask how many days are in a month is provided as an example, you may NOT use this as the theme for your questions. Please be creative and come up with your own questions.(Note, also consider the possibility of multiple choice questions with integer options (e.g., 1, 2, 3, 4).)
import java.util.*;
class Menu{
public checkIfRepeat(int num, int[] repeat){
for (int x = 0; x<3; x++){
if (repeat[x]==num)
return false
}
return true
}
public static void main(String at[]) throws Exception{
Scanner R = new Scanner(System.in);
//Setting up a question bank
String[] ques = new String[]{'What is the capital of Chile?','What is the highest mountain in Britain?','What is the smallest country in the world?','Alberta is a province of which country?','How many countries still have the shilling as currency?','Which is the only vowel not used as the first letter in a US State?','What is the largest country in the world?','Where would you find the River Thames?','What is the hottest continent on Earth?','What is the longest river in the world?'};
String[] ans = new String[]{'Santiago','Ben Nevis','Vatican City','Canada','Four – Kenya, Uganda, Tanzania and Somalia','E','Russia','London, UK','Africa','River Nile'};
//Keeping Statistics
int[] times = new int[]{0,0,0,0,0,0,0,0,0,0};
int[] correct = new int[]{0,0,0,0,0,0,0,0,0,0};
int[] repeat = new int[]{0,0,0}
int quit = 0,count=0;
System.out.println("Welcome to the Quiz!");
while(quit==0){
System.out.println("Enter your choice: ");
System.out.println("1: Take Quiz \n2: View Question Statistics \n3: Quit \n");
int choice = R.nextInt()
switch(choice){
//Take Quiz
case 1:
//Returns number between 0-9
int rand = ran.nextInt(10);
//Checking for repeats
while (count!=3){
//Checking if this random question has been repeated, if it hasn't only then will it display
if (checkIfRepeat(rand,repeat)){
repeat[count]=rand;
count++;
times[rand]++;
//Asking the question
System.out.println("Question No. "+ count + "!");
System.out.println(ques[rand]);
System.out.println("Your Answer: ");
//Taking the answer
String answer = R.nextLine();
//Checking the answer
if (answer.equalsIgnoreCase(ans[rand]){
correct[rand]++;
System.out.println("Correct!!\n");
}
}
else
rand = ran.nextInt(10);
}
count=0;
repeat = {0,0,0};
break;
case 2:
System.out.println("Statistics: \n");
System.out.println("Questions attempted: \n");
int C=1;
for (int x=0;x<times.length){
if (times[x]>0){
System.out.print(C+". ");
System.out.println(ques[x]+"\tAsked: " + times[x] + "\tAnswered Correctly: " + correct[x])
C++;
}
}
if (C==1)
System.out.println("No Question has been attempted\n");
break;
case 3:
quit = 1;
break;
default:
System.out.println("Give a correct option");
}
}
}
}
}