Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a class, TriviaTest, with methods that: Read a list of true-false questio

ID: 3695939 • Letter: D

Question

Design a class, TriviaTest, with methods that:

Read a list of true-false questions and answers from a file,

interactively administer the test,

score the test and return the score as a percentage, and

display the correct answers along with the test-taker's answers.

Below is what I already have completed. I am struggling with how i need to arrange the information in the .txt file. Assuming that the information is in the file ComSci.txt, answer the questions above.

import java.util.*;
import java.io.*;
public class TriviaTest
{
private String[] correctAnswers;
private String[] responses;
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();
correctAnswers = new String[numQuestions];
for (int i = 0; i < numQuestions; 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 < numQuestions; i++)
  
{
System.out.println((i+1)+ "." + fileInput.nextLine());
System.out.print("Answer:");
  
responses[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 (responses[i].equals(correctAnswers[i]))
correct++;
  
return Math.round((100*correct) / numQuestions);
  
}
  
private void showCorrectAnswers()
{
System.out.println(" Correct answers: Your Answers:");
for (int i = 0; i < numQuestions; i++)
  
System.out.println((i+1)+". " + correctAnswers[i] + " " + responses[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();
}
}

  

Explanation / Answer

//Assume the answers for question number 1 is A,question number 2 is C and question number 3 is B
//TriviaTest.java
import java.util.*;
class TriviaTest
{
   public static void main(String args[])
   {
       int total_questions=3;
       int score=0;
       String response;
       String correct_Answer[]={"A","C","B"};
       for(int i=0;i<3;i++)
       {
       System.out.println(" Enter the given question number "+(i+1));
       Scanner input=new Scanner(System.in);
       int question_no=input.nextInt();
       System.out.println(" Enter Answer to the question");
   response=input.next();
       if(response.equals(correct_Answer[i]))
       {
           score++;
       }
       }
       if(score>0)
       {
       System.out.println(" Congrats ! Ur score is "+score+"out of "+total_questions);
       }
       else
       {
       System.out.println(" Sorry ! Ur score is "+score+"out of "+total_questions);
       }
   }
}
      

Sample Output:

E:cheggcheggqa>java TriviaTest
Enter the given question number 1
1
Enter Answer to the question
A
Enter the given question number 2
2
Enter Answer to the question
C
Enter the given question number 3
3
Enter Answer to the question
B
Congrats ! Ur score is 3out of 3

E:cheggcheggqa>java TriviaTest
Enter the given question number 1
1
Enter Answer to the question
B
Enter the given question number 2
2
Enter Answer to the question
A
Enter the given question number 3
3
Enter Answer to the question
D
Sorry ! Ur score is 0out of 3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote