Part 1 Use this example listed below, compile it, and run it. Note that this pro
ID: 3695229 • Letter: P
Question
Part 1
Use this example listed below, compile it, and run it. Note that this program consists of two classes. Don't forget to create the input file containing the questions and answers according to the following specifications:
1) read a list of true-false questions and answer from a file
2) interactively administer the test
3) score the test and return the score as a percentage
4) display the correct answers along with the test-taker's answers
A suggestion maybe to put the input file in the same folder as the program. Write at least ten real true/false questions for the input file.
public class TriviaTest{
private String[] correctAnswer;
private String[] response;
private int numQuestions;
private Scanner input;
private Scanner fileInput;
public TriviaTest(){
System.out.println("No filename supplied");
System.exit(0);}
public TriviaTest(String filename) throws IOException{
input = new Scanner(System.in);
File questionFile = new File(filename);
if(!questionFile.exists()){
System.out.println("Error: "+filename+" not found");
System.exit(0);}
fileInput = new Scanner(questionFile);
numQuestions = fileInput.nextInt();
correctAnswer = new String[numQuestions];
for(int i = 0;i
correctAnswers[i]=fileInput.next();
responses = new String[numQuestions];
fileInput.nextLine();
}
public void giveTest(){
System.out.println("Answer each of the following: T or t for True and F or f for False ");
for(int i = 0;i System.out.println((i+1)+". "+fileInput.nextLine());
System.out.print("Answer: ");
response[i]=(input.next()).toUpperCase();
System.out.println();}
System.out.println("You scored "+scoreTest()+"% on the test");
showCorrectAnswers();
fileInput.close();}
private int scoreTest(){
int correct=0;
for(int i=0;i
if(responses[i].equals(correctAnswers[i]))
correct++;
return Math.round((100*correct)/numQuestions);}
private void showCorrectAnswers(){
System.out.println(" Correct answer: Your Answers: ");
for(int i=0;i
System.out.println((i+1)+". "+correctAnswer[i]+" "+responses[i]);}}
public class TriviaTestGiver{
public static void main(String[] args) throws IOException{
Scanner input=new Scanner(System.in);
System.out.print("Enter filename: ");
String filename=input.nextLine();
TriviaTest test=new TriviaTest(filename);
test.giveTest();}}
Part 2
Create new classes called TriviaRandom and TriviaTestGiverRandom. When TriviaTestGiverRandom is run, it should present the questions from the input file in a random order, ensuring that each question is presented exactly once. Make sure that the test is scored correctly.
Explanation / Answer
/* Input File */
The first line of the input file contains the numbe rof questions infile. the seond lineholds he correct answers
( T or F) . Answers are separated by white spaces.The remainder of the file consists of questions, one question per line.
/**TriviaTest.java**/
import java.util.*;
import java.io.*;
public class TriviaTest{
private String[] correctAnswer;
private String[] response;
private int numQuestions;
private Scanner input;
private Scanner fileInput;
public TriviaTest(){
System.out.println("No filename supplied");
System.exit(0);
}
public TriviaTest(String filename) throws IOException{
input = new Scanner(System.in);
File questionFile = new File(filename);
if(!questionFile.exists()){
System.out.println("Error: "+filename+" not found");
System.exit(0);
}
fileInput = new Scanner(questionFile);
numQuestions = fileInput.nextInt();
correctAnswer = new String[numQuestions];
for(int i = 0;i < numQuestions;i++)
correctAnswer[i]=fileInput.next();
response = new String[numQuestions];
fileInput.nextLine();
}
public void giveTest(){
System.out.println("Answer each of the following: T or t for True and F or f for False ");
for(int i = 0;i < numQuestions; i++){
System.out.println((i+1)+". "+fileInput.nextLine());
System.out.print("Answer: ");
response[i]=(input.next()).toUpperCase();
System.out.println();
}
System.out.println("You scored "+scoreTest()+"% on the test");
showCorrectAnswers();
fileInput.close();
}
private int scoreTest(){
int correct=0;
for(int i=0;i<numQuestions;i++)
if(response[i].equals(correctAnswer[i]))
correct++;
return Math.round((100*correct)/numQuestions);
}
private void showCorrectAnswers(){
System.out.println(" Correct answer: Your Answers: ");
for(int i=0;i<numQuestions;i++)
System.out.println((i+1)+". "+correctAnswer[i]+" "+response[i]);
}
}
/*TriviaTestGiver.java*/
import java.util.*;
import java.io.*;
public class TriviaTestGiver{
public static void main(String[] args) throws IOException{
Scanner input=new Scanner(System.in);
System.out.print("Enter filename: ");
String filename=input.nextLine();
TriviaTest test=new TriviaTest(filename);
test.giveTest();
}
}
/*TriviaRandom.java*/
import java.util.*;
import java.io.*;
import java.util.concurrent.ThreadLocalRandom;
public class TriviaRandom{
private String[] correctAnswer;
private String[] response;
private int numQuestions;
private String[] fileQuestions;
private Scanner input;
private Scanner fileInput;
public TriviaRandom(){
System.out.println("No filename supplied");
System.exit(0);
}
public TriviaRandom(String filename) throws IOException{
input = new Scanner(System.in);
File questionFile = new File(filename);
if(!questionFile.exists()){
System.out.println("Error: "+filename+" not found");
System.exit(0);
}
fileInput = new Scanner(questionFile);
numQuestions = fileInput.nextInt();
correctAnswer = new String[numQuestions];
for(int i = 0;i < numQuestions;i++)
correctAnswer[i]=fileInput.next();
response = new String[numQuestions];
fileInput.nextLine();
}
static void shuffleArray(String[] questionFile, String[] response){
// Shuffling the array inorder to print random questions
// shuffling the respose array along with the question array
Random rnd = ThreadLocalRandom.current();
for (int i = questionFile.length - 1; i > 0; i--){
int index = rnd.nextInt(i + 1);
// Simple swap
String a = questionFile[index];
questionFile[index] = questionFile[i];
questionFile[i] = a;
String b = response[index];
response[index] = response[i];
response[i] = b;
}
}
public void giveTest(){
System.out.println("Answer each of the following: T or t for True and F or f for False ");
for(int i = 0;i < numQuestions; i++){
fileQuestions[i] = fileInput.nextLine();
}
// calling the shuffle function
shuffleArray(fileQuestions,response);
for(int i = 0;i < numQuestions; i++){
System.out.println((i+1)+". "+fileQuestions[i]);
System.out.print("Answer: ");
response[i]=(input.next()).toUpperCase();
System.out.println();
}
//System.out.println((i+1)+". "+fileInput.nextLine());
//System.out.print("Answer: ");
//response[i]=(input.next()).toUpperCase();
//System.out.println();
System.out.println("You scored "+scoreTest()+"% on the test");
showCorrectAnswers();
fileInput.close();
}
private int scoreTest(){
int correct=0;
for(int i=0;i<numQuestions;i++)
if(response[i].equals(correctAnswer[i]))
correct++;
return Math.round((100*correct)/numQuestions);
}
private void showCorrectAnswers(){
System.out.println(" Correct answer: Your Answers: ");
for(int i=0;i<numQuestions;i++)
System.out.println((i+1)+". "+correctAnswer[i]+" "+response[i]);
}
}
/*TriviaTestGiverRandom.java*/
import java.util.*;
import java.io.*;
public class TriviaTestGiverRandom{
public static void main(String[] args) throws IOException{
Scanner input=new Scanner(System.in);
System.out.print("Enter filename: ");
String filename=input.nextLine();
TriviaRandom test=new TriviaRandom(filename);
test.giveTest();
}
}