Write a Java documented program to perform the role of a quiz maker. The program
ID: 3810492 • Letter: W
Question
Write a Java documented program to perform the role of a quiz maker. The program should work as follows:
Prompt the student to enter their user name and password.
Read a file that contains a list of all students’ information to validate the login credentials.
Start the quiz only when the credentials are correct. After 3 failed attempts, exit the program.
Randomly pick ten questions from the TestBank.txt file. Display one question at a time.
Get the answer then move to the next question. Do not accept answers other than true or false (T or F should be fine too).
The answers should not be case sensitive. When the user is done with the quiz, print out a report (On screen and on a file) with the below information in it:
First name Last name Score Elapsed time User’s answers and the correct answer.
Name the file from step 5 as follows: (userName_COSC_236_Quiz_Date_Time), where: userName is the actual user name of the student who took the quiz. Date_Time is the date and time of the start of the test.
Prompt for another user name and password or done as a user name to exit.
Explanation / Answer
public class Question {
private String text;
private boolean answer;
public Question(String txt, boolean ans){
setText(txt);
setAnswer(ans);
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public boolean isAnswer() {
return answer;
}
public void setAnswer(boolean answer) {
this.answer = answer;
}
}
//=============================================
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Quiz {
public static Scanner sc;
public static List<Question> questions;
public static void main(String[] args) {
questions = new ArrayList<>();
if(login("users.txt")){
loadQuestions("TestBank.txt");
showQuestions();
}
}
public static void loadQuestions(String filename){
try{
Scanner fr = new Scanner(new File(filename));
while(fr.hasNext()){
String[] data = fr.nextLine().split(" ");
Question q = new Question(data[0], Boolean.parseBoolean(data[1]));
questions.add(q);
}
fr.close();
}catch(FileNotFoundException e){
}
}
public static void showQuestions(){
Random rand = new Random();
int start = Math.abs(rand.nextInt())%questions.size();
int count = 0;
int i=start;
int score = 0;
sc = new Scanner(System.in);
while(count < 5){
i = i%questions.size();
Question q = questions.get(i);
System.out.println(q.getText());
System.out.println("Enter Answer(True/False):");
boolean ans = Boolean.parseBoolean(sc.next());
if(q.isAnswer() == ans){
score ++;
}
i++;
count++;
}
System.out.println("Your Score is "+score+" our of "+5+" ");
}
public static boolean login(String filename){
try{
Scanner fr;
sc = new Scanner(System.in);
int retry = 0;
while(retry < 3){
retry++;
System.out.println("Enter User Name :");
String uname = sc.next();
System.out.println("Enter Password :");
String pass = sc.next();
fr = new Scanner(new File(filename));
while(fr.hasNext()){
String[] data = fr.nextLine().split(" ");
if(data[0].equals(uname) && data[1].equals(pass)){
return true;
}
}
fr.close();
System.out.println("Incorrect login! Try again. ");
}
}catch(FileNotFoundException e){
e.printStackTrace();
}
System.out.println("You attempted 3 Incorrect login. ");
return false;
}
}
//=======================================
TestBank.txt
questin1 true
questin2 true
questin3 false
questin4 true
questin5 false
questin6 true
questin7 false
questin8 true
questin9 false
//===========================
users.txt
lok pass
lok 44
un passwd
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.