Write a Java documented program to perform the role of a quiz maker. The program
ID: 3816845 • 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.
You need to initalize a variable before you can use it Array indexes always starts at 0 Arrays may not be of any type Once created an array size can be modified The subscript identifies which element in the array to access. Array of int 10] would have indexes of 0 through 9 The if statement decides whether a section of code executes or not A boolean expression can result in values other than true or false String is a primitive data type Java is a object-oriented programmin language All computers must have an operating system The JVM (java virtual machine) can run on a very limited number of computers RAM is volatile meaning all data is erased when the computer is shut down The short and float data types are the same size (in bytes) In a method header you can only include 1 exception per "throws" clause A compiler converts high-level instructions into machine code for execution. Variables must be declared outside of the main method A "break" statement must be used after a ll cases in a SW itch selection besides the "default case All elements of an array must be of the same data type The operator is used to declare a variable. A java source code file contains one or more Java classes The print method places a newline character at the end of whatever is being print out If you want to express that x greater than or equal to y the it can expressA y The conditionally executed statement should be on the line after the if condition Arrays allow us to create a collection of like values that are indexed RAM stands for Random Access Memories Char variables can be either upper or lowercase If a source code file has more than one class only one may be public In Java, not every opening brace needs a closing brace. Single line comments in Java begin with RAM stands for Random Access Memory In coding all data types use the same amount of memory storage Pixels are dots of color that make up an image If all of the and taken out of a program i will a lways run the same as w he the and are there are When coding constants are generally written in ALL CAPS Desktop computers always have a seperate monitor The CPU is considered the brain of the computer All computers do the exact same function ROM stands for Read only Memory Super Computers are only used to do calculations Java is the only programming language The term software refers to the physical components of a computerExplanation / Answer
package com.qm;
//UserNames.txt contains date in following format username password firstname lastname
//we use the line number to form a mapping between question and answer in respective file.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Scanner;
public class QuizMaker {
private static int getRandomNumberInRange(int min, int max) {
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
public static void main(String[] args)
{
String userNamesFilePath="C:/xampp/chg/UserNames.txt";
String testBankFilePath="C:/xampp/chg/TestBank.txt";
String testBankAnswersPath="C:/xampp/chg/TestBankAnswers.txt";
BufferedReader br = null;
FileReader fr = null;
HashMap<String,String> uLoginDetails= new HashMap<String,String>();
HashMap<String,String> uFirstName= new HashMap<String,String>();
HashMap<String,String> uLastName= new HashMap<String,String>();
LinkedHashSet<Integer> randomQuestionNumbers = new LinkedHashSet<Integer>();
LinkedHashMap<Integer,String> questions = new LinkedHashMap<Integer,String>();
LinkedHashMap<Integer,String> answers = new LinkedHashMap<Integer,String>();
LinkedHashMap<Integer,String> userEnteredAnswers = new LinkedHashMap<Integer,String>();
int loginAttempt=0;
String username="";
String password="";
Scanner sc= new Scanner(System.in);
int score=0;
//Get all user details from file for validation
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(userNamesFilePath));
while ((sCurrentLine = br.readLine()) != null) {
String[] splitLine = sCurrentLine.split("\s+");
uLoginDetails.put(splitLine[0], splitLine[1]);
uFirstName.put(splitLine[0], splitLine[2]);
uLastName.put(splitLine[0], splitLine[3]);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
while(loginAttempt<4)
{
System.out.println("Enter User Name");
username=sc.nextLine();
System.out.println("Enter Password");
password=sc.nextLine();
if(uLoginDetails.containsKey(username))
{
String pwd=uLoginDetails.get(username);
if(pwd.equals(password))
{
break;
}
else
{
System.out.println("Incorrect Credentials, please try again");
loginAttempt=loginAttempt+1;
}
}
else
{
System.out.println("Incorrect Credentials, please try again");
loginAttempt=loginAttempt+1;
}
}
if(loginAttempt<3)
{
//Generate 10 random numbers in range of 1 to 125
while(randomQuestionNumbers.size()<10)
{
randomQuestionNumbers.add(getRandomNumberInRange(1, 125));
}
//Get 10 questions from file based on random numbers
try {
String sCurrentLine;
int LineCount=0;
br = new BufferedReader(new FileReader(testBankFilePath));
while ((sCurrentLine = br.readLine()) != null) {
LineCount=LineCount+1;
if(randomQuestionNumbers.contains(LineCount))
{
questions.put(LineCount,sCurrentLine);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
//Get 10 Answers from file based on random numbers
try {
String sCurrentLine;
int LineCount=0;
br = new BufferedReader(new FileReader(testBankAnswersPath));
while ((sCurrentLine = br.readLine()) != null) {
LineCount=LineCount+1;
if(randomQuestionNumbers.contains(LineCount))
{
answers.put(LineCount,sCurrentLine);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
long startTime = System.currentTimeMillis();
for(int q:randomQuestionNumbers)
{
int validFormat=0;
System.out.println(questions.get(q));
while(validFormat<1)
{
System.out.println("Enter answer as either T or F");
String answer=sc.nextLine();
if(answer.contentEquals("T") || answer.contentEquals("F") || answer.contentEquals("t") || answer.contentEquals("f"))
{
String ans=answers.get(q);
if(answer.equalsIgnoreCase(ans))
{
score=score+1;
}
userEnteredAnswers.put(q,answer);
validFormat=1;
}
}
}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("Result of the Test");
System.out.println("First Name "+uFirstName.get(username));
System.out.println("Last Name "+uLastName.get(username));
System.out.println("Score "+score);
System.out.println("Elapsed Time in milli seconds"+totalTime);
System.out.println("User Answer"+"-"+"Correct Answer");
for(int q:randomQuestionNumbers)
{
System.out.println(userEnteredAnswers.get(q)+"-"+answers.get(q));
}
//DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//Date date = new Date();
//dateFormat.format(date);
String opFileName="C:/xampp/chg/"+username+"_COSC_QUIZ_"+new SimpleDateFormat("yyyyMMdd_HHmm'.txt'").format(new Date());;
//File file=new File(opFileName);
try (BufferedWriter bw = new BufferedWriter(new FileWriter(opFileName))) {
bw.write("First Name "+uFirstName.get(username));
bw.write("Last Name "+uLastName.get(username));
bw.write("Score "+score);
bw.write("Elapsed Time in milli seconds"+totalTime);
bw.write("Elapsed Time in milli seconds"+totalTime);
bw.write("User Answer"+"-"+"Correct Answer");
for(int q:randomQuestionNumbers)
{
bw.write(userEnteredAnswers.get(q)+"-"+answers.get(q));
}
// no need to close it.
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.