JAVA program true/false quiz program. Your program will: present to the user a t
ID: 3576218 • Letter: J
Question
JAVA program true/false quiz program.
Your program will:
present to the user a true/false question
get from the user an answer (true/false)
advise the user if their answer was correct or incorrect
keep score (number correct and number of questions asked)
The questions and and correct answers are to be stored in a .txt file (name it questionAnswers.txt). I suggest that your file have the format of answer question as below (where “true” is the correct answer and “Java is the best language is the question”):
true Java is the best language
When the quiz is completed:
tell the user the percentage of questions answered correctly
save to a file (name it results.txt, append to the file the information re: each time the quiz is taken) the following information about the quiz:
number of correct answers
number of questions asked
elapsed time (in seconds) from presentation of first question to submission of last answer.
I ALREADY WROTE THE PROGRAM THE ONLY PROBLEM IS I NEED TO GET THE QUESTIONS AND ANSWERS FROM .TXT FILE. I HAVE THE QUESTIONS AND ANSWERS HARD CODED IN MY PROGRAM I NEED TO GET RID OF THEM. HERE IS WHAT I HAVE SO FAR:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Quiz
{
//Declare instance variables
String name[] = new String[100];
String answer[][] = new String[100][10];
String correctAnswers[]={"a","b"};
private static final String FILENAME = "questionAnswers.txt";
private static final String FILENAME1 = "result.txt";
PrintWriter fw = null;
PrintWriter resultf = null;
public void takeInputs()
{
//Declare Variables
Scanner scan=new Scanner(System.in);
int i=0;
double score = 0;
String sentinel="";
try{
fw = new PrintWriter(FILENAME);
resultf = new PrintWriter(FILENAME1);
//Take inputs
System.out.print(" Enter your Name: " );
sentinel= scan.next();
final long startTime = System.currentTimeMillis();
for (i=0; i < answer.length; i++)
{
while(!sentinel.equalsIgnoreCase("x"))
{
name[i]=sentinel;
//Question 1
System.out.print(" 1.Another name for Class variable is Static variable.");
System.out.println(" (a) True");
System.out.println("(b) False");
System.out.print("Enter Your Choice: ");
answer[i][0]=scan.next();
if(!(answer[i][0].equalsIgnoreCase(correctAnswers[0])))
System.out.println(" Sorry! Correct answer is " + correctAnswers[0]);
else
{
System.out.println(" Correct!");
fw.println(" 1.Another name for Class variable is Static variable. ");
fw.println(" True Another name for Class variable is Static variable. ");
score+=1;
}
//Question 2
System.out.println(" 2.When passing an argument to a method, Java will automatically perform a narrowing conversion if necessary.");
System.out.println("(a) True");
System.out.println("(b) False");
System.out.print("Enter Your Choice: ");
answer[i][1]=scan.next();
if(!(answer[i][1].equalsIgnoreCase(correctAnswers[1])))
System.out.println(" Sorry! Correct answer is "+correctAnswers[1]);
else
{
System.out.println(" Correct!");
fw.println(" 2.When passing an argument to a method, Java will automatically perform a narrowing conversion if necessary. ");
fw.println(" False When passing an argument to a method, Java will automatically perform a narrowing conversion if necessary. ");
score+=1;
}
//Question 3
System.out.println(" 3.The contents of a String object cannot be changed.");
System.out.println("(a) True");
System.out.println("(b) False");
System.out.print("Enter Your Choice: ");
answer[i][2]=scan.next();
if(!(answer[i][2].equalsIgnoreCase(correctAnswers[0])))
System.out.println(" Sorry! Correct answer is "+correctAnswers[0]);
else
{
System.out.println(" Correct!");
fw.println(" 3.The contents of a String object cannot be changed. ");
fw.println(" True The contents of a String object cannot be changed. ");
score+=1;
}
//Question 4
System.out.println(" 4.You must have a return statement in a value-returning method.");
System.out.println("(a) True");
System.out.println("(b) False");
System.out.print("Enter Your Choice: ");
answer[i][3]=scan.next();
if(!(answer[i][3].equalsIgnoreCase(correctAnswers[0])))
System.out.println(" Sorry! Correct answer is "+correctAnswers[0]);
else
{
System.out.println(" Correct!");
fw.println(" 4.You must have a return statement in a value-returning method. ");
fw.println(" True You must have a return statement in a value-returning method. ");
score+=1;
}
//Question 5
System.out.println(" 5.You terminate a method header with a semicolon.");
System.out.println("(a) True");
System.out.println("(b) False");
System.out.print("Enter Your Choice: ");
answer[i][4]=scan.next();
if(!(answer[i][4].equalsIgnoreCase(correctAnswers[1])))
System.out.println(" Sorry! Correct answer is "+correctAnswers[1]);
else
{
System.out.println(" Correct!");
fw.println(" 5.You terminate a method header with a semicolon. ");
fw.println(" False You terminate a method header with a semicolon. ");
score+=1;
}
//Question 6
System.out.println(" 6.Each instance of a class has its own set of instance fields.");
System.out.println("(a) True");
System.out.println("(b) False");
System.out.print("Enter Your Choice: ");
answer[i][5]=scan.next();
if(!(answer[i][5].equalsIgnoreCase(correctAnswers[0])))
System.out.println(" Sorry! Correct answer is "+correctAnswers[0]);
else
{
System.out.println(" Correct!");
fw.println(" 6.Each instance of a class has its own set of instance fields. ");
fw.println(" True Each instance of a class has its own set of instance fields. ");
score+=1;
}
//Question 7
System.out.println(" 7.A class may not have more than one constructor.");
System.out.println("(a) True");
System.out.println("(b) False");
System.out.print("Enter Your Choice: ");
answer[i][6]=scan.next();
if(!(answer[i][6].equalsIgnoreCase(correctAnswers[1])))
System.out.println(" Sorry! Correct answer is "+correctAnswers[1]);
else
{
System.out.println(" Correct!");
fw.println(" 7.A class may not have more than one constructor. ");
fw.println(" False A class may not have more than one constructor. ");
score+=1;
}
System.out.println(" Percentage of questions answered correctly is "+ Math.round(((score/7)* 100)*10) / 10 + "%");
resultf.println(" Number of correct answers "+ score+" ");
resultf.println(" Number of questions asked 7");
score=0;
i++;
final long endTime = System.currentTimeMillis();
resultf.println("Total execution time: " + (endTime - startTime)/1000 +" seconds");
if(i==100)
{
System.out.println(" Sorry cannot make more entries. Limit exceeds");
break;
}//if ends
else
{
System.out.println(" Enter x to stop");
sentinel=scan.next();
}
}
}
}
catch(IOException e)
{
e.printStackTrace();
} finally
{
if (fw != null && resultf!=null)
{
fw.close();
resultf.close();
}
}
}
}
------------------------------------------------------
import java.util.Scanner;
public class Quizprogram
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Quiz obj = new Quiz();
obj.takeInputs();
}//end main
}//end class
-------------------------------------------------------
HERE IS MY questionAnswers.txt FILE WITH QUESTIONS AND ANSWERS. SO HOW DO I MAKE MY (QUIZ CODE) GET THESE QUESTIONS AND ANSWERS FROM THIS FILE W/O ME HARD CODING IT INTO MY PROGRAM.
Another name for Class variable is Static variable.
True Another name for Class variable is Static variable.
When passing an argument to a method, Java will automatically perform a narrowing conversion if necessary.
False When passing an argument to a method, Java will automatically perform a narrowing conversion if necessary.
The contents of a String object cannot be changed.
True The contents of a String object cannot be changed.
You must have a return statement in a value-returning method.
True You must have a return statement in a value-returning method.
You terminate a method header with a semicolon.
False You terminate a method header with a semicolon.
Each instance of a class has its own set of instance fields.
True Each instance of a class has its own set of instance fields.
Explanation / Answer
Hello ,
questionAnswers.txt
True Another name for Class variable is Static variable.
False When passing an argument to a method, Java will automatically perform a narrowing conversion if necessary.
True The contents of a String object cannot be changed.
True You must have a return statement in a value-returning method.
False You terminate a method header with a semicolon.
True Each instance of a class has its own set of instance fields
Please find my code
Explanation : -
--------------------------
Code :-
----------------------
Question.java
------------------------------
/**
*
*
* This class is template of One Question
* For each question one object of this class will be created
*
*/
public class Question {
String question = ""; //Question
String answer = "" ; //Stores True/False
}
Quizprogram.java :-
--------------------------------
import java.util.Scanner;
public class Quizprogram
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Quiz obj = new Quiz();
obj.takeInputs();
}//end main
}//end class
Quiz.java :-
--------------------
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Quiz
{
//Declare instance variables
String name[] ;
//List of Questions
ArrayList< Question> questions = new ArrayList<Question>() ;
private static final String FILENAME_QUESTIONS = "questionAnswers.txt";
private static final String FILENAME_RESULTS = "result.txt";
//Counter for Corect answers
private int correctAnswers = 0;
public void takeInputs()
{
BufferedReader br = null;
try {
/** Read Questions from File**/
br = new BufferedReader(new FileReader(FILENAME_QUESTIONS));
//get a line from the file
String line = br.readLine();
//Loop until end of File
while (line != null) {
/*
* Find index of first space of String
* "0..index of first space" is answer True/False
* Remaining Part is Question
*/
int firstSpaceIdx = line.indexOf(" ");
//if(tokens.length == 2){
//Create Question object
Question q = new Question();
q.answer = line.substring(0, firstSpaceIdx);
q.question = line.substring(firstSpaceIdx+1);
//Add Qustion to list
questions.add(q);
//}
//get line line from the file
line = br.readLine();
}
}
catch(Exception e){
}
finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/***** Start Quiz ****/
//get Start Time
final long startTime = System.currentTimeMillis();
//Loop list of Questions
for(int i =0 ; i < questions.size(); i++){
Scanner scan=new Scanner(System.in);
//Ask a Question
System.out.print(" ");
System.out.print(questions.get(i).question);
System.out.println(" (a) True");
System.out.println("(b) False");
System.out.print("Enter Your Choice: ");
String answer = scan.next();
if(!(answer.equalsIgnoreCase(questions.get(i).answer))){
System.out.println(" Sorry! Correct answer is " + questions.get(i).answer);
}
else
{
System.out.println(" Correct!");
correctAnswers++;
}
}
final long endTime = System.currentTimeMillis();
double diff = (double)correctAnswers/(double)questions.size();
double percent = diff*100;
System.out.println(" Percentage of Correct answers is " + percent);
//resultf.println("Total execution time: " + (endTime - startTime)/1000 +" seconds");
try {
PrintWriter out = new PrintWriter(FILENAME_RESULTS);
out.println("number of correct answers "+ correctAnswers);
out.println("number of questions asked "+ questions.size());
out.println("elapsed time (in seconds) "+ ((endTime - startTime)/1000));
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.