Quiz tester

Quiz tester 

Project: Online Test System (Design)

 

Overview

For this project you will implement the data manager of an online test system. The system allows for the definition of exams with three possible kinds of questions: true and false, multiple choice and fill-in-the-blanks questions. The system will grade submitted exams and generate some statistical information.

Code Distribution

The project’s code distribution is available by checking out the project named OnlineTest. The code distribution provides you with the following:

  • A package named cmdLineInterpreter – Package where classe(s) that implement a simple command line interpreter for the System Manager will reside. We should be able to run your interpreter by running the main method of the Interpreter Additional details about this interpreter are provided below.
  • A package named onlineTest – Package where all the classes representing your design will be implemented.
  • A package named tests – Includes the public tests (java). Notice that you are not required to write student tests for this project; however, we recommend you do so.

 

Specifications

Data Manager

For this project you will implement the data manager (model component of the MVC paradigm) for an online test system. The system allows for the definition of exams with three possible kinds of questions: true and false, multiple choice and fill-in-the-blanks questions. The system will grade submitted exams and generate some statistical information.

The functionality of the system you will implement is represented by the interface Manager. The interface represents the methods available to anyone trying to make use of the system. The javadoc documentation for the project can be found at javadoc. Your task for the design part of the project is to come up with a set of classes and interfaces that together provide the functionality defined by the Manager interface.

Access to the data manager functionality will be possible through a class named SystemManager that you must define. This class implements the Manager interface. Feel free to add any instance variables and private methods to this class. Public tests create an instance of the SystemManager class in order to test your system.

Your design should make use of object-oriented design concepts (e.g., generalization, specification, etc.) discussed in class. Keep in mind that having a large number of classes does not guarantee a better design grade. If you only implement the SystemManager class you will lose all the points associated with the design component of this project. We encourage you to discuss your design with your instructor or TA to make sure your design is a valid one.

 

Requirements

  • An exam can have different types of questions (true/false, fill in the blanks, etc.)
  • We expect to see at least five classes/interfaces (SystemManager and four additional classes/interfaces) in your design. For this project, do not use anonymous inner classes (except for iterators). Notice that classes used for iterators do not count towards the minimum of five classes. In addition, classes associated with the command line interpreter are not included in this count.
  • You should use maps/sets to keep track of your data.
  • Your command line interpreter will provide access to a reduced set of the functionality of your system. You need to define a command line interpreter that allow us to:
    • Add a student
    • Add an exam
    • Add a true/false question
    • Answer a true/false question
    • Get the exam score for a student
  • Your command line interpreter will display a menu with the above options. Your interpreter should rely on standard input/output (not GUI/JavaFX).

 Manager.java

 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);

}

    Solution

     Command.java

   package cmdLineInterpreter;

import java.util.List;

public class Command {

private String action;

private List<String> args;

public Command(String command, List<String> args2) {

this.action = command;

this.args = args2;

}

public String getCommand() {

return action;

}

public List<String> getArgs() {

return args;

}

} 

 CommandManager.java

  package cmdLineInterpreter;

import onlineTest.Manager;

import onlineTest.SystemManager;

public class CommandManager {

Manager manager;

public CommandManager() {

manager = new SystemManager();

}

public void runCommand(Command command) {

}

}

  Interpreter.java

  package cmdLineInterpreter;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

/**

*

* By running the main method of this class we will be able to

* execute commands associated with the SystemManager.  This command

* interpreter is text based.

*

*/

public class Interpreter {

private static CommandManager cmdManager;

private static Scanner kb;

public static void main(String[] args) {

cmdManager = new CommandManager();

kb = new Scanner(System.in);

 

while(true) {

cmdManager.runCommand(parseCommand(kb.nextLine()));

}

}

private static Command parseCommand(String input) {

Scanner cmdScanner = new Scanner(input);

if(!cmdScanner.hasNext()) {

cmdScanner.close();

return null;

}

String command = “”;

List<String> args = new ArrayList<String>();

command = cmdScanner.next();

while(cmdScanner.hasNext()) {

args.add(cmdScanner.next());

}

cmdScanner.close();

return new Command(command, args);

}

} 

FillBlankQuestion.java

 package onlineTest;

