package jsmith.TextAns5; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*; import java.util.*; public class TextAnswerApplet extends Applet implements ActionListener{ JScrollPane scrollPane = null; JTextArea textArea = null; JButton submitButton = null; int attemptNumber; JTextArea[] hintText = new JTextArea[5]; JTextArea answerTextArea = null; Vector[] compareOrSubVectors = new Vector[20]; Vector compareOrVector = new Vector(); Vector hintVector = new Vector(); public void init(){ textArea = new JTextArea(); textArea.setLineWrap(true); hintVector = new Vector(); scrollPane = new JScrollPane(textArea); submitButton = new JButton("Submit Answer"); submitButton.addActionListener(this); this.setLayout(new BorderLayout()); this.add(scrollPane, "Center"); this.add(submitButton, "South"); attemptNumber = 0; answerTextArea = new JTextArea( "Answer 1: The claim that the current caused the fire wouldn't \n" + "strictly be \"wrong\" because that current started the chain \n" + "of events that led to the fire. But the current ALONE would \n" + "not have caused the fire. \n\n" + "Answer 2: The current caused the spark and the spark caused \n " + "the fire under the conditions that existed in the cabin. So \n " + "the current did cause the fire.\n\n" + "Answer 3: The spark was one of the causal factors that collectively \n " + "were responsible for the fire. The spark was the result of electrical\n " + "current in a faulty wire. So, in this sense the current was A CAUSE \n " + "of the fire." ); for(int j = 0; j < 20; ++j){ compareOrSubVectors[j] = new Vector(); } compareOrVector = new Vector(); compareOrSubVectors[0].add(new String("spark")); compareOrSubVectors[0].add(new String("cause")); compareOrSubVectors[0].add(new String("caused")); compareOrSubVectors[0].add(new String("causal")); compareOrSubVectors[0].add(new String("factor")); compareOrSubVectors[0].add(new String("current")); compareOrSubVectors[0].add(new String("faulty")); compareOrSubVectors[0].add(new String("wire")); compareOrSubVectors[0].add(new String("wiring")); compareOrVector.add(compareOrSubVectors[0]); // compareOrVector.add(compareOrSubVectors[1]); hintText[0] = new JTextArea( "You answers must be in grammatically complete\n" + "sentences. Note: Electrical current is what produces \n" + "a spark when there is a gap in wiring and the voltage \n" + "producing the current is high enough. \n" ); hintVector.addElement(hintText[0]); hintText[1] = new JTextArea( "Make sure to identify in complete sentences whether\n" + "or not the current was an event that PLAYED A ROLE\n" + "in the fire. Remember that we have been identifying \n" + "something as a \"causal factor\" even it is just one of \n" + "of the things that had to happen for the effect to happen.\n\n" ); hintVector.addElement(hintText[1]); hintText[2] = new JTextArea( "Here is a good answer. Type it in.\n\n" + "The current caused the spark and the spark caused \n " + "the fire under the conditions that existed in the cabin. So \n " + "the current did cause the fire.\n\n" ); hintVector.addElement(hintText[2]); hintText[3] = new JTextArea( "Here is a good answer. Type it in.\n\n" + "The current caused the spark and the spark caused \n " + "the fire under the conditions that existed in the cabin. So \n " + "the current did cause the fire.\n\n" ); hintVector.addElement(hintText[3]); hintText[4] = new JTextArea( "Here is a good answer. Type it in.\n\n" + "The current caused the spark and the spark caused \n " + "the fire under the conditions that existed in the cabin. So \n " + "the current did cause the fire.\n\n" ); hintVector.addElement(hintText[4]); } public void actionPerformed(ActionEvent evt){ boolean periodFlag = false; boolean[] acceptableOrAnswerFlag = new boolean[20]; boolean acceptableAndAnswerFlag = true; attemptNumber = attemptNumber + 1; for(int j = 0; j < 20; ++j){ acceptableOrAnswerFlag[j] = false; } String studentAnswerString = new String(textArea.getText()) + " "; Vector answerWords = new Vector(); String holdString = new String(); Character holdChar; holdString = new String(""); int i = 0; while(i < studentAnswerString.length()){ while(studentAnswerString.charAt(i) != ' ' && studentAnswerString.charAt(i) != '.'){ holdChar = new Character(studentAnswerString.charAt(i)); holdString = holdString + holdChar.toString(); i = i + 1; } answerWords.addElement(holdString); holdString = new String(); if(studentAnswerString.charAt(i) == '.'){ periodFlag = true; } i = i + 1; } // System.out.println("words in answer:\n" + answerWords); // System.out.println("compare with:\n" + compareOrVector); for (int j = 0; j < compareOrVector.size(); ++j){ for(int k = 0; k < compareOrSubVectors[j].size(); ++k){ if(answerWords.contains(compareOrSubVectors[j].elementAt(k))){ acceptableOrAnswerFlag[j] = true; } } } for(int j = 0; j < compareOrVector.size(); ++j){ if(acceptableOrAnswerFlag[j] == false){ acceptableAndAnswerFlag = false; } } if(acceptableAndAnswerFlag && periodFlag){ ResponseFrame jFrame = new ResponseFrame(textArea, answerTextArea); jFrame.setLocation(50,50); jFrame.setVisible(true); } else{ // System.out.println(""); FeedbackFrame feedbackFrame = new FeedbackFrame( "Inadequate answer, try again.", attemptNumber, hintVector); feedbackFrame.setVisible(true); } } } class ResponseFrame extends JFrame{ JTextArea answerText = null; JTextArea rightAnswerText = null; JPanel panel1 = null; JPanel panel2 = null; JLabel label1 = null; JLabel label2 = null; JScrollPane answerScrollPane = null; JScrollPane correctAnswerScrollPane = null; ResponseFrame(JTextArea text, JTextArea ansText){ rightAnswerText = ansText; rightAnswerText.setLineWrap(true); panel1 = new JPanel(); panel2 = new JPanel(); panel1.setLayout(new BorderLayout()); panel2.setLayout(new BorderLayout()); label1 = new JLabel("Your answer:"); label2 = new JLabel("One or more possible answers:"); this.setSize(500,400); answerText = new JTextArea(); answerText.setLineWrap(true); answerText = text; this.getContentPane().setLayout(new GridLayout(2,1)); answerScrollPane = new JScrollPane(answerText); panel1.add(label1, "North"); panel1.add(answerScrollPane, "Center"); this.getContentPane().add(panel1); correctAnswerScrollPane = new JScrollPane(rightAnswerText); panel2.add(label2, "North"); panel2.add(correctAnswerScrollPane, "Center"); this.getContentPane().add(panel2); } } class FeedbackFrame extends JFrame implements ActionListener{ JPanel centerPanel = null; JLabel centerLabel = null; JButton hintButton = null; JButton giveUpButton = null; Vector hintVector = new Vector(); int attemptNumber; FeedbackFrame(String responseMessage, int attNum, Vector hVector){ attemptNumber = attNum; hintVector = hVector; // System.out.println("attempt = " + attemptNumber); setSize(200,200); setLocation(100,100); centerPanel = new JPanel(); centerPanel.setBackground(Color.white); centerLabel = new JLabel(responseMessage, JLabel.CENTER); centerPanel.setLayout(new BorderLayout()); centerPanel.add(centerLabel); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(centerPanel, "Center"); if(attemptNumber > 1){ hintButton = new JButton("Hint"); hintButton.addActionListener(this); this.getContentPane().add(hintButton,"South"); } if(attemptNumber > 6){ attemptNumber = 6; } } public void actionPerformed(ActionEvent evt){ if(evt.getSource() == hintButton){ JTextArea thisHint = new JTextArea(); if((attemptNumber - 2) <= hintVector.size()){ thisHint = (JTextArea)hintVector.elementAt(attemptNumber - 2); } else{ thisHint = (JTextArea)hintVector.elementAt(hintVector.size() - 1); } this.setSize(400,200); this.setLocation(100,100); centerPanel.remove(centerLabel); this.getContentPane().remove(hintButton); JScrollPane scrollPane = new JScrollPane(thisHint); centerPanel.add(scrollPane); this.setVisible(true); } } }