Link to original problem: https://www.chegg.com/homework-help/Starting-out-with-
ID: 3815501 • Letter: L
Question
Link to original problem: https://www.chegg.com/homework-help/Starting-out-with-Visual-C-2012-with-CD-Rom--3rd-edition-chapter-7-problem-4PP-solution-9780133129458
The local driver's license office has asked you to create an application that grades the written portion of the driver's license exam. The exam has 20 multiple-choice questions. Here are the correct answers: Your program should store these correct answers in an array. The program should read the student's answers for each of the 20 questions from a text file and store the answers in another array. (Create your own text file to test the application.) After the student's answers have been read from the file, the program should display a message indicating whether the student passed or failed the exam. (A student must correctly answer 15 of the 20 questions to pass the exam.) It should then display the total number of correctly answered questions, the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions.Explanation / Answer
import java.io.*;
import java.util.*;
public class ReaFile {
public static void main(String[] args) {
try
{
File f1=new File("D:\ ead.txt");
/*
*
* Data in read.txt is
* 1.B 2.D 3.A 4.A 5.C 6.A 7.B 8.A 9.C 10.D 11.B 12.C 13.D 14.A 15.D 16.C 17.C 18.B 19.D 20.A
*/
FileInputStream fi1=new FileInputStream(f1);
DataInputStream di1=new DataInputStream(fi1);
/* if answers are defined in program*/
fileRead(di1);
// if answers are defined in another text file
/* File f2=new File("D:\ ead1.txt");
FileInputStream fi2=new FileInputStream(f2);
DataInputStream di2=new DataInputStream(fi2);
fileRead(di1,di2);*/
/*
*
* Data in read1.txt is
* 1.C 2.D 3.A 4.A 5.A 6.A 7.B 8.A 9.B 10.D 11.B 12.D 13.D 14.B 15.D 16.D 17.C 18.B 19.D 20.A
*/
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void fileRead(InputStream is1, InputStream is2) throws IOException {
Scanner sc1 = new Scanner(is1);
Scanner sc2 = new Scanner(is2);
int j=0,k=0,i=0;//j correct and k incorrect
String incorrect=" ";
while (sc1.hasNext()) {
i++;
String str1 = sc1.next();
String str2 = sc2.next();
if (str1.equalsIgnoreCase(str2))
j++;
else
{
k++;
incorrect+=i+",";
}
}
incorrect=incorrect.substring(0,incorrect.length()-1);
sc1.close();
sc2.close();
if(j>=15)
System.out.println("you are PASSED in exams");
else
System.out.println("you are FAILED in exams");
System.out.println("Total No.of Questions Correctly Answered = "+j);
System.out.println("Total No.of Questions Wrongly Answered = "+k);
if(k>0)
System.out.println("Wrongly Answered Questions are = "+incorrect);
}
public static void fileRead(InputStream is1) throws IOException {
Scanner sc1 = new Scanner(is1);
String[] ans = new String[]{"A","B","C","D","D","C","B","A","B","C","D","A","C","D","B","A","D","A","B","C"};
int i=0; //total
int j=0; //correct
int k=0; //incorrect
String incorrect=" ";
while (sc1.hasNext())
{
String s=sc1.next();
int length=s.length();
String str1=s.substring(length-1, length);
String str2=ans[i];
if(str2.equalsIgnoreCase(str1))
{
j++;
}
else
{
incorrect+=(i+1)+",";
k++;
}
i++;
}
incorrect=incorrect.substring(0,incorrect.length()-1);
sc1.close();
if(j>=15)
System.out.println("you are PASSED in exams");
else
System.out.println("you are FAILED in exams");
System.out.println("Total No.of Questions Correctly Answered = "+j);
System.out.println("Total No.of Questions Wrongly Answered = "+k);
if(k>0)
System.out.println("Wrongly Answered Questions are = "+incorrect);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.