import java.util.ArrayList;

import java.util.Collections;

public class FillBlankQuestion extends Question {

protected String[] answer;

public FillBlankQuestion(int questionNumber, String text, double points, String[] answer) {

super(questionNumber, text, points);

this.answer = answer;

}

public int getQuestionNumber() {

return super.getQuestionNumber();

}

public double getPoints() {

return super.getPoints();

}

public ArrayList<String> getAnswer() {

ArrayList<String> temp = new ArrayList<String>();

for (int i = 0; i < answer.length; i++) {

temp.add(answer[i]);

}

return temp;

}

public String toString() {

ArrayList<String> temp = new ArrayList<String>();

for (int i = 0; i < answer.length; i++) {

temp.add(answer[i]);

}

Collections.sort(temp);

return super.toString() + “Correct Answer: ” + temp;

}

public int compareTo(FillBlankQuestion obj) {

if (super.getQuestionNumber() > obj.getQuestionNumber()) {

return 1;

} else if (super.getQuestionNumber() == obj.getQuestionNumber()) {

return 0;

}

return -1;

}

} 

Manager.java

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);

} 

MultipleChoiceQuestion.java

 package onlineTest;

import java.util.ArrayList;

import java.util.Collections;

public class MultipleChoiceQuestion extends Question {

protected String[] answer;

public MultipleChoiceQuestion(int questionNumber, String text, double points, String[] answer) {

super(questionNumber, text, points);

this.answer = answer;

}

public int getQuestionNumber() {

return super.getQuestionNumber();

}

public double getPoints() {

return super.getPoints();

}

public ArrayList<String> getAnswer() {

ArrayList<String> temp = new ArrayList<String>();

for (int i = 0; i < answer.length; i++) {

temp.add(answer[i]);

}

return temp;

}

public String toString() {

ArrayList<String> temp = new ArrayList<String>();

for (int i = 0; i < answer.length; i++) {

temp.add(answer[i]);

}

Collections.sort(temp);

return super.toString() + “Correct Answer: ” + temp;

}

public int compareTo(MultipleChoiceQuestion obj) {

if (super.getQuestionNumber() > obj.getQuestionNumber()) {

return 1;

} else if (super.getQuestionNumber() == obj.getQuestionNumber()) {

return 0;

}

return -1;

}

} 

Question.java

 package onlineTest;

public class Question implements Comparable<Question> {

protected int questionNumber;

protected double points;

protected String text;

public Question(int questionNumber, String text, double points) {

this.questionNumber = questionNumber;

this.text = text;

this.points = points;

}

public int getQuestionNumber() {

return this.questionNumber;

}

public double getPoints() {

return this.points;

}

public String toString() {

String results = “”;

results = “Question Text: ” + this.text + “\n”;

results += “Points: ” + this.points + “\n”;

return results;

}

public int compareTo(Question obj) {

if (this.questionNumber > obj.questionNumber) {

return 1;

} else if (this.questionNumber == obj.questionNumber) {

return 0;

}

return -1;

}

} 

SystemManager.java

 package onlineTest;

import java.util.*;

