Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. (10 points) Biology Midterm Your Biology professor from last semester has ask

ID: 3722174 • Letter: 1

Question

1. (10 points) Biology Midterm Your Biology professor from last semester has asked you to write a program that grades the solutions for her midterm exam being given to this semester's students. The exam has 15 multiple choice questions. These are the correct answers: 3. E 4. A 9. C 14. A 2. D 5. C 10. D 12. C 13. D Biology standards for passing the Midterm are correctly answering 10 of the 15 questions. An application is used by her students to take the exam. Another application takes each students answers from the first application and enters them into a file in the following format: each line includes the student's 7-digit Cardinal ID number, followed by their answers {A, B, C, D or E to each of the questions. You must open and read this summary file, with all students' answers, in your program Write a class named BiologyMidterm that holds the correct answers to the exam in an array field. A second array holds a count of correct answers for each of the questions. It should also have private fields so that as your class reads the answers from each student, it counts the number of students (1) taking and (2) passing the test. Another private field counts how many questions have been answered by the current student line being processed, reset for successive students. The class also has the following methods: passed method should whether (a Boolean) a student has passed the test taken - method should return the number of students taking the test gets tudentAverage-method should retum the average score for each student, as that student is graded .getQuestionAverage method should return the fraction of students answering each question (expressed as a decimal, 0-1) .totalCorrect method should return the total number of questions each student answers correctlv Demonstrate the BiologyMidterm class in a complete program that accepts the file with the students' ID number and answers as input, as it loops through each student summary, it displays on the Console (separate lines for each student): (1) the ID number, (2) whether the student has passed; (3) the student's average (0-100%); and (4) the number correctly answered. After processing all students, your program should display how many students took the exam and the average for each of the 15 questions.

Explanation / Answer

package January;

import java.io.File;

import java.util.Scanner;

// Class BiologyMidterm definition

public class BiologyMidterm

{

// Instance variables to store data

int numberOfStudent = 0;

String studentId[];

String correctAnswer[];

String studentAnswer[][];

// Default constructor

BiologyMidterm()

{

// Allocates memory to arrays

correctAnswer = new String[15];

studentAnswer = new String[20][15];

studentId = new String[20];

}// End of default constructor

// Method to read data from file

void readData()

{

// File object created for correct answer

File file = new File("CorrectAnswer.txt");

// File object created for students answer

File file1 = new File("StudentAnswer.txt");

// Handles exception

try

{

// Initializes row counter to zero

int r = 0;

// Scanner object created for file and file1

Scanner scanner = new Scanner(file);

Scanner scanner1 = new Scanner(file1);

// Checks for the data availability

while(scanner.hasNext())

{

// Reads the correct answer and stores it in r index position

correctAnswer[r++] = scanner.next();

}//End of while

// Reinitializes the counter

r = 0;

// Checks for the data availability

while(scanner1.hasNext())

{

// Reads the student id and stores it in r index position

studentId[r] = scanner1.next();

// Loops 15 times for student answer

for(int c = 0; c < 15; c++)

// Reads student answer and stores it in r for row and c for column index position

studentAnswer[r][c] = scanner1.next();

// Increase the row counter

r++;

}//End of while

// Close the file

scanner.close();

scanner1.close();

// Assigns number of students

numberOfStudent = r;

}//End of try

// Catch block to handle the exception

catch(Exception e)

{

e.printStackTrace();

}//End of catch

}//End of method

// Method to return number of students

int taken()

{

return numberOfStudent;

}// End of method

// Method to return pass status of each student, receives average as parameter

boolean passed(double avg)

{

// Checks if the average mark is greater than or equals to 50 return true for pass

if(avg >= 50)

return true;

// Otherwise, return false for fail

else

return false;

}// End of method

// Method to return each student average mark, takes number of correct answer as parameter

double getStudentAverage(int totCorrect)

{

// Calculates average

return (totCorrect / .15);

}// End of method

// Method to return each students number of correct answer, takes each student answer

int totalCorrect(String result[])

{

// Initializes correct answer counter to zero

int correctA = 0;

// Loops 15 times for student answer

for(int x = 0; x < 15; x++)

{

// Compares student answers x index position data with correct answer x index position

// If both are same increase the correctA counter by one

if(result[x].compareTo(correctAnswer[x]) == 0)

correctA++;

}// End of for loop

// Returns correct answer counter

return correctA;

}// End of method

// Method to display Student result

void displayResult()

{

// Loops till number of students

for(int x = 0; x < taken(); x++)

{

// Local variable to store average and total correct answer of each student

double avg = 0;

int totCorrect = 0;

// Displays student id

System.out.print(" Student Id: " + studentId[x]);

// Calls the method to calculate total correct answer

totCorrect = totalCorrect(studentAnswer[x]);

// Calls the method to calculate average

avg = getStudentAverage(totCorrect);

// Displays correct answer and average

System.out.printf(" Correct Answer: %d Average: %.2f", totCorrect, avg);

// Checks the pass status and displays it

if(passed(avg))

System.out.print(" Result: Pass");

else

System.out.print(" Result: Fail");

}// End of for loop

}// End of method

// main method definition

public static void main(String[] args)

{

// Creates an object of class BiologyMidterm

BiologyMidterm bm = new BiologyMidterm();

// Calls the method to read correct answer and student id and students answers

bm.readData();

// Calls the method to display student result

bm.displayResult();

}// End of main method

}// End of class

Sample Output:

Student Id: 111 Correct Answer: 13 Average: 86.67 Result: Pass

Student Id: 112 Correct Answer: 15 Average: 100.00 Result: Pass

Student Id: 113 Correct Answer: 11 Average: 73.33 Result: Pass

Student Id: 114 Correct Answer: 6 Average: 40.00 Result: Fail

Student Id: 115 Correct Answer: 10 Average: 66.67 Result: Pass

Student Id: 116 Correct Answer: 8 Average: 53.33 Result: Pass

Student Id: 117 Correct Answer: 9 Average: 60.00 Result: Pass

-----------------------------------------------

CorrectAnswer.txt file contents

B D E A C E B A C D E C D A B

------------------------------------------------

StudentAnswer.txt file contents

111 A D E A C E B A C D E C D A A
112 B D E A C E B A C D E C D A B
113 B A C E C A B A C D E C D A B
114 B A C E C A C A D D D A A A B
115 A D E A C A B A C D D A A A B
116 A D E A C A B A C A D A C A A
117 A D C A A E B C C D E C D C A