My prompt is to: Create a java application that grades a multiple choice quiz. T
ID: 3691964 • Letter: M
Question
My prompt is to:
Create a java application that grades a multiple choice quiz.
The application will do the following:
Prompt the user to enter the number of questions for the quiz, then prompt the user to enter the answer key for each question. Use an array.
Create an array of Students that will contain the student’s name and grade on the quiz.
From a file, read each student’s name and quiz answers.
By comparing the entered answers to the answer key, the program can determine if the answer is correct or incorrect. Count the correct answers and store the final score for each student.
Display a report of students' grades, the highest grade, the lowest grade, and the average score for the class.
This is what I already have done:
import java.util.Scanner;
import java.text.NumberFormat;
public class Quizzes
{
public static void main(String[] args)
{
int numQuestions;
int numCorrect;
String anotherQuiz;
int answer;
NumberFormat percent = NumberFormat.getPercentInstance();
Scanner scan = new Scanner(System.in);
System.out.println("Quiz Grading");
System.out.println();
do
{
System.out.print("Enter the number of questions on the quiz: ");
numQuestions = scan.nextInt();
int[] key = new int[numQuestions];
System.out.print("Enter the answer key: ");
for (int i = 0; i < numQuestions; i++)
{
System.out
.print("Enter the key for Question " + (i + 1) + ": ");
key[i] = scan.nextInt();
}
System.out.print("Enter the student answers: ");
numCorrect = 0;
for (int i = 0; i < numQuestions; i++)
{
System.out.print("Enter the Answer for Question " + (i + 1)
+ ": ");
answer = scan.nextInt();
if (answer == key[i])
{
numCorrect++;
}
}
System.out.println("Number of Correct Answers :" + numCorrect);
System.out.printf("Percent correct :%.2f ",
((float) numCorrect / (float) numQuestions) * 100.0);
System.out.print("Grade another quiz? (y/n):");
anotherQuiz = scan.next();
if (anotherQuiz.equals("n"))
break;
}
while (true);
}
}
import java.util.Scanner;
public class ReverseArray
{
public static void main(String[] args)
{
int numElements;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
numElements = scan.nextInt();
int[] a = new int[numElements];
System.out.println("Enter the array elements (integers)...");
for (int i = 0; i < numElements; i++)
{
System.out.print("Enter element " + (i + 1) + ": ");
a[i] = scan.nextInt();
}
System.out.println();
System.out.println("The array elements before reversing:");
for (int i = 0; i < numElements; i++)
System.out.print(a[i] + " ");
System.out.println();
for (int i = 0; i < numElements / 2; i++)
{
int temp = a[i];
a[i] = a[numElements - 1 - i];
a[numElements - 1 - i] = temp;
}
System.out.println(" The array after reversing: ");
for (int i = 0; i < numElements; i++)
System.out.print(a[i] + " ");
System.out.println();
}
}
My question is, using my current program how do I:
1.) Ask how many students are in the class and input their names
2.) For the program to output the quiz grade for that student
3.) And display it in a table from highest to lowest grade and the average of the class
Any help would be greatly appreciated! Thanks you!
Explanation / Answer
import java.io.*;
import java.util.Scanner;
/*
1) The question does not specify anything about number of students. So you can either prompt the user to enter or just declare an array of 10000 and assume that class does not have more students than that. The user does not input the names of the students, you have to read them from a file. Prompt the user to enter the filename or you can read from a file which you created. You can choose any file format you want. I would prefer using the format for each line as: Name ans1 ans2 ans3... etc. Each line has the info for a single student. This makes it easier to read.
2) You need to store the number of correct answers for each student separately. So it also should be stored in an array. So declare another int array to store this.
3) There is no requirement to display the grades in a decreasing order. You need to display all of them, highest,lowest and average of the class.
Let me know if you need the code for this.
*/
//Sample input is given below. Copy it in to a file. The input has name followed by answers for five questions.
/*
student1 b d d b d
student2 a d d b d
student3 b b a d d
student4 b b a b a
student5 b a a d b
student6 d d d b a
student7 d a a d b
student8 d b d b d
student9 b d d a b
student10 d b d d b
student11 d b d a a
student12 d d b a d
student13 d b d a b
student14 b d b b d
student15 d b b d d
student16 d b b a b
student17 d d d b d
student18 b a d b d
student19 d b a b d
student20 d d b b d
student21 d d a b d
student22 d d d d b
student23 d d a d d
student24 d d d d b
student25 a d a a d
student26 a d b b a
student27 d d b b a
student28 d a d d d
student29 b d d d d
student30 d b d d b
student31 d a d a d
student32 b d a d a
student33 a d d a b
student34 b a d b b
student35 a d d d a
student36 a d d b d
student37 d a d b b
student38 b d d b d
student39 b d d b a
student40 d d a d a
student41 d d b b d
student42 d d d a d
student43 d a a d b
student44 a d b d d
student45 a b b b d
student46 b d d d d
student47 a d d d d
student48 a d d a d
student49 b a d d d
student50 b d d d b
student51 d a b d d
student52 d d a d a
student53 d d d d d
student54 a a a d d
student55 d d d d a
student56 d d d a d
student57 b d d a a
student58 d d d a b
student59 d d b d b
student60 d a b a d
student61 d a b b b
student62 b d d b b
student63 b d b b d
student64 b b b a a
student65 b b b d d
student66 a b d b b
student67 a d d d d
student68 d d d b a
student69 b a b b d
student70 d a b d a
student71 d d b b d
student72 a a d d d
student73 b d b d d
student74 d d a b b
student75 d b d b a
student76 d b a d d
student77 d d d d b
student78 d a a a b
student79 d d d a b
student80 d b d d d
student81 a d d d b
student82 b b d d b
student83 d d d b b
student84 d d d a d
student85 a d d a a
student86 a b d d d
student87 d d d a b
student88 d b a a b
student89 d d a a d
student90 d d a b b
student91 d d d b b
student92 d d a a b
student93 d b d a d
student94 d b d d d
student95 d d d d d
student96 a b d d d
student97 d d d d a
student98 d d b d b
student99 d d d d d
student100 a a a d a
*/
public class StudentScore
{
public static void main(String[] args)
{
int numques=10;
Scanner scan = new Scanner(System.in);
System.out.print("Please enter the number of questions?");
numques = scan.nextInt();
String[] key = new String[numques];
for(int i=0;i<numques;i++)
{
int j=i+1;
System.out.print("What is the key for question number "+j+"?");
key[i] = scan.next();
}
int numstu=10;
System.out.print("Please enter the number of students?");
numstu = scan.nextInt();
int[] sans = new int[numstu];
String[] sname = new String[numstu];
System.out.print("Please enter the complete file path for students answers:"); // example C:\Users\... etc
String file = scan.next();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
String[] a=new String[30];
int kl=0;
while ((line = br.readLine()) != null) {
// process the line.
a = line.split(" ");
sname[kl]=a[0];
sans[kl]=0;
for(int ik=0;ik<numques;ik++)
{
if(key[ik].equals(a[ik+1]))
{
sans[kl]++;
}
}
kl++;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Name and score for each student:");
System.out.println("=================================");
int max=0,min=0;
double sum=0;
for(int i=0;i<numstu;i++)
{
if(sans[i]>sans[max])max=i;
if(sans[i]<sans[min])min=i;
sum+=sans[i];
System.out.print(sname[i]+' ');
System.out.println(sans[i]);
}
System.out.print("The maximum score is ");
System.out.print(sans[max]);
System.out.println(" for student "+sname[max]);
System.out.print("The minimum score is ");
System.out.print(sans[min]);
System.out.println(" for student "+sname[min]);
System.out.print("The average score is ");
System.out.println(sum/numstu);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.