public class SystemManager implements Manager {

private Map<String, Double> cutoffGrades = new TreeMap<String, Double>();

private Map<String, Map<Integer, ArrayList<Double>>> studentScores = new HashMap<String, Map<Integer, ArrayList<Double>>>();

private Map<Integer, ArrayList<Question>> exams = new HashMap<Integer, ArrayList<Question>>();

private Map<Integer, String> examTitles = new HashMap<Integer, String>();

public boolean addExam(int examId, String title) {

if (!exams.containsKey(examId)) {

examTitles.put(examId, title);

exams.put(examId, new ArrayList<Question>());

return true;

}

return false;

}

public void addTrueFalseQuestion(int examId, int questionNumber, String text, double points, boolean answer) {

if (!exams.containsKey(examId)) {

return;

}

for (int i = 0; i < exams.get(examId).size(); i++) {

if (i == questionNumber) {

exams.get(examId).remove(i);

exams.get(examId).add(new TrueFalse(questionNumber, text, points, answer));

Collections.sort(exams.get(examId));

return;

}

}

exams.get(examId).add(new TrueFalse(questionNumber, text, points, answer));

Collections.sort(exams.get(examId));

}

public void addMultipleChoiceQuestion(int examId, int questionNumber, String text, double points, String[] answer) {

if (!exams.containsKey(examId)) {

return;

}

for (int i = 0; i < exams.get(examId).size(); i++) {

if (i == questionNumber) {

exams.get(examId).remove(i);

exams.get(examId).add(new MultipleChoiceQuestion(questionNumber, text, points, answer));

Collections.sort(exams.get(examId));

return;

}

}

exams.get(examId).add(new MultipleChoiceQuestion(questionNumber, text, points, answer));

Collections.sort(exams.get(examId));

}

public void addFillInTheBlanksQuestion(int examId, int questionNumber, String text, double points,

String[] answer) {

if (!exams.containsKey(examId)) {

return;

}

for (int i = 0; i < exams.get(examId).size(); i++) {

if (i == questionNumber) {

exams.get(examId).remove(i);

exams.get(examId).add(new FillBlankQuestion(questionNumber, text, points, answer));

Collections.sort(exams.get(examId));

return;

}

}

exams.get(examId).add(new FillBlankQuestion(questionNumber, text, points, answer));

Collections.sort(exams.get(examId));

}

public String getKey(int examId) {

if (!exams.containsKey(examId)) {

return “Exam not found”;

}

String key = “”;

for (int i = 0; i < exams.get(examId).size(); i++) {

key += exams.get(examId).get(i).toString() + “\n”;

}

return key;

}

public boolean addStudent(String name) {

if (!studentScores.containsKey(name)) {

studentScores.put(name, new HashMap<Integer, ArrayList<Double>>());

return true;

}

return false;

}

public void answerTrueFalseQuestion(String studentName, int examId, int questionNumber, boolean answer) {

if (!studentScores.get(studentName).containsKey(examId)) {

studentScores.get(studentName).put(examId, new ArrayList<Double>());

}

for (int i = 0; i < exams.get(examId).size(); i++) {

Question temp = (Question) exams.get(examId).get(i);

if (temp.getQuestionNumber() == questionNumber) {

TrueFalse q = (TrueFalse) temp;

if (answer == q.getAnswer()) {

studentScores.get(studentName).get(examId).add(q.getPoints());

} else if (answer != q.getAnswer()) {

studentScores.get(studentName).get(examId).add(0.0);

}

}

}

}

public void answerMultipleChoiceQuestion(String studentName, int examId, int questionNumber, String[] answer) {

if (!studentScores.get(studentName).containsKey(examId)) {

studentScores.get(studentName).put(examId, new ArrayList<Double>());

}

ArrayList<String> examKey = new ArrayList<String>();

List<String> studentKey = Arrays.asList(answer);

double points = 0.0;

for (int i = 0; i < exams.get(examId).size(); i++) {

Question temp = (Question) exams.get(examId).get(i);

if (questionNumber == temp.getQuestionNumber()) {

MultipleChoiceQuestion q = (MultipleChoiceQuestion) temp;

points = q.getPoints();

for (int j = 0; j < q.getAnswer().size(); j++) {

examKey.add(q.getAnswer().get(j));

}

}

}

if (examKey.size() != studentKey.size()) {

studentScores.get(studentName).get(examId).add(0.0);

return;

}

for (int i = 0; i < examKey.size(); i++) {

if (!studentKey.contains(examKey.get(i))) {

studentScores.get(studentName).get(examId).add(0.0);

return;

}

}

studentScores.get(studentName).get(examId).add(points);

}

public void answerFillInTheBlanksQuestion(String studentName, int examId, int questionNumber, String[] answer) {

if (!studentScores.get(studentName).containsKey(examId)) {

studentScores.get(studentName).put(examId, new ArrayList<Double>());

}

ArrayList<String> list = new ArrayList<String>();

ArrayList<String> studentKey = new ArrayList<String>();

double score = 0.0;

double finalScore = 0.0;

for (int i = 0; i < answer.length; i++) {

studentKey.add(answer[i]);

}

for (int i = 0; i < exams.get(examId).size(); i++) {

Question temp = (Question) exams.get(examId).get(i);

if (questionNumber == temp.getQuestionNumber()) {

FillBlankQuestion q = (FillBlankQuestion) temp;

score = q.getPoints() / q.getAnswer().size();

for (int j = 0; j < q.getAnswer().size(); j++) {

list.add(q.getAnswer().get(j));

}

break;

}

}

for (int i = 0; i < studentKey.size(); i++) {

if (list.contains(studentKey.get(i))) {

finalScore += score;

}

}

studentScores.get(studentName).get(examId).add(finalScore);

}

public double getExamScore(String studentName, int examId) {

double score = 0.0;

for (int i = 0; i < studentScores.get(studentName).get(examId).size(); i++) {

score += studentScores.get(studentName).get(examId).get(i);

}

return score;

}

public String getGradingReport(String studentName, int examId) {

String report = “”;

double studentScore = 0.0;

double totalScore = 0.0;

int counter = 1;

for (int i = 0; i < studentScores.get(studentName).get(examId).size(); i++) {

studentScore += studentScores.get(studentName).get(examId).get(i);

totalScore += exams.get(examId).get(i).getPoints();

report += “Question #” + counter++ + ” ” + studentScores.get(studentName).get(examId).get(i);

report += ” points out of ” + exams.get(examId).get(i).getPoints();

report += “\n”;

}

report += “Final Score: ” + studentScore + ” out of ” + totalScore;

return report;

}

public void setLetterGradesCutoffs(String[] letterGrades, double[] cutoffs) {

if (letterGrades.length != cutoffs.length) {

throw new IllegalArgumentException(“There is not a grade for every cuttoff.”);

}

for (int i = 0; i < letterGrades.length; i++) {

cutoffGrades.put(letterGrades[i], cutoffs[i]);

}

}

public double getCourseNumericGrade(String studentName) {

double raw = 0.0;

double examTotal = 0.0;

for (Integer e : studentScores.get(studentName).keySet()) {

for (int i = 0; i < exams.get(e).size(); i++) {

examTotal += exams.get(e).get(i).getPoints();

}

raw += (getExamScore(studentName, e) / examTotal);

examTotal = 0;

}

return raw * 100 / exams.size();

}

public String getCourseLetterGrade(String studentName) {

String grade = “”;

for (String g : cutoffGrades.keySet()) {

if (cutoffGrades.get(g) <= getCourseNumericGrade(studentName)) {

return g;

}

}

return grade;

}

public String getCourseGrades() {

String grades = “”;

ArrayList<String> list = new ArrayList<String>();

for (String student : studentScores.keySet()) {

list.add(student);

}

Collections.sort(list);

for (int i = 0; i < list.size(); i++) {

grades += list.get(i) + ” ” + getCourseNumericGrade(list.get(i)) + ” ” + getCourseLetterGrade(list.get(i))

+ “\n”;

}

return grades;

}

public double getMaxScore(int examId) {

double currentStudentScore = 0.0;

ArrayList<Double> allScores = new ArrayList<Double>();

for (String student : studentScores.keySet()) {

for (int k = 0; k < studentScores.get(student).get(examId).size(); k++) {

currentStudentScore += studentScores.get(student).get(examId).get(k);

}

allScores.add(currentStudentScore);

currentStudentScore = 0;

}

return Collections.max(allScores);

}

public double getMinScore(int examId) {

double currentStudentScore = 0.0;

ArrayList<Double> allScores = new ArrayList<Double>();

for (String student : studentScores.keySet()) {

for (int k = 0; k < studentScores.get(student).get(examId).size(); k++) {

currentStudentScore += studentScores.get(student).get(examId).get(k);

}

allScores.add(currentStudentScore);

currentStudentScore = 0;

}

return Collections.min(allScores);

}

public double getAverageScore(int examId) {

double average = 0.0;

int studentCounter = 0;

for (String student : studentScores.keySet()) {

studentCounter++;

for (int k = 0; k < studentScores.get(student).get(examId).size(); k++) {

average += studentScores.get(student).get(examId).get(k);

}

}

average = average / studentCounter;

return average;

}

@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;

}

} 

