/* * @(#)SetBuilderExerciseServlet.java 0.0.0 99/08/10 * * Copyright (c) 1999 by Willie Wheeler. All rights reserved. */ package stats.exercises; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import javax.swing.*; import exercise.*; import stats.*; /** * @version 0.0.0 08/10/99 * @author Willie Wheeler */ public class SetBuilderExerciseServlet extends ExerciseServlet { protected static final String ATOM_PATH = "e:/courseware/classes/resources/images/atoms/"; protected static Map exercises = new HashMap(); static { // // This is just a temporary way of handling the exercises. Ultimately // we should read them from a database. // // // Exercise 0. // Vector vars = new Vector(); vars.add(new TextVariable("Sex", new String[] { "Male", "Female" })); vars.add(new TextVariable("Hair color", new String[] { "Blond", "Brown" })); vars.add(new TextVariable("Smoker?", new String[] { "Yes", "No" })); Vector atoms = new Vector(); ImageIcon atom = null; atom = new ImageIcon(ATOM_PATH + "white_male_blond_smoker.gif"); atom.setDescription("0,0,0"); atoms.add(atom); atom = new ImageIcon(ATOM_PATH + "white_male_blond_nonsmoker.gif"); atom.setDescription("0,0,1"); atoms.add(atom); atom = new ImageIcon(ATOM_PATH + "white_male_black_smoker.gif"); atom.setDescription("0,1,0"); atoms.add(atom); atom = new ImageIcon(ATOM_PATH + "white_male_black_nonsmoker.gif"); atom.setDescription("0,1,1"); atoms.add(atom); atom = new ImageIcon(ATOM_PATH + "white_female_blond_smoker.gif"); atom.setDescription("1,0,0"); atoms.add(atom); atom = new ImageIcon(ATOM_PATH + "white_female_blond_nonsmoker.gif"); atom.setDescription("1,0,1"); atoms.add(atom); atom = new ImageIcon(ATOM_PATH + "white_female_black_smoker.gif"); atom.setDescription("1,1,0"); atoms.add(atom); atom = new ImageIcon(ATOM_PATH + "white_female_black_nonsmoker.gif"); atom.setDescription("1,1,1"); atoms.add(atom); SetBuilderExercise sbe = new SetBuilderExercise(vars, atoms); Vector constraints = new Vector(); int[] state = { -1, -1, 0 }; int[] condState = { 0, -1, -1 }; constraints.add(sbe.new Constraint(state, condState, .50)); sbe.setConstraints(constraints); exercises.put("0", sbe); } public Exercise createExercise(String exerciseID) { return (Exercise)exercises.get(exerciseID); } public Grade evaluateSolution(Exercise exercise, Solution solution) { // Get exercise constraints. SetBuilderExercise sbe = (SetBuilderExercise)exercise; Vector constraints = sbe.getConstraints(); // Get solution relative frequencies. SetBuilderSolution sbs = (SetBuilderSolution)solution; ContingencyTableModel model = sbs.getModel(); int depIndex = ContingencyTableModel.ESTIMATE_OFFSET; // Evaluate the solution. boolean correct = true; for (int i = 0; i < constraints.size(); i++) { SetBuilderExercise.Constraint constraint = (SetBuilderExercise.Constraint)constraints.get(i); int[] state = constraint.getState(); int[] conditioningState = constraint.getConditioningState(); double target = constraint.getTarget(); double actual = model.getEstimate(state, conditioningState); if (Math.abs(target - actual) > 0.01) { // tolerance = 0.01 correct = false; break; } } // Return the results. double score = (correct ? 100.0 : 0.0); double maxScore = 100.0; return new SetBuilderGrade(score, maxScore); } }