ELIZA was a program written in 1966 that parodied a psychotherapist session. The
ID: 3536977 • Letter: E
Question
ELIZA was a program written in 1966 that parodied a psychotherapist session. The user typed sentences and the program used those words to compose a question.
Create a simple applet or GUI application based on this idea. The program will use a label to hold the program%u2019s question, a text field into which the use can type an answer, a button for the use to signal that the answer is complete, and a quit button.
The initial text for the question label should read:
What would you like to talk about?%u201D
When the user presses a button, get the text from text field. Now extract the words from the text one at a time and find the largest word of length 4 or more. Let%u2019s call this largest word X for now. In response, create a question based on the length of the word. If the word is length 4, the new question is: %u201CTell me more about X.%u201D. If the word length 5, the new questions: %u201CWhy do you think X is important?%u201D. If the word is length 6 or more, the new question is:%u201DNow we are getting somewhere. How does X affect you the most?%u201D. If there is no word of length 4, the new question is:%u201DMaybe we should move on. Is there something else you would like to talk about?%u201D (Hint: you can use the class Scanner to extract the words from a string, assuming blanks separate the words. For example, the following statements:
String text =%u201D one potato two potato %u201C;
Scanner parser = new Scanner (text);
System.out.println(parser.next());
System.out.println(parser.next());
Display one and potato on a separate lines).
Explanation / Answer
try { String xStr = xInput.getText(); Double d = new Double(xStr); x = d.doubleValue(); } catch (NumberFormatException e) { answer.setText("Illegal data for x."); return; // Break out of the actionPerformed method. } /* Get a number from yInput in the same way. */ try { String yStr = yInput.getText(); Double d = new Double(yStr); y = d.doubleValue(); } catch (NumberFormatException e) { answer.setText("Illegal data for y."); return; // Break out of the actionPerformed method. } /* Perform the operation based on the action command from the button. Note that division by zero produces an error message. */ String op = evt.getActionCommand(); if (op.equals("+")) answer.setText( "x + y = " + (x+y) ); else if (op.equals("-")) answer.setText( "x - y = " + (x-y) ); else if (op.equals("*")) answer.setText( "x * y = " + (x*y) ); else if (op.equals("/")) { if (y == 0) answer.setText("Can't divide by zero!"); else answer.setText( "x / y = " + (x/y) ); } } // end actionPerformed()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.