Lab 8: Graphical User InterfacesCalculator by Greg Vogl

The goal of this exercise is to create a simple calculator. The user types two numbers into text fields and can click Add, Subtract, Multiply, Divide, or Power. The resulting calculation is displayed in a third text field.
  1. Review Lecture 9.
  2. Create an empty directory named javalab8/ in your directory.
  3. Browse to the examples in Teach Yourself Java Ch. 12, in the java1.1 folder.
    1. Copy ButtonActionsTest.html and ButtonActionsTest.java into your javalab8 directory.
    2. Rename your copies to Calculate.html and Calculate.java.
    3. Change ButtonActionsTest to Calculate in both files using search and replace.
  4. Modify the interface in Calculate.java.
    1. Add a label A and an editable text field a of width 10.
    2. Add a label B and an editable text field b of width 10.
    3. Add a label Result and non-editable text field result of width 10.
    4. Put these components into a new panel p1 using a grid layout with 3 rows and 2 columns.
    5. Change the text of the buttons to Add, Subtract, Multiply, Divide, Power.
    6. Make sure the buttons and text fields are instance variables which are defined in the init() method.
    7. Put the buttons into a new panel p2 using a centered flow layout.
    8. Add the two new panels to the applet.
    9. Compile the applet by typing javac Calculate.java.
    10. Open Calculate.html to test and debug the applet GUI and layout.
      • Your applet should look like the picture at right.
  5. Create the event handler in Calculate.java.
    1. Copy the code from HandleButton.java into Calculate.java as an inner class.
      • Make sure to import java.awt.event.* at the top.
    2. Add to the HandleButton constructor three TextField arguments: a, b, result.
      • The HandleButton class will also need three TextField variables with the same name.
      • In the constructor, set these equal to the arguments using this.a = a, etc.
    3. Instead of using a Color variable c, use a String variable named operation.
    4. Change the five HandleButton method calls to take this plus these four arguments.
      • Each button needs its own HandleButton object.
    5. In the actionPerformed() method of HandleButton
      1. Remove the statement to set the background color.
      2. Declare three floating point variables fa, fb, and fresult.
      3. Get the text from the two text boxes using getText() methods and convert to float using Float.parseFloat().
      4. Test the operation variable and add, subtract, multiply, divide or take the power accordingly.
      5. Set the result text field to fresult using its setText() method.
    6. Compile the applet by typing javac Calculate.java.
    7. Open Calculate.html to test and debug the applet event handler.
      • Enter two numbers and check that the buttons correctly calculate the sum, difference, etc.
  6. Compress the folder using the Ark archiving program to produce javalab8.zip.
  7. Send the javalab8.zip file to gvogl@umu.ac.ug as an attachment in a message with subject java lab 8.