Use map and sets only package onlineTest; public interface Manager { /** * Adds
ID: 3760682 • Letter: U
Question
Use map and sets only
package onlineTest;
public interface Manager {
/**
* Adds the specified exam to the database.
* @param examId
* @param title
* @return false is exam already exists.
*/
public boolean addExam(int examId, String title);
/**
* Adds a true and false question to the specified exam. If the question
* already exists it is overwritten.
* @param examId
* @param questionNumber
* @param text Question text
* @param points total points
* @param answer expected answer
*/
public void addTrueFalseQuestion(int examId, int questionNumber,
String text, double points, boolean answer);
/**
* Adds a multiple choice question to the specified exam. If the question
* already exists it is overwritten.
* @param examId
* @param questionNumber
* @param text Question text
* @param points total points
* @param answer expected answer
*/
public void addMultipleChoiceQuestion(int examId, int questionNumber,
String text, double points, String[] answer);
/**
* Adds a fill-in-the-blanks question to the specified exam. If the question
* already exits it is overwritten. Each correct response is worth
* points/entries in the answer.
* @param examId
* @param questionNumber
* @param text Question text
* @param points total points
* @param answer expected answer
*/
public void addFillInTheBlanksQuestion(int examId, int questionNumber,
String text, double points, String[] answer);
/**
* Returns a string with the following information per question:<br />
* "Question Text: " followed by the question's text<br />
* "Points: " followed by the points for the question<br />
* "Correct Answer: " followed by the correct answer. <br />
* The format for the correct answer will be: <br />
* a. True or false question: "True" or "False"<br />
* b. Multiple choice question: [ ] enclosing the answer (each entry separated by commas) and in
* sorted order. <br />
* c. Fill in the blanks question: [ ] enclosing the answer (each entry separated by commas) and
* in sorted order. <br />
* @param examId
* @return "Exam not found" if exam not found, otherwise the key
*/
public String getKey(int examId);
/**
* Adds the specified student to the database. Names are specified in the format
* LastName,FirstName
* @param name
* @return false if student already exists.
*/
public boolean addStudent(String name);
/**
* Enters the question's answer into the database.
* @param studentName
* @param examId
* @param questionNumber
* @param answer
*/
public void answerTrueFalseQuestion(String studentName, int examId, int questionNumber, boolean answer);
/**
* Enters the question's answer into the database.
* @param studentName
* @param examId
* @param questionNumber
* @param answer
*/
public void answerMultipleChoiceQuestion(String studentName, int examId, int questionNumber, String[] answer);
/**
* Enters the question's answer into the database.
* @param studentName
* @param examId
* @param questionNumber
* @param answer
*/
public void answerFillInTheBlanksQuestion(String studentName, int examId, int questionNumber, String[] answer);
/**
* Returns the score the student got for the specified exam.
* @param studentName
* @param examId
* @return score
*/
public double getExamScore(String studentName, int examId);
/**
* Generates a grading report for the specified exam. The report will include
* the following information for each exam question:<br />
* "Question #" {questionNumber} {questionScore} " points out of " {totalQuestionPoints}<br />
* The report will end with the following information:<br />
* "Final Score: " {score} " out of " {totalExamPoints};
* @param studentName
* @param examId
* @return report
*/
public String getGradingReport(String studentName, int examId);
/**
* Sets the cutoffs for letter grades. For example, a typical curve we will have
* new String[]{"A","B","C","D","F"}, new double[] {90,80,70,60,0}. Anyone with a 90 or
* above gets an A, anyone with an 80 or above gets a B, etc. Notice we can have different
* letter grades and cutoffs (not just the typical curve).
* @param letterGrades
* @param cutoffs
*/
public void setLetterGradesCutoffs(String[] letterGrades, double[] cutoffs);
/**
* Computes a numeric grade (value between 0 and a 100) for the student taking
* into consideration all the exams. All exams have the same weight.
* @param studentName
* @return grade
*/
public double getCourseNumericGrade(String studentName);
/**
* Computes a letter grade based on cutoffs provided. It is assumed the cutoffs have
* been set before the method is called.
* @param studentName
* @return letter grade
*/
public String getCourseLetterGrade(String studentName);
/**
* Returns a listing with the grades for each student. For each student the report will
* include the following information: <br />
* {studentName} {courseNumericGrade} {courseLetterGrade}<br />
* The names will appear in sorted order.
* @return grades
*/
public String getCourseGrades();
/**
* Returns the maximum score (among all the students) for the specified exam.
* @param examId
* @return maximum
*/
public double getMaxScore(int examId);
/**
* Returns the minimum score (among all the students) for the specified exam.
* @param examId
* @return minimum
*/
public double getMinScore(int examId);
/**
* Returns the average score for the specified exam.
* @param examId
* @return average
*/
public double getAverageScore(int examId);
/**
* It will serialize the Manager object and store it in the
* specified file.
*/
public void saveManager(Manager manager, String fileName);
/**
* It will return a Manager object based on the serialized data
* found in the specified file.
*/
public Manager restoreManager(String fileName);
}
package tests;
import static org.junit.Assert.*;
import java.util.ArrayList;
import onlineTest.SystemManager;
import org.junit.Test;
public class PublicTests {
@Test
public void testTrueFalse() {
StringBuffer answer = new StringBuffer();
SystemManager manager = new SystemManager();
manager.addExam(10, "Midterm");
manager.addTrueFalseQuestion(10, 1, "Abstract classes cannot have constructors.", 2, false);
manager.addTrueFalseQuestion(10, 2, "The equals method returns a boolean.", 4, true);
manager.addTrueFalseQuestion(10, 3, "Identifiers can start with numbers.", 3, false);
answer.append(manager.getKey(10));
String studentName = "Smith,John";
manager.addStudent(studentName);
manager.answerTrueFalseQuestion(studentName, 10, 1, false);
manager.answerTrueFalseQuestion(studentName, 10, 2, true);
manager.answerTrueFalseQuestion(studentName, 10, 3, false);
answer.append("Exam score for " + studentName + " " + manager.getExamScore(studentName, 10));
studentName = "Peterson,Rose";
manager.addStudent(studentName);
manager.answerTrueFalseQuestion(studentName, 10, 1, false);
manager.answerTrueFalseQuestion(studentName, 10, 2, false);
manager.answerTrueFalseQuestion(studentName, 10, 3, false);
answer.append(" Exam score for " + studentName + " " + manager.getExamScore(studentName, 10));
studentName = "Sanders,Linda";
manager.addStudent(studentName);
manager.answerTrueFalseQuestion(studentName, 10, 1, true);
manager.answerTrueFalseQuestion(studentName, 10, 2, false);
manager.answerTrueFalseQuestion(studentName, 10, 3, true);
answer.append(" Exam score for " + studentName + " " + manager.getExamScore(studentName, 10));
assertTrue(TestingSupport.correctResults("pubTestTrueFalse.txt", answer.toString()));
}
@Test
public void testReport() {
StringBuffer answer = new StringBuffer();
SystemManager manager = new SystemManager();
manager.addExam(10, "Midterm");
manager.addTrueFalseQuestion(10, 1, "Abstract classes cannot have constructors.", 2, false);
manager.addTrueFalseQuestion(10, 2, "The equals method returns a boolean.", 4, true);
manager.addTrueFalseQuestion(10, 3, "Identifiers can start with numbers.", 3, false);
String studentName = "Peterson,Rose";
manager.addStudent(studentName);
manager.answerTrueFalseQuestion(studentName, 10, 1, false);
manager.answerTrueFalseQuestion(studentName, 10, 2, false);
manager.answerTrueFalseQuestion(studentName, 10, 3, false);
answer.append(manager.getGradingReport(studentName, 10));
assertTrue(TestingSupport.correctResults("pubTestReport.txt", answer.toString()));
}
@Test
public void testMultipleChoiceKey() {
StringBuffer answer = new StringBuffer();
SystemManager manager = new SystemManager();
manager.addExam(10, "Midterm");
double points;
String questionText = "Which of the following are valid ids? ";
questionText += "A: house B: 674 C: age D: &";
points = 3;
manager.addMultipleChoiceQuestion(10, 1, questionText, points, new String[]{"A","C"});
questionText = "Which of the following methods belong to the Object class? ";
questionText += "A: equals B: hashCode C: separate D: divide ";
points = 2;
manager.addMultipleChoiceQuestion(10, 2, questionText, points, new String[]{"A", "B"});
questionText = "Which of the following allow us to define a constant? ";
questionText += "A: abstract B: equals C: class D: final ";
points = 4;
manager.addMultipleChoiceQuestion(10, 3, questionText, points, new String[]{"D"});
answer.append(manager.getKey(10));
assertTrue(TestingSupport.correctResults("pubTestMultipleChoiceKey.txt", answer.toString()));
}
@Test
public void testMultipleChoice() {
StringBuffer answer = new StringBuffer();
SystemManager manager = new SystemManager();
manager.addExam(10, "Midterm");
double points;
String questionText = "Which of the following are valid ids? ";
questionText += "A: house B: 674 C: age D: &";
points = 3;
manager.addMultipleChoiceQuestion(10, 1, questionText, points, new String[]{"A","C"});
questionText = "Which of the following methods belong to the Object class? ";
questionText += "A: equals B: hashCode C: separate D: divide ";
points = 2;
manager.addMultipleChoiceQuestion(10, 2, questionText, points, new String[]{"A","B"});
questionText = "Which of the following allow us to define a constant? ";
questionText += "A: abstract B: equals C: class D: final ";
points = 4;
manager.addMultipleChoiceQuestion(10, 3, questionText, points, new String[]{"D"});
String studentName = "Peterson,Rose";
manager.addStudent(studentName);
manager.answerMultipleChoiceQuestion(studentName, 10, 1, new String[]{"A", "C"});
manager.answerMultipleChoiceQuestion(studentName, 10, 2, new String[]{"A", "B"});
manager.answerMultipleChoiceQuestion(studentName, 10, 3, new String[]{"D"});
answer.append("Report for " + studentName + " " + manager.getGradingReport(studentName, 10));
studentName = "Sanders,Mike";
manager.addStudent(studentName);
manager.answerMultipleChoiceQuestion(studentName, 10, 1, new String[]{"A"});
manager.answerMultipleChoiceQuestion(studentName, 10, 2, new String[]{"A", "B"});
manager.answerMultipleChoiceQuestion(studentName, 10, 3, new String[]{"D"});
answer.append(" Report for " + studentName + " " + manager.getGradingReport(studentName, 10));
assertTrue(TestingSupport.correctResults("pubTestMultipleChoice.txt", answer.toString()));
}
@Test
public void testFillInTheBlanksKey() {
StringBuffer answer = new StringBuffer();
SystemManager manager = new SystemManager();
manager.addExam(10, "Midterm");
double points;
String questionText = "Name two types of initialization blocks.";
points = 4;
manager.addFillInTheBlanksQuestion(10, 1, questionText, points, new String[]{"static","non-static"});
questionText = "Name the first three types of primitive types discussed in class.";
points = 6;
manager.addFillInTheBlanksQuestion(10, 2, questionText, points, new String[]{"int","double","boolean"});
answer.append(manager.getKey(10));
assertTrue(TestingSupport.correctResults("pubTestFillInTheBlanksKey.txt", answer.toString()));
}
@Test
public void testFillInTheBlanks() {
StringBuffer answer = new StringBuffer();
SystemManager manager = new SystemManager();
manager.addExam(10, "Midterm");
double points;
String questionText = "Name two types of initialization blocks.";
points = 4;
manager.addFillInTheBlanksQuestion(10, 1, questionText, points, new String[]{"static","non-static"});
questionText = "Name the first three types of primitive types discussed in class.";
points = 6;
manager.addFillInTheBlanksQuestion(10, 2, questionText, points, new String[]{"int","double","boolean"});
String studentName = "Peterson,Rose";
manager.addStudent(studentName);
manager.answerFillInTheBlanksQuestion(studentName, 10, 1, new String[]{"static", "non-static"});
manager.answerFillInTheBlanksQuestion(studentName, 10, 2, new String[]{"int", "double", "boolean"});
answer.append("Report for " + studentName + " " + manager.getGradingReport(studentName, 10));
studentName = "Sanders,Laura";
manager.addStudent(studentName);
manager.answerFillInTheBlanksQuestion(studentName, 10, 1, new String[]{"static"});
manager.answerFillInTheBlanksQuestion(studentName, 10, 2, new String[]{"int", "boolean"});
answer.append(" Report for " + studentName + " " + manager.getGradingReport(studentName, 10));
assertTrue(TestingSupport.correctResults("pubTestFillInTheBlanks.txt", answer.toString()));
}
@Test
public void testCourseNumericLetterGrade() {
StringBuffer answer = new StringBuffer();
SystemManager manager = new SystemManager();
double points;
/* First Exam */
manager.addExam(1, "Midterm #1");
manager.addTrueFalseQuestion(1, 1, "Abstract classes cannot have constructors.", 10, false);
manager.addTrueFalseQuestion(1, 2, "The equals method returns a boolean.", 20, true);
String questionText = "Which of the following are valid ids? ";
questionText += "A: house B: 674 C: age D: &";
points = 40;
manager.addMultipleChoiceQuestion(1, 3, questionText, points, new String[]{"A","C"});
questionText = "Name the first three types of primitive types discussed in class.";
points = 30;
manager.addFillInTheBlanksQuestion(1, 4, questionText, points, new String[]{"int","double","boolean"});
String studentName = "Peterson,Laura";
manager.addStudent(studentName);
manager.answerTrueFalseQuestion(studentName, 1, 1, false);
manager.answerTrueFalseQuestion(studentName, 1, 2, false);
manager.answerMultipleChoiceQuestion(studentName, 1, 3, new String[]{"A", "C"});
manager.answerFillInTheBlanksQuestion(studentName, 1, 4, new String[]{"int", "double"});
/* Second Exam */
manager.addExam(2, "Midterm #2");
manager.addTrueFalseQuestion(2, 1, "A break statement terminates a loop.", 10, true);
manager.addTrueFalseQuestion(2, 2, "A class can implement multiple interfaces.", 50, true);
manager.addTrueFalseQuestion(2, 3, "A class can extend multiple classes.", 40, false);
manager.answerTrueFalseQuestion(studentName, 2, 1, true);
manager.answerTrueFalseQuestion(studentName, 2, 2, false);
manager.answerTrueFalseQuestion(studentName, 2, 3, false);
answer.append("Numeric grade for " + studentName + " " + manager.getCourseNumericGrade(studentName));
answer.append(" Exam #1 Score" + " " + manager.getExamScore(studentName, 1));
answer.append(" " + manager.getGradingReport(studentName, 1));
answer.append(" Exam #2 Score" + " " + manager.getExamScore(studentName, 2));
answer.append(" " + manager.getGradingReport(studentName, 2));
manager.setLetterGradesCutoffs(new String[]{"A","B","C","D","F"},
new double[] {90,80,70,60,0});
answer.append(" Course letter grade: " + manager.getCourseLetterGrade(studentName));
assertTrue(TestingSupport.correctResults("pubTestCourseNumericLetterGrade.txt", answer.toString()));
}
@Test
public void testGetCourseGrades() {
StringBuffer answer = new StringBuffer();
SystemManager manager = new SystemManager();
manager.addExam(1, "Midterm #1");
manager.addTrueFalseQuestion(1, 1, "Abstract classes cannot have constructors.", 35, false);
manager.addTrueFalseQuestion(1, 2, "The equals method returns a boolean.", 15, true);
manager.addTrueFalseQuestion(1, 3, "The hashCode method returns an int", 50, true);
String studentName = "Peterson,Laura";
manager.addStudent(studentName);
manager.answerTrueFalseQuestion(studentName, 1, 1, true);
manager.answerTrueFalseQuestion(studentName, 1, 2, true);
manager.answerTrueFalseQuestion(studentName, 1, 3, true);
studentName = "Cortes,Laura";
manager.addStudent(studentName);
manager.answerTrueFalseQuestion(studentName, 1, 1, false);
manager.answerTrueFalseQuestion(studentName, 1, 2, true);
manager.answerTrueFalseQuestion(studentName, 1, 3, true);
studentName = "Sanders,Tom";
manager.addStudent(studentName);
manager.answerTrueFalseQuestion(studentName, 1, 1, true);
manager.answerTrueFalseQuestion(studentName, 1, 2, false);
manager.answerTrueFalseQuestion(studentName, 1, 3, false);
manager.setLetterGradesCutoffs(new String[]{"A","B","C","D","F"},
new double[] {90,80,70,60,0});
answer.append(manager.getCourseGrades());
assertTrue(TestingSupport.correctResults("pubGetCourseGrades.txt", answer.toString()));
}
@Test
public void testMaxMinAverageScoreInExam() {
StringBuffer answer = new StringBuffer();
SystemManager manager = new SystemManager();
manager.addExam(1, "Midterm #1");
manager.addTrueFalseQuestion(1, 1, "Abstract classes cannot have constructors.", 35, false);
manager.addTrueFalseQuestion(1, 2, "The equals method returns a boolean.", 15, true);
manager.addTrueFalseQuestion(1, 3, "The hashCode method returns an int", 50, true);
String studentName = "Peterson,Laura";
manager.addStudent(studentName);
manager.answerTrueFalseQuestion(studentName, 1, 1, true);
manager.answerTrueFalseQuestion(studentName, 1, 2, true);
manager.answerTrueFalseQuestion(studentName, 1, 3, true);
studentName = "Cortes,Laura";
manager.addStudent(studentName);
manager.answerTrueFalseQuestion(studentName, 1, 1, false);
manager.answerTrueFalseQuestion(studentName, 1, 2, true);
manager.answerTrueFalseQuestion(studentName, 1, 3, true);
studentName = "Sanders,Tom";
manager.addStudent(studentName);
manager.answerTrueFalseQuestion(studentName, 1, 1, true);
manager.answerTrueFalseQuestion(studentName, 1, 2, false);
manager.answerTrueFalseQuestion(studentName, 1, 3, false);
answer.append("Maximum Score Midterm " + manager.getMaxScore(1) + " ");
answer.append("Minimum Score Midterm " + manager.getMinScore(1) + " ");
answer.append("Average Score Midterm " + manager.getAverageScore(1) + " ");
assertTrue(TestingSupport.correctResults("pubTestMaxMinAverageScoreInExam.txt", answer.toString()));
}
@Test
public void testMultipleExamsStudents() {
StringBuffer answer = new StringBuffer();
SystemManager manager = new SystemManager();
String laura = "Peterson,Laura";
String mike = "Sanders,Mike";
String john = "Costas,John";
/* Adding students */
manager.addStudent(laura);
manager.addStudent(mike);
manager.addStudent(john);
/* First Exam */
int examId = 1;
manager.addExam(examId, "Midterm #1");
manager.addTrueFalseQuestion(examId, 1, "Java methods are examples of procedural abstractions.", 2, true);
manager.addTrueFalseQuestion(examId, 2, "An inner class can only access public variables and methods of the enclosing class.", 2, false);
String questionText = "Which of the following allow us to define an abstract class? ";
questionText += "A: abstract B: equals C: class D: final ";
double points = 4;
manager.addMultipleChoiceQuestion(examId, 3, questionText, points, new String[]{"A"});
questionText = "Name three access specifiers";
points = 6;
manager.addFillInTheBlanksQuestion(examId, 4, questionText, points, new String[]{"public","private","protected"});
/* Answers */
examId = 1;
manager.answerTrueFalseQuestion(laura, examId, 1, true);
manager.answerTrueFalseQuestion(laura, examId, 2, true);
manager.answerMultipleChoiceQuestion(laura, examId, 3, new String[]{"A"});
manager.answerFillInTheBlanksQuestion(laura, examId, 4, new String[]{"private", "public", "protected"});
manager.answerTrueFalseQuestion(mike, examId, 1, true);
manager.answerTrueFalseQuestion(mike, examId, 2, false);
manager.answerMultipleChoiceQuestion(mike, examId, 3, new String[]{"A"});
manager.answerFillInTheBlanksQuestion(mike, examId, 4, new String[]{"private"});
manager.answerTrueFalseQuestion(john, examId, 1, true);
manager.answerTrueFalseQuestion(john, examId, 2, false);
manager.answerMultipleChoiceQuestion(john, examId, 3, new String[]{"A", "B", "C"});
manager.answerFillInTheBlanksQuestion(john, examId, 4, new String[]{"private", "while"});
/* Second Exam */
examId = 2;
manager.addExam(examId, "Midterm #2");
manager.addTrueFalseQuestion(examId, 1, "The Comparable interface specifies a method called compareTo", 2, true);
manager.addTrueFalseQuestion(examId, 2, "The Comparator interface specifies a method called compare", 2, true);
manager.addTrueFalseQuestion(examId, 3, "A static initialization block is executed when each object is created", 4, false);
questionText = "Which of the following allow us to access a super class method? ";
questionText += "A: abstract B: equals C: super D: final ";
points = 8;
manager.addMultipleChoiceQuestion(examId, 4, questionText, points, new String[]{"C"});
questionText = "Which of the following are methods of the Object class? ";
questionText += "A: hashCode B: equals C: super D: final ";
points = 6;
manager.addMultipleChoiceQuestion(examId, 5, questionText, points, new String[]{"A","B"});
/* Answers */
examId = 2;
manager.answerTrueFalseQuestion(laura, examId, 1, true);
manager.answerTrueFalseQuestion(laura, examId, 2, true);
manager.answerTrueFalseQuestion(laura, examId, 3, true);
manager.answerMultipleChoiceQuestion(laura, examId, 4, new String[]{"C"});
manager.answerMultipleChoiceQuestion(laura, examId, 5, new String[]{"A", "C"});
manager.answerTrueFalseQuestion(mike, examId, 1, true);
manager.answerTrueFalseQuestion(mike, examId, 2, true);
manager.answerTrueFalseQuestion(mike, examId, 3, true);
manager.answerMultipleChoiceQuestion(mike, examId, 4, new String[]{"C"});
manager.answerMultipleChoiceQuestion(mike, examId, 5, new String[]{"A", "B"});
manager.answerTrueFalseQuestion(john, examId, 1, false);
manager.answerTrueFalseQuestion(john, examId, 2, true);
manager.answerTrueFalseQuestion(john, examId, 3, false);
manager.answerMultipleChoiceQuestion(john, examId, 4, new String[]{"C"});
manager.answerMultipleChoiceQuestion(john, examId, 5, new String[]{"A", "B"});
/* Third Exam */
examId = 3;
manager.addExam(examId, "Midterm #3");
manager.addTrueFalseQuestion(examId, 1, "There are two types of exceptions: checked and unchecked.", 4, true);
manager.addTrueFalseQuestion(examId, 2, "The traveling salesman problem is an example of an NP problem.", 4, true);
manager.addTrueFalseQuestion(examId, 3, "Array indexing takes O(n) time", 4, false);
questionText = "Name two properties of a good hash function.";
points = 6;
manager.addFillInTheBlanksQuestion(examId, 4, questionText, points, new String[]{"not expensive","distributes values well"});
/* Answers */
examId = 3;
manager.answerTrueFalseQuestion(laura, examId, 1, true);
manager.answerTrueFalseQuestion(laura, examId, 2, true);
manager.answerTrueFalseQuestion(laura, examId, 3, false);
manager.answerFillInTheBlanksQuestion(laura, examId, 4, new String[]{"not expensive", "distributes values well"});
manager.answerTrueFalseQuestion(mike, examId, 1, false);
manager.answerTrueFalseQuestion(mike, examId, 2, true);
manager.answerTrueFalseQuestion(mike, examId, 3, false);
manager.answerFillInTheBlanksQuestion(mike, examId, 4, new String[]{"polynomial", "distributes values well"});
manager.answerTrueFalseQuestion(john, examId, 1, false);
manager.answerTrueFalseQuestion(john, examId, 2, false);
manager.answerTrueFalseQuestion(john, examId, 3, false);
manager.answerFillInTheBlanksQuestion(john, examId, 4, new String[]{"distributes values well"});
ArrayList<String> list = new ArrayList<String>();
list.add(laura);
list.add(mike);
list.add(john);
for (examId = 1; examId <= 3; examId++) {
for (String student : list) {
answer.append("Report for " + student + " Exam # " + examId + " " + manager.getGradingReport(student, examId) + " ");
}
}
for (examId = 1; examId <= 3; examId++) {
answer.append("Minimum for Exam # " + examId + " " + manager.getMinScore(examId) + " ");
answer.append("Maximum for Exam # " + examId + " " + manager.getMaxScore(examId) + " ");
answer.append("Average for Exam # " + examId + " " + (int)manager.getAverageScore(examId) + " ");
}
manager.setLetterGradesCutoffs(new String[]{"A+","A", "B+", "B", "C", "D", "F"}, new double[]{95,90,85,80,70,60,0});
for (String student : list)
answer.append("Letter Grade for " + student + " " + manager.getCourseLetterGrade(student) + " ");
assertTrue(TestingSupport.correctResults("pubTestMultipleExamsStudents.txt", answer.toString()));
}
@Test
public void testSerialization() {
StringBuffer answer = new StringBuffer();
SystemManager manager = new SystemManager();
manager.addExam(10, "Midterm");
manager.addTrueFalseQuestion(10, 1, "Abstract classes cannot have constructors.", 2, false);
manager.addTrueFalseQuestion(10, 2, "The equals method returns a boolean.", 4, true);
manager.addTrueFalseQuestion(10, 3, "Identifiers can start with numbers.", 3, false);
answer.append(manager.getKey(10));
String fileName = "serializedManager.ser";
manager.saveManager(manager, fileName);
SystemManager restoredManager = (SystemManager) manager.restoreManager(fileName);
assertTrue(TestingSupport.correctResults("serializationTest1.txt", restoredManager.getKey(10)));
}
@Test
public void testSerializationTwo() {
StringBuffer answer = new StringBuffer();
SystemManager manager = new SystemManager();
String laura = "Peterson,Laura";
String mike = "Sanders,Mike";
String john = "Costas,John";
/* Adding students */
manager.addStudent(laura);
manager.addStudent(mike);
manager.addStudent(john);
/* First Exam */
int examId = 1;
manager.addExam(examId, "Midterm #1");
manager.addTrueFalseQuestion(examId, 1, "Java methods are examples of procedural abstractions.", 2, true);
manager.addTrueFalseQuestion(examId, 2, "An inner class can only access public variables and methods of the enclosing class.", 2, false);
String questionText = "Which of the following allow us to define an abstract class? ";
questionText += "A: abstract B: equals C: class D: final ";
double points = 4;
manager.addMultipleChoiceQuestion(examId, 3, questionText, points, new String[]{"A"});
questionText = "Name three access specifiers";
points = 6;
manager.addFillInTheBlanksQuestion(examId, 4, questionText, points, new String[]{"public","private","protected"});
/* Answers */
examId = 1;
manager.answerTrueFalseQuestion(laura, examId, 1, true);
manager.answerTrueFalseQuestion(laura, examId, 2, true);
manager.answerMultipleChoiceQuestion(laura, examId, 3, new String[]{"A"});
manager.answerFillInTheBlanksQuestion(laura, examId, 4, new String[]{"private", "public", "protected"});
manager.answerTrueFalseQuestion(mike, examId, 1, true);
manager.answerTrueFalseQuestion(mike, examId, 2, false);
manager.answerMultipleChoiceQuestion(mike, examId, 3, new String[]{"A"});
manager.answerFillInTheBlanksQuestion(mike, examId, 4, new String[]{"private"});
manager.answerTrueFalseQuestion(john, examId, 1, true);
manager.answerTrueFalseQuestion(john, examId, 2, false);
manager.answerMultipleChoiceQuestion(john, examId, 3, new String[]{"A", "B", "C"});
manager.answerFillInTheBlanksQuestion(john, examId, 4, new String[]{"private", "while"});
/* Second Exam */
examId = 2;
manager.addExam(examId, "Midterm #2");
manager.addTrueFalseQuestion(examId, 1, "The Comparable interface specifies a method called compareTo", 2, true);
manager.addTrueFalseQuestion(examId, 2, "The Comparator interface specifies a method called compare", 2, true);
manager.addTrueFalseQuestion(examId, 3, "A static initialization block is executed when each object is created", 4, false);
questionText = "Which of the following allow us to access a super class method? ";
questionText += "A: abstract B: equals C: super D: final ";
points = 8;
manager.addMultipleChoiceQuestion(examId, 4, questionText, points, new String[]{"C"});
questionText = "Which of the following are methods of the Object class? ";
questionText += "A: hashCode B: equals C: super D: final ";
points = 6;
manager.addMultipleChoiceQuestion(examId, 5, questionText, points, new String[]{"A","B"});
/* Answers */
examId = 2;
manager.answerTrueFalseQuestion(laura, examId, 1, true);
manager.answerTrueFalseQuestion(laura, examId, 2, true);
manager.answerTrueFalseQuestion(laura, examId, 3, true);
manager.answerMultipleChoiceQuestion(laura, examId, 4, new String[]{"C"});
manager.answerMultipleChoiceQuestion(laura, examId, 5, new String[]{"A", "C"});
manager.answerTrueFalseQuestion(mike, examId, 1, true);
manager.answerTrueFalseQuestion(mike, examId, 2, true);
manager.answerTrueFalseQuestion(mike, examId, 3, true);
manager.answerMultipleChoiceQuestion(mike, examId, 4, new String[]{"C"});
manager.answerMultipleChoiceQuestion(mike, examId, 5, new String[]{"A", "B"});
manager.answerTrueFalseQuestion(john, examId, 1, false);
manager.answerTrueFalseQuestion(john, examId, 2, true);
manager.answerTrueFalseQuestion(john, examId, 3, false);
manager.answerMultipleChoiceQuestion(john, examId, 4, new String[]{"C"});
manager.answerMultipleChoiceQuestion(john, examId, 5, new String[]{"A", "B"});
/* Third Exam */
examId = 3;
manager.addExam(examId, "Midterm #3");
manager.addTrueFalseQuestion(examId, 1, "There are two types of exceptions: checked and unchecked.", 4, true);
manager.addTrueFalseQuestion(examId, 2, "The traveling salesman problem is an example of an NP problem.", 4, true);
manager.addTrueFalseQuestion(examId, 3, "Array indexing takes O(n) time", 4, false);
questionText = "Name two properties of a good hash function.";
points = 6;
manager.addFillInTheBlanksQuestion(examId, 4, questionText, points, new String[]{"not expensive","distributes values well"});
/* Answers */
examId = 3;
manager.answerTrueFalseQuestion(laura, examId, 1, true);
manager.answerTrueFalseQuestion(laura, examId, 2, true);
manager.answerTrueFalseQuestion(laura, examId, 3, false);
manager.answerFillInTheBlanksQuestion(laura, examId, 4, new String[]{"not expensive", "distributes values well"});
manager.answerTrueFalseQuestion(mike, examId, 1, false);
manager.answerTrueFalseQuestion(mike, examId, 2, true);
manager.answerTrueFalseQuestion(mike, examId, 3, false);
manager.answerFillInTheBlanksQuestion(mike, examId, 4, new String[]{"polynomial", "distributes values well"});
manager.answerTrueFalseQuestion(john, examId, 1, false);
manager.answerTrueFalseQuestion(john, examId, 2, false);
manager.answerTrueFalseQuestion(john, examId, 3, false);
manager.answerFillInTheBlanksQuestion(john, examId, 4, new String[]{"distributes values well"});
String fileName = "serializedManagerTwo.ser";
manager.saveManager(manager, fileName);
SystemManager restoredManager = (SystemManager) manager.restoreManager(fileName);
/* After manager has been restored */
ArrayList<String> list = new ArrayList<String>();
list.add(laura);
list.add(mike);
list.add(john);
for (examId = 1; examId <= 3; examId++) {
for (String student : list) {
answer.append("Report for " + student + " Exam # " + examId + " " + restoredManager.getGradingReport(student, examId) + " ");
}
}
for (examId = 1; examId <= 3; examId++) {
answer.append("Minimum for Exam # " + examId + " " + restoredManager.getMinScore(examId) + " ");
answer.append("Maximum for Exam # " + examId + " " + restoredManager.getMaxScore(examId) + " ");
answer.append("Average for Exam # " + examId + " " + (int)restoredManager.getAverageScore(examId) + " ");
}
restoredManager.setLetterGradesCutoffs(new String[]{"A+","A", "B+", "B", "C", "D", "F"}, new double[]{95,90,85,80,70,60,0});
for (String student : list)
answer.append("Letter Grade for " + student + " " + restoredManager.getCourseLetterGrade(student) + " ");
assertTrue(TestingSupport.correctResults("pubTestMultipleExamsStudents.txt", answer.toString()));
}
}
Explanation / Answer
Answer:
Here in the below code, the Manager.java and PublicTests.java are not provided. The rest of the required code is added.
Program code to copy:
// Question.java
package onlineTest;
public class Question
{
int examID;
int questionNumber;
String question;
double points;
public Question(int examID, int questionNumber, String question, double points)
{
this.examID = examID;
this.questionNumber = questionNumber;
this.question = question;
this.points = points;
}
public String getQuestion()
{
return question;
}
public void setQuestion(String question)
{
this.question = question;
}
public double getPoints()
{
return points;
}
public void setPoints(double points)
{
this.points = points;
}
public int getExamID()
{
return examID;
}
public void setExamID(int examID)
{
this.examID = examID;
}
public int getQuestionNumber()
{
return questionNumber;
}
public void setQuestionNumber(int questionNumber)
{
this.questionNumber = questionNumber;
}
public String toString()
{
String result = "Question Text: "+getQuestion()+" ";
result+="Points: "+getPoints()+" ";
return result;
}
}
// Exam.java
package onlineTest;
import java.util.*;
public class Exam
{
int examID;
String examTitle;
Set<Question> questions;
public Exam()
{
examID = 0;
examTitle = "";
questions = new HashSet<Question>();
}
public Exam(int examID, String examTitle)
{
this.examID = examID;
this.examTitle = examTitle;
questions = new HashSet<Question>();
}
public int getExamID()
{
return examID;
}
public void setExamID(int examID)
{
this.examID = examID;
}
public String getExamTitle()
{
return examTitle;
}
public void setExamTitle(String examTitle)
{
this.examTitle = examTitle;
}
public void addQuestion(Question question)
{
questions.add(question);
}
public Set<Question> getQuestions()
{
return questions;
}
public Question getQuestion(int examId)
{
for (Question question : questions)
{
if (question.getExamID() == examId)
return question;
}
return null;
}
public boolean equals(Exam other)
{
if (this.examID == other.getExamID())
{
if (this.examTitle.equals(other.getExamTitle()))
return true;
else
return false;
}
else
return false;
}
/*
* @Override public int hashCode() { final int prime = 31; int result = 1;
* result = prime * result + examID; result = prime * result + ((examTitle
* == null) ? 0 : examTitle.hashCode()); return result; }
*/
}
// MultipleChoiceQuestion.java
package onlineTest;
public class MultipleChoiceQuestion extends Question
{
String answers[];
public MultipleChoiceQuestion(int examID, int questionNumber, String questionText, double points, String answers[])
{
super(examID, questionNumber, questionText, points);
this.answers = answers;
}
public String[] getAnswers()
{
return answers;
}
public void setAnswers(String[] answers)
{
this.answers = answers;
}
public String toString()
{
String result=super.toString()+"Correct Answer: [";
for(int i = 0; i<answers.length; i++)
{
if(i<answers.length-1)
{
result+= answers[i]+", ";
}
else
{
result+=answers[i]+"] ";
}
}
return result;
}
public double getScoreValue(MultipleChoiceQuestion other)
{
String otheranswers[] = other.getAnswers();
double score =0;
for(int i =0; i<answers.length; i++)
{
if(!otheranswers[i].equals(answers[i]))
{
return 0;
}
}
return this.points;
}
}
// FillInTheBlanksQuestion.java
package onlineTest;
public class FillInTheBlanksQuestion extends Question
{
String answers[];
public String[] getAnswers()
{
return answers;
}
public void setAnswer(String[] answers)
{
this.answers = answers;
}
public FillInTheBlanksQuestion(int examID, int questionNumber, String question, double points, String answers[])
{
super(examID, questionNumber, question, points);
// TODO Auto-generated constructor stub
this.answers = answers;
}
public double getScoreValue(FillInTheBlanksQuestion other)
{
String otheranswers[] = other.getAnswers();
double score =0;
for(int i =0; i<answers.length; i++)
{
if(!otheranswers[i].equals(answers[i]))
{
return 0;
}
}
return this.points;
}
public String toString()
{
String result=super.toString()+"Correct Answer: [";
for(int i = 0; i<answers.length; i++)
{
if(i<answers.length-1)
{
result+= answers[i]+", ";
}
else
{
result+=answers[i]+"] ";
}
}
return result;
}
}
// TrueOrFalseQuestion.java
package onlineTest;
public class TrueOrFalseQuestion extends Question
{
boolean answer;
public TrueOrFalseQuestion(int examID, int questionNumber, String question, double points, boolean answer)
{
super(examID, questionNumber, question, points);
// TODO Auto-generated constructor stub
this.answer = answer;
}
public boolean isAnswer()
{
return answer;
}
public void setAnswer(boolean answer)
{
this.answer = answer;
}
public double getScoreValue(TrueOrFalseQuestion other)
{
if(answer == other.isAnswer())
return this.points;
return 0;
}
public String toString()
{
String result=super.toString()+"Correct Answer: "+isAnswer()+" ";
return result;
}
}
//TestingSupport.java
package tests;
import java.io.*;
import java.util.*;
public class TestingSupport
{
@SuppressWarnings("resource")
public static boolean correctResults(String string, String string2)
{
Scanner input = null;
String result = "";
boolean flag = false;
try
{
input = new Scanner(new File(string));
result += input.nextLine();
while (input.hasNextLine())
{
result += " "+input.nextLine();
}
if (result.equals(string2))
{
flag = true;
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return flag;
}
}
// SystemManager.java
package onlineTest;
import java.util.*;
public class SystemManager implements Manager
{
Question currentQuestion, currentAnswer;
HashMap<Exam, Set<Question>> questionsList;
HashMap<String, Set<Question>> studentAnswers;
String reportResult = "";
double questionScore = 0;
String letterGrades[];
double scoreGrades[];
public SystemManager()
{
questionsList = new HashMap<Exam, Set<Question>>();
currentQuestion = null;
currentAnswer = null;
studentAnswers = new HashMap<String, Set<Question>>();
}
private Exam getExamId(int id)
{
Set<Exam> keys = questionsList.keySet();
for (Exam exam : keys)
{
if (exam.getExamID() == id)
{
return exam;
}
}
return null;
}
@Override
public boolean addExam(int examId, String title)
{
Exam newExam = new Exam(examId, title);
Set<Exam> expectedQuestionsSet = questionsList.keySet();
if (expectedQuestionsSet == null)
{
questionsList.put(newExam, null);
return true;
}
else
{
for (Exam exam : expectedQuestionsSet)
{
if (exam.equals(newExam))
{
return false;
}
}
}
questionsList.put(newExam, null);
return true;
}
@Override
public void addTrueFalseQuestion(int examId, int questionNumber, String text, double points, boolean answer)
{
Exam exam = getExamId(examId);
Set<Question> question = questionsList.get(exam);
if (question == null)
{
question = new LinkedHashSet<Question>();
questionsList.put(exam, question);
}
currentQuestion = new TrueOrFalseQuestion(examId, questionNumber, text, points, answer);
question.add(currentQuestion);
}
@Override
public void addMultipleChoiceQuestion(int examId, int questionNumber, String text, double points, String[] answer)
{
Exam exam = getExamId(examId);
Set<Question> question = questionsList.get(exam);
if (question == null)
{
question = new LinkedHashSet<Question>();
questionsList.put(exam, question);
}
currentQuestion = new MultipleChoiceQuestion(examId, questionNumber, text, points, answer);
question.add(currentQuestion);
// TODO Auto-generated method stub
}
@Override
public void addFillInTheBlanksQuestion(int examId, int questionNumber, String text, double points, String[] answer)
{
Exam exam = getExamId(examId);
Set<Question> question = questionsList.get(exam);
if (question == null)
{
question = new LinkedHashSet<Question>();
questionsList.put(exam, question);
}
currentQuestion = new FillInTheBlanksQuestion(examId, questionNumber, text, points, answer);
question.add(currentQuestion);
// TODO Auto-generated method stub
}
@Override
public String getKey(int examId)
{
/*
* "Question Text: " followed by the question's text<br />
*
* "Points: " followed by the points for the question<br />
*
* "Correct Answer: " followed by the correct answer. <br />
*
* The format for the correct answer will be: <br />
*
* a. True or false question: "True" or "False"<br />
*
* b. Multiple choice question: [ ] enclosing the answer (each entry
* separated by commas) and in
*
* sorted order. <br />
*
* c. Fill in the blanks question: [ ] enclosing the answer (each entry
* separated by commas) and
*/
Exam exam = getExamId(examId);
Set<Question> questions = questionsList.get(exam);
String result = "";
if (questions != null)
{
for (Question quest : questions)
{
if (quest instanceof TrueOrFalseQuestion)
{
result += (TrueOrFalseQuestion) quest;
}
}
return result;
}
return "Exam not found";
}
@Override
public boolean addStudent(String name)
{
// TODO Auto-generated method stub
if (studentAnswers.size() == 0)
{
studentAnswers.put(name, null);
return true;
}
else
{
Set<String> keys = studentAnswers.keySet();
for (String studName : keys)
{
if (name.equals(studName))
return false;
}
studentAnswers.put(name, null);
return true;
}
}
@Override
public void answerTrueFalseQuestion(String studentName, int examId, int questionNumber, boolean answer)
{
Exam exam = getExamId(examId);
Set<Question> questions = questionsList.get(exam);
Question quest = null;
for (Question question : questions)
{
if (question.getQuestionNumber() == questionNumber)
quest = question;
}
currentAnswer = new TrueOrFalseQuestion(examId, questionNumber, quest.getQuestion(), quest.getPoints(), answer);
Set<Question> answers = studentAnswers.get(studentName);
if (answers == null)
{
answers = new LinkedHashSet<Question>();
studentAnswers.put(studentName, answers);
}
answers.add(currentAnswer);
// TODO Auto-generated method stub
}
@Override
public void answerMultipleChoiceQuestion(String studentName, int examId, int questionNumber, String[] answer)
{
Exam exam = getExamId(examId);
Set<Question> questions = questionsList.get(exam);
Question quest = null;
for (Question question : questions)
{
if (question.getQuestionNumber() == questionNumber)
quest = question;
}
currentAnswer = new MultipleChoiceQuestion(examId, questionNumber, quest.getQuestion(), quest.getPoints(), answer);
Set<Question> answers = studentAnswers.get(studentName);
if (answers == null)
{
answers = new LinkedHashSet<Question>();
studentAnswers.put(studentName, answers);
}
answers.add(currentAnswer);
// TODO Auto-generated method stub
}
@Override
public void answerFillInTheBlanksQuestion(String studentName, int examId, int questionNumber, String[] answer)
{
Exam exam = getExamId(examId);
Set<Question> questions = questionsList.get(exam);
Question quest = null;
for (Question question : questions)
{
if (question.getQuestionNumber() == questionNumber)
quest = question;
}
currentAnswer = new FillInTheBlanksQuestion(examId, questionNumber, quest.getQuestion(), quest.getPoints(), answer);
Set<Question> answers = studentAnswers.get(studentName);
if (answers == null)
{
answers = new LinkedHashSet<Question>();
studentAnswers.put(studentName, answers);
}
answers.add(currentAnswer);
// TODO Auto-generated method stub
}
private String getAnswerQuestion(String name)
{
Set<String> keys = studentAnswers.keySet();
for (String names : keys)
{
if (names.equals(name))
{
return names;
}
}
return null;
}
private double getTotalScore(Set<Question> questions, Set<Question> answers)
{
double score = 0;
TrueOrFalseQuestion tfq = null, tqa = null;
MultipleChoiceQuestion mcq = null, mca = null;
FillInTheBlanksQuestion fitbq = null, fitba = null;
reportResult = "";
int questionCount = 1;
double eachScore = 0;
questionScore = getTotalQuestionScore(questions);
for (Question quest : questions)
{
if (quest instanceof TrueOrFalseQuestion)
{
tfq = (TrueOrFalseQuestion) quest;
}
if (quest instanceof MultipleChoiceQuestion)
{
mcq = (MultipleChoiceQuestion) quest;
}
if (quest instanceof FillInTheBlanksQuestion)
{
fitbq = (FillInTheBlanksQuestion) quest;
}
for (Question ans : answers)
{
if (ans instanceof TrueOrFalseQuestion)
{
TrueOrFalseQuestion tfa = (TrueOrFalseQuestion) ans;
if (tfa.getExamID() == tfq.getExamID() && tfa.getQuestionNumber() == tfq.getQuestionNumber())
{
eachScore = tfq.getScoreValue((TrueOrFalseQuestion) ans);
score += eachScore;
reportResult += "Question #"+questionCount+": "+tfq.getPoints()+" points out of "+questionScore+" ";
questionCount++;
}
}
if (ans instanceof MultipleChoiceQuestion)
{
mca = (MultipleChoiceQuestion) ans;
if (mca.getExamID() == mcq.getExamID() && mca.getQuestionNumber() == mcq.getQuestionNumber())
{
eachScore = mcq.getScoreValue((MultipleChoiceQuestion) ans);
score += eachScore;
reportResult += "Question #"+questionCount+": "+tfq.getPoints()+" points out of "+questionScore+" ";
questionCount++;
}
}
if (ans instanceof FillInTheBlanksQuestion)
{
fitba = (FillInTheBlanksQuestion) ans;
if (fitba.getExamID() == fitbq.getExamID()
&& fitba.getQuestionNumber() == fitbq.getQuestionNumber())
{
eachScore= fitbq.getScoreValue((FillInTheBlanksQuestion) ans);
score += eachScore;
reportResult += "Question #"+questionCount+": "+tfq.getPoints()+" points out of "+questionScore+" ";
questionCount++;
}
}
}
}
return score;
}
@Override
public double getExamScore(String studentName, int examId)
{
// TODO Auto-generated method stub
double score = 0;
Exam exam = getExamId(examId);
Set<Question> questions = questionsList.get(exam);
Set<Question> answers = studentAnswers.get(studentName);
score = getTotalScore(questions, answers);
return score;
}
private double getTotalQuestionScore(Set<Question> questions)
{
double score = 0;
TrueOrFalseQuestion tfq = null;
MultipleChoiceQuestion mcq = null;
FillInTheBlanksQuestion fitbq = null;
for (Question quest : questions)
{
if (quest instanceof TrueOrFalseQuestion)
{
tfq = (TrueOrFalseQuestion) quest;
score+=tfq.getPoints();
}
if (quest instanceof MultipleChoiceQuestion)
{
mcq = (MultipleChoiceQuestion) quest;
score+=mcq.getPoints();
}
if (quest instanceof FillInTheBlanksQuestion)
{
fitbq = (FillInTheBlanksQuestion) quest;
score+=fitbq.getPoints();
}
}
return score;
}
@Override
public String getGradingReport(String studentName, int examId)
{
String report="";
double finalScore = 0;
double questionScoreTotal = 0;
Exam exam = getExamId(examId);
String answer = getAnswerQuestion(studentName);
Set<Question> questions = questionsList.get(exam);
Set<Question> answers = studentAnswers.get(studentName);
finalScore = getExamScore(studentName, examId);
report = reportResult;
report+="Final Score: "+finalScore +" out of "+ questionScoreTotal+" ";
return report;
}
@Override
public void setLetterGradesCutoffs(String[] letterGrades, double[] cutoffs)
{
this.letterGrades = letterGrades;
this.scoreGrades= cutoffs;
// TODO Auto-generated method stub
}
@Override
public double getCourseNumericGrade(String studentName)
{
String answer = getAnswerQuestion(studentName);
Set<Question> answers = studentAnswers.get(studentName);
int examId = 0;
for(Question ans: answers)
{
examId = ans.getExamID();
}
double score = getExamScore(studentName, examId) *10;
// TODO Auto-generated method stub
return score;
}
@Override
public String getCourseLetterGrade(String studentName)
{
// TODO Auto-generated method stub
double score = getCourseNumericGrade(studentName);
for(int i =0; i<letterGrades.length; i++)
{
if(score<=scoreGrades[i])
{
return letterGrades[i];
}
}
return null;
}
@Override
public String getCourseGrades()
{
// TODO Auto-generated method stub
return null;
}
@Override
public double getMaxScore(int examId)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public double getMinScore(int examId)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public double getAverageScore(int examId)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public void saveManager(Manager manager, String fileName)
{
// TODO Auto-generated method stub
}
@Override
public Manager restoreManager(String fileName)
{
// TODO Auto-generated method stub
return null;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.