create an Exam class with the following requirements: one string array of length
ID: 3556943 • Letter: C
Question
create an Exam class with the following requirements:
one string array of length 20 that holds the 20 answers for the exam object created from this class. answers must be a, b, c, d.
one string array property of length 20 that holds the 20 character answers taken from the person taking the exam(the answers will be passed in from the main program). answers must be a,b,c,d.
create a constructor with no inputs that sets the key array to the correct answers.
create a set method that takes in a string for each of the student's answers, and checks for the answer to be a,b,c,d(if not, please throw an illegalargumentexception)
create a calculation method that tells the user the number of questions that they got correct as integer.
create a calculation method that tells the user the score(number correct/ the number of questions) as a floating point type(%).
please create a main program that demonstrates the Exam class with the following requirements:
create an Exam object.
take the answers from the keyboard and try to add them to the answer array in the object, properly handling any possible exceptions that may be thrown.
if the user generates nayexceptions, they are allowed to attempt the question again until they get all answers into the Exam.
display the results both as the number of questions answered correctly, as well as their score.
Explanation / Answer
Program Code:
import java.util.Scanner;
public class ExamTest
{
public static void main(String args[])
{
String[] choice=new String[20];
boolean[] valid=new boolean[20];
String str="";
Scanner sc=new Scanner(System.in);
Exam ex=new Exam();
System.out.println("Enter answers:");
do
{
for(int i=0;i<20;i++)
{
str=sc.next();
choice[i]=str;
if(i==20)
break;
}
}
while(str=="a" ||str=="b"||str=="c"||str=="d");
ex.set(choice);
int correct=ex.calculation();
System.out.println("Number of correct answers="+correct);
float score=ex.score(correct);
System.out.println("Total Score="+score);
}
}
class Exam
{
String[] key=new String[20];
String[] answer=new String[20];
String nw;
Exam()
{
key[0]="a";
key[1]="b";
key[2]="a";
key[3]="a";
key[4]="d";
key[5]="c";
key[6]="b";
key[7]="b";
key[8]="b";
key[9]="c";
key[10]="c";
key[11]="a";
key[12]="a";
key[13]="c";
key[14]="c";
key[15]="d";
key[16]="d";
key[17]="a";
key[18]="c";
key[19]="b";
}
void set(String ans[])
{
for(int i=0;i<20;i++)
{
answer[i]=ans[i];
}
}
int calculation()
{
int correct=0;
for(int i=0;i<20;i++)
{
if(key[i].equals(answer[i]))
correct++;
}
return correct;
}
float score(int correct)
{
return (float) (correct/20.0);
}
}
Sample Output:
Enter answers:
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
Number of correct answers=6
Total Score=0.3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.