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

Java programing, I need to present the questions from the input file in a random

ID: 3696310 • Letter: J

Question

Java programing, I need to 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(i.e the scoring must take into account the order in which the question were presented).


/**
* Write a description of class TriviaTest here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
import java.io.*;
public class TriviaRandom
{
private String[] correctAnswers;
private String[] responses;
private int numQuestions;
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);
//open the file with the questions and answers
File questionFile = new File(filename);
if(!questionFile.exists())
{
System.out.println("Error;"+filename+"not found");
System.exit(0);
}
fileInput = new Scanner(questionFile);
//first read the numver of question in the file
numQuestions = fileInput.nextInt();
//read correct answer from the input file
correctAnswers = new String[numQuestions];
for(int i=0;i correctAnswers[i]=fileInput.next();
responses = new String[numQuestions];
fileInput.nextLine();
}

public void giveTest()
{
//Display each question and records an answe
System.out.println("Answer each of the following: T or t for True and F for False ");
for (int i=0;i {
System.out.println((i+1)+"."+fileInput.nextLine());
System.out.print("Answer; ");
responses[i]=(input.next()).toUpperCase();//get user's response, convert to upper case
System.out.println();

}

System.out.println("you scored" +scoreTest()+"% on the test");
showCorrectAnswers();

fileInput.close();
}

private int scoreTest()
{
//scores the test and returns the percent of correct answers
int correct = 0;
for (int i=0; i if(responses[i].equals(correctAnswers[i]))
correct++;
return Math.round((100*correct)/numQuestions);
}

private void showCorrectAnswers()
{
//Display the correct answers
System.out.println(" Correct answers: Your Answers:");
for (int i =0;i System.out.println((i+1)+". "+correctAnswers[i]+" "+responses[i]);
}

  
}

and here is what the .txt file look like

3 //number of question

T F T // answer

Rain is wet.

Sunny is cool.

Computer is awesome.

// 5 lines in total idk why these lines are double spaced

Explanation / Answer

/*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;
   }
   }

   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]);
   }


   public void giveTest(){
       System.out.println("Answer each of the following: T or t for True and F or f for False ");
       fileQuestions = new String[numQuestions];
       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();
   }

}

/*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();
   }
}

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