TrueFalse.java

 package onlineTest;

public class TrueFalse extends Question {

protected boolean answer;

public TrueFalse(int questionNumber, String text, double points, boolean answer) {

super(questionNumber, text, points);

this.answer = answer;

}

public int getQuestionNumber() {

return super.getQuestionNumber();

}

public double getPoints() {

return super.getPoints();

}

public boolean getAnswer() {

return this.answer;

}

public String toString() {

if (answer == true) {

return super.toString() + “Correct Answer: True”;

} else

return super.toString() + “Correct Answer: False”;

}

public int compareTo(TrueFalse obj) {

if (super.getQuestionNumber() > obj.getQuestionNumber()) {

return 1;

} else if (super.getQuestionNumber() == obj.getQuestionNumber()) {

return 0;

}

return -1;

}

} 

PublicTests.java

 package tests;

import junit.framework.TestCase;

import onlineTest.*;

import java.util.*;

public class PublicTests extends TestCase {

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”;

assertTrue(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(“\nExam 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(“\nExam score for ” + studentName + ” ” + manager.getExamScore(studentName, 10));

assertTrue(TestingSupport.correctResults(“pubTestTrueFalse.txt”, answer.toString()));

}

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()));

}

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?\n”;

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?\n”;

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?\n”;

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()));

}

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?\n”;

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?\n”;

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?\n”;

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 + “\n” + 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(“\nReport for ” + studentName + “\n” + manager.getGradingReport(studentName, 10));

