/* * TestPaper.java * * Created on December 9, 2002, 2:09 AM */ package grading; import java.util.Random; /** * * @author mike */ public class TestPaper { private String name = ""; private char[] answers; // array of answers by student private double pctCorrect = 0; private int numCorrect = 0; private int numIncorrect = 0; private static Random generator = new Random(); // uses time as a seed /** * Service method for generating a random multiple choice answer */ public static char genRandAnsw () { int rand; rand = generator.nextInt(5); // generate an int from 0-4 // 0 - 'a', 1 - 'b' etc switch (rand) { case 0: return 'a'; case 1: return 'b'; case 2: return 'c'; case 3: return 'd'; case 4: return 'e'; } return 'a'; // should never be hit } /** * Creates a new instance of TestPaper */ public TestPaper() { name = "John Doe"; } /** * Creates a new instance of TestPaper, given name */ public TestPaper(String nam) { name = nam; } /** * Creates a new instance of TestPaper, given name, number of answers * (answers are randomly generated) */ public TestPaper(String nam, int numAnsw) { name = nam; answers = new char [numAnsw]; for (int ans = 0; ans < numAnsw; ans++) { answers[ans] = genRandAnsw(); } } /** * report student name */ public String getName() { return name; } /** * reports students answer on the Nth question * Should throw an exception if bad n */ public char getNthAnswer(int n) { return answers[n]; } /** * Checks student's answer on Nth question angainst correct answer (answ) * Should throw an exception if bad n */ public boolean checkNthAnswer(int n, char answ) { if (answers[n] == answ) { return true; } else { return false; } } /** * report number of answers student has made */ public int getNumAnswers() { return answers.length; } /** * obtain whole array of student's answers */ public char[] getAnswers() { return answers; } /** * determines if two test papers are essentially the same - take tough stance here * - name and all answers must be the same */ public boolean equals(Object obj) { // equal if exactly the same TestPaper toTest = (TestPaper) obj; // should check instanceof to avoid bugs boolean eq = true; if (! name.equals(toTest.name)) { // name doesn't match - students must not be equal eq = false; } // if don't have the same number of answers as other student - must not be equal if (getNumAnswers() != toTest.getNumAnswers() ) { eq = false; } // check all answers if still possible to equal if (eq) { for (int ans = 0; ans < getNumAnswers(); ans++) { if (answers[ans] != toTest.getNthAnswer(ans) ) { // this answer doesn't match, so students must not match eq = false; break; } } } return eq; } /** * obtain string representation for the test paper */ public String toString() { String res = name; for (int ans = 0; ans < getNumAnswers(); ans++) { res = res + " " + answers[ans]; } // consider adding stats on performance return res; } /** * change student's name */ public void setName(String nam) { name = nam; } /** * change student's Nth answer to given value */ public void setNthAnswer(int n, char answ) { // need to check for bad n answers[n] = answ; } /** * report number of correct by student * Assumes that Test Paper has already been checked */ public int getNumCorrect() { return numCorrect; } /** * report number of incorrect by student * Assumes that Test Paper has already been checked */ public int getNumIncorrect() { return numIncorrect; } /** * report pct correct by student * Assumes that Test Paper has already been checked */ public double getPctCorrect() { return pctCorrect; } /** * Grade student's answers against correct answers, including updating stats * (number correct etc) */ public double gradeAll(char[] correctAnswers) { // need to check that arrays are compatible sizes numCorrect = 0; numIncorrect = 0; for (int ans = 0; ans < getNumAnswers(); ans++) { if (answers[ans] == correctAnswers[ans] ) { numCorrect++; } else { numIncorrect++; } } // need to avoid division by 0 pctCorrect = (double) numCorrect / (numCorrect + numIncorrect); return pctCorrect; } public static void main (String args[]) { char [] correct = { 'a', 'b', 'c', 'd', 'e', 'd', 'c', 'b', 'a', 'e'}; TestPaper [] allStudents = new TestPaper[3]; for (int cnt = 0; cnt < allStudents.length; cnt++ ) { allStudents[cnt] = new TestPaper("John " + (cnt+1), 10); System.out.println("TestPaper: " + allStudents[cnt]); double pct = allStudents[cnt].gradeAll(correct); System.out.println("Correct: " + pct); } } }