1 - 3
Fundamentals of Java Programming Lab 11.7.2
Copyright
2003, Cisco Systems, Inc.
Lab 11.7.2 Calculator
Estimated Time: 30 minutes
Learning Objective:
• In this lab activity, the student will create a GUI using a grid layout to position
components.
Description/Scenario:
• This lab will create the following:
o A class called Calculator that has methods that will add, subtract, divide, and
multiply.
o A class called CalculatorGUI that displays the calculator user interface and
instantiates Calculator class that performs the number crunching.
o A class called CalculatorApp that creates a frame to display the calculator.
o A class called CalculatorApplet that displays the calculator in the appletviewer or
a browser.
File Management
Open BlueJ. Click on Project from the BlueJ main menu and select New. In the New
Project window and in the Look in: list box select c:\. Now, double click the javacourse
folder listed in the text window and a different New Project window opens with javacourse
in the Look in: list box. Now, double click the chap11 folder listed in the text window and a
different New Project window opens with chap11 in the Look in: list box. Type lab11.7.2
in the File name text box and click on Create to create a lab11.7.2 subfolder in the
chap11 folder.
Tasks:
Step 1 Create CalculatorGUI
a. Create the class CalculatorGUI that uses the following design:
b. In the CalculatorGUI class, create buttons for 0 to 9 digits and six buttons for the
period, equal, plus, minus, multiply, and divide. Use a 4 x 4 grid layout for the
buttons.
2 - 3
Fundamentals of Java Programming Lab 11.7.2
Copyright
2003, Cisco Systems, Inc.
c. Use panels and layout managers that will assist you in the CalculatorGUI design.
d. Add a constructor that instantiates the Calculator class and creates all attributes.
e. Add
a
getContentPanel() method that returns a panel that has all necessary
buttons and action listeners added to it.
f. Now, add two nested classes, OpButtonHandler & NumberButtonHandler. The
OpButtonHandler class contains an actionPerformed() method that uses the
Calculator class methods to compute answer to math operations.
g. The NumberButtonHandler class contains an actionPerformed() method that
assigns text to the label or textfield named answer. Use the following code to create
the CalculatorGUI class but modify the code that creates 12 separate buttons to code
that creates an array of buttons. Use a for loop and the button array to create the
buttons, add them to the buttonArea panel and to add the correct action listener.
Sample
Code:
private class NumberButtonHanlder implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if ( readyForNextNumber )
{
answer.setText(event.getActionCommand());
readyForNextNumber = false;
}
else
}
answer.setText(answer.getText() +
event.getActionCommand().charAt(0));
}
}//end of if
}
}//end of class
Step 2 Create class Calculator
a. Create a class named Calculator that follows the UML listed below. Use the main
method to test the math operations of the class. Use the following data for the test:
Calculator calc = new Calculator();
System.out.println("2.0 + >> " + calc.opAdd("2.0"));
System.out.println("3.0 = >> " + calc.opEquals("3.0"));
System.out.println("15.3 * >> " + calc.opMultiply("15.3"));
System.out.println("2.0 / >> " + calc.opDivide("2.0"));
Step 3 Create class CalculatorApp
a. Create a class named CalculatorApp that follows the UML listed below. Have this
class create a frame to display the CalculatorGUI class. Have the main method
launch the frame.
b. Now test the operation of the calculator using the frame.
Step 4 Create class CalculatorApplet
a. Create a class named CalculatorApplet that extends Applet and follows the UML
diagram listed below. Have this class instantiate the CalculatorGUI.
3 - 3
Fundamentals of Java Programming Lab 11.7.2
Copyright
2003, Cisco Systems, Inc.
b. Launch this applet using the appletviewer or the browser option after selecting run
from the menu in response to the right clicking of the CalculatorApplet class in the
BlueJ IDE.
c. Now test the operation of the calculator using the applet.
d. Which launches faster the frame or the applet?