assertTrue(TestingSupport.correctResults(“pubTestMultipleChoice.txt”, answer.toString()));

}

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));

// System.out.println(manager.getKey(10));

assertTrue(TestingSupport.correctResults(“pubTestFillInTheBlanksKey.txt”, answer.toString()));

}

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 + “\n” + 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(“\nReport for ” + studentName + “\n” + manager.getGradingReport(studentName, 10));

assertTrue(TestingSupport.correctResults(“pubTestFillInTheBlanks.txt”, answer.toString()));

}

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?\n”;

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(“\nExam #1 Score” + ” ” + manager.getExamScore(studentName, 1));

answer.append(“\n” + manager.getGradingReport(studentName, 1));

answer.append(“\nExam #2 Score” + ” ” + manager.getExamScore(studentName, 2));

answer.append(“\n” + manager.getGradingReport(studentName, 2));

manager.setLetterGradesCutoffs(new String[] { “A”, “B”, “C”, “D”, “F” }, new double[] { 90, 80, 70, 60, 0 });

answer.append(“\nCourse letter grade: ” + manager.getCourseLetterGrade(studentName));

assertTrue(TestingSupport.correctResults(“pubTestCourseNumericLetterGrade.txt”, answer.toString()));

}

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()));

}

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) + “\n”);

answer.append(“Minimum Score Midterm ” + manager.getMinScore(1) + “\n”);

answer.append(“Average Score Midterm ” + manager.getAverageScore(1) + “\n”);

assertTrue(TestingSupport.correctResults(“pubTestMaxMinAverageScoreInExam.txt”, answer.toString()));

}

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?\n”;

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?\n”;

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?\n”;

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 + “\n”

+ manager.getGradingReport(student, examId) + “\n\n”);

}

}

for (examId = 1; examId <= 3; examId++) {

answer.append(“Minimum for Exam # ” + examId + ” ” + manager.getMinScore(examId) + “\n”);

answer.append(“Maximum for Exam # ” + examId + ” ” + manager.getMaxScore(examId) + “\n”);

answer.append(“Average for Exam # ” + examId + ” ” + (int) manager.getAverageScore(examId) + “\n”);

}

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) + “\n”);

}

assertTrue(TestingSupport.correctResults(“pubTestMultipleExamsStudents.txt”, answer.toString()));

}

} 

StudentTests.java

package tests;

import junit.framework.TestCase;

import onlineTest.*;

public class StudentTests extends TestCase {

public void testAddTrueFalseQuestion() {

SystemManager manager = new SystemManager();

manager.addExam(10, “Midterm”);

manager.addTrueFalseQuestion(10, 1, “Is it A”, 2.0, true);

// System.out.println(manager.getKey(10));

}

public void testAddAnyTypeOfQuestion() {

SystemManager manager = new SystemManager();

manager.addExam(10, “Midterm”);

manager.addStudent(“Bob”);

manager.addTrueFalseQuestion(10, 1, “blah”, 1.0, false);

manager.addMultipleChoiceQuestion(10, 2, “blah”, 1.0, new String[] { “A” });

}

public void testAnswerMultipleChoice() {

SystemManager manager = new SystemManager();

manager.addExam(10, “Midterm”);

manager.addStudent(“Bob”);

manager.addMultipleChoiceQuestion(10, 1, “A or B”, 1.0, new String[] { “A”, “B” });

manager.answerMultipleChoiceQuestion(“Bob”, 10, 1, new String[] { “B” });

// System.out.println(manager.getGradingReport(“Bob”, 10));

}

public void testAddMultipleExams() {

SystemManager manager = new SystemManager();

assertTrue(manager.addExam(9, “Midterm 2”));

assertTrue(manager.addExam(8, “Midterm”));

manager.addTrueFalseQuestion(9, 1, “blah”, 1.0, false);

manager.addMultipleChoiceQuestion(9, 2, “blah”, 1.0, new String[] { “A” });

System.out.println(manager.getKey(9));

}

} 

TestingSupport.java 

package tests;

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

import javax.swing.JOptionPane;

public class TestingSupport {

/**

* Feel free to use the correctResults method while developing your own tests.

* Notice that if you define text files with some expected results, the text

* files must be named starting with “studentTest” and ending with the .txt

* extension. If you don’t name files this way, then the submit server will

* generate an authorization error.

*

* @param filename

* @param results

* @return true if the contents of the file corresponds to those in results

*/

public static boolean correctResults(String filename, String results) {

officialUseIgnore(filename, results);

String officialResults = “”;

try {

BufferedReader fin = new BufferedReader(new FileReader(filename));

String line;

while ((line = fin.readLine()) != null) {

officialResults += line + “\n”;

}

} catch (IOException e) {

System.out.println(“File opening failed.”);

return false;

}

results = removeBlanks(results);

officialResults = removeBlanks(officialResults);

if (results.equals(officialResults)) {

return true;

}

return false;

}

public static boolean sameContents(String firstFile, String secondFile) {

if (removeBlanks(fileData(firstFile)).equals(removeBlanks(fileData(secondFile))))

return true;

return false;

}

public static String fileData(String fileName) {

StringBuffer stringBuffer = new StringBuffer();

try {

FileReader fileReader = new FileReader(fileName);

BufferedReader bufferedReader = new BufferedReader(fileReader);

Scanner fileScanner = new Scanner(bufferedReader);

while (fileScanner.hasNextLine())

stringBuffer.append(fileScanner.nextLine());

fileScanner.close();

} catch (IOException e) {

System.out.println(e.getMessage());

}

return stringBuffer.toString();

}

public static String removeBlanks(String src) {

StringBuffer resultsBuf = new StringBuffer();

char curr;

for (int i = 0; i < src.length(); i++) {

curr = src.charAt(i);

if (curr != ‘ ‘ && curr != ‘\n’)

resultsBuf.append(curr);

}

return resultsBuf.toString();

}

public static boolean writeToFile(String filename, String message) {

try {

FileWriter output = new FileWriter(filename);

output.write(message);

output.close();

} catch (IOException exception) {

System.out.println(“ERROR: Writing to file ” + filename + ” failed.”);

return false;

}

return true;

}

/* Leave the following variable set to false. We use while developing the */

/* expected solutions for a test. If you change it to false you will corrupt */

/* your *.txt files containing expected results. */

private static boolean generateOfficialResults = false;

/**

* We use this method to generate text files with the expected results for a

* test.

*

* @param filename

* @param results

*/

private static void officialUseIgnore(String filename, String results) {

if (generateOfficialResults) {

String warningMessage = “Warning: You will overwrite result files.”;

warningMessage += ” Do you want to continue?”;

if (JOptionPane.showConfirmDialog(null, warningMessage) == JOptionPane.YES_OPTION) {

TestingSupport.writeToFile(filename, results);

System.out.println(“File ” + filename + ” has been updated.”);

}

}

}

}