Homework 11
|
Modified: |
Java was designed to support applications distributed across the Internet. These applications are called applets and are typically executed by a Web browser such as Netscape capable of executing Java code. A complete applet consists of a Java class that is the actual applet executable and an HTML (HyperText Markup Language) file with instructions to the browser to execute the Java applet class.
Consider the design of a simple four-function calculator. Visually, a calculator consists of a user interface containing a few buttons and a readout display. The calculator is then a user interface with internal accumulator that holds numbers as they're entered, displayed and used also as an operand in arithmetic. Binary operations using infix notation for expressions, 45 + 73 = for example, save the accumulator, 45, when '+' is entered, then store 73 to accumulator. When the '=' is pressed the earlier '+' operation is performed, so the + operation must be stored for use later when the '=' is pressed. The result of the arithmetic is stored back to accumulator for display and use in later calculations.
12.34 1 2 3 + -
4 5 6 * /
7 8 9 0 =
CR
The graphical user interface is implemented in the abstract UserInterface class. The class defines two user interface objects, a display on which to display text and visible buttons, and handles mouse clicking on buttons by invoking an onClick() method. Note that because UserInterface is an abstract class, no UserInterface objects can be constructed directly by new UserInterface( ). This makes sense because the UserInterface class is incomplete having no buttons to click and is designed to be extended.
The UserInterface implements an inner-class (one that is nested inside another class) of ButtonHandler which wires each button to the event handler actionPerformed. The inner-class has access to all the UserInterface fields, instance and class methods.
The event handled is a mouse click on a button, which invokes the actionPerformed() method which invokes the onClick() method, passing the first character of the button label. The onClick() method must be over-ridden and functionality is added by classes inheriting the UserInterface class, an non-functional version is defined here in order to satisfy the actionPerformed() method definition.
The purpose of each UserInterface method is:
- UserInterface( ) - Constructor that defines the user interface display and keypad, and an event handler for button clicks.
- addButton(String s) - Constructs a button with the label String s, adds the button to the keypad, and connects the button to the button handler.
- onClick( ) - An unimplemented method necessary to satisfy the definition of the ButtonHandler inner-class.
- updateDisplay( String s) - Updates display to the String s parameter.
- init( ) - To an Applet what main( ) is to non-applets.
import java.awt.*; import java.awt.event.*; import java.applet.*; abstract class UserInterface extends Applet { private class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { onClick(e.getActionCommand().charAt(0) ); } } private TextField display; private Panel keypad; public UserInterface() { this.setLayout(new BorderLayout()); display = new TextField("0"); this.add("North", display); keypad = new Panel(); keypad.setLayout(new GridLayout(4,5)); this.add("Center", keypad); } public void onClick( char c ) {} public void addButton(String s) { Button b = new Button(s); keypad.add( b ); b.addActionListener( new ButtonHandler() ); } public void updateDisplay(String s) { display.setText(s); } public void init( ) { } }
The Calculator adds addition, subtraction, multiplication, and division calculation to a UserInterface class which defines the user interface display and keypad. A Calculator must maintain state of an accumulator, operand, and operation to perform binary arithmetic.
Input of 42-15= must produce the following:
42 accumulator = 42
- operation = -, operand = accumulator
15 accumulator = 15
= accumulator = operand operation accumulator (i.e. 42-15)The purpose and special notes of each Calculator method are:
- Calculator( ) - Adds calculator buttons.
- equal( ) - Performs the arithmetic operation entered (+, -, *, /).
- getAccumulator( ) - Returns the value of the accumulator field.
- setAccumulator( ) - Sets the value of the accumulator field.
- onClick( ) - Each button click invokes the onClick() method which serves to change the calculator state. The onClick() method should invoke the updateDisplay() method to display the new state to the user interface display. Invoked on a mouse click event and receives the first character of the corresponding button label. For example, clicking SM button would invoke as: onClick( 'S' ).
public class Calculator extends UserInterface { private double accumulator=0.0, operand; private char operation; public Calculator() { addButton("1"); addButton("+"); addButton("="); } public void onClick(char c) { switch (c) { case '1' : accumulator = accumulator * 10+1; break; case '=' : equal(); break; case '+' : operation = c; operand = accumulator; accumulator = 0.0; break; default : ; } updateDisplay(getAccumulator()+""); } public void equal() { switch (operation) { case '+' : accumulator = accumulator + operand; break; } } public double getAccumulator() { return accumulator; } public void setAccumulator(double acc) { accumulator = acc; } public double getOperand() { return operand;} public void setOperand(double newOperand) { operand = newOperand;} public char getOperation() { return operation; } public void setOperation(char newOperation ) { operation = newOperation; } }
m 12.34 1 2 3 + -
4 5 6 * /
7 8 9 0 =
CR RM SM MCA memory calculator requires three buttons to store memory, recall memory, and clear memory. The buttons provide an interface of:
- store - SM Store the accumulator to memory.
- recall - RM Recall from memory to the accumulator .
- clear - MC Clear memory to 0.0
By inheriting the basic four function calculator of Calculator class, the Memory class need add only:
- memory field to store the accumulator value,
- buttons (SM, RM, MC) to access memory through an user interface,
- Constructor where the SM, RM, MC buttons are added.
- onClick( ) method where the SM, RM, MC button events are handled or the super class onClick( ) method is invoked for any other button as default.
MemoryCalculator class is then a Calculator class with the additional fields and methods, specifically memory that can be stored, recalled or cleared using the SM, RM and MC buttons.
One important issue is how to access Calculator fields (accumulator) without modifying the Calculator class? The answer is to use the get and set methods (e.g. getAccumulator( ) ) for each field provided specifically to expose attributes in a way that limits access (data encapsulation) and hides the details of the implementation (information hiding).
A skeleton of the MemoryCalculator:
public class MemoryCalculator extends Calculator { public MemoryCalculator( ) { } public void onClick(char c) { switch (c) { default : super.onClick(c); } } }

A scientific calculator requires one or more buttons to provide an interface that:
- unary function - Sets accumulator to some function result, for example: accumulator = Math.sin(accumulator).
- binary function - Set accumulator to some binary function result, for example: accumulator = ab.
By inheriting the basic four function calculator of Calculator class the ScientificCalculator class need add:
- Constructor method where the buttons are added, and super class constructor is invoked,
- onClick( ) method where the ScientificCalculator buttons are handled or the super class onClick( ) is invoked,
- methods specific to the ScientificCalculator.
When completed, the class diagram appear similar to that at right.
C201 OnCourse Dropbox - Create a HW11 folder in the C201 OnCourse DropBox. Copy the following:



Browsers can execute Java applets but the starting Java class must be named in an HTML file. BlueJ writes the necessary HTML file when the applet is run in BlueJ.
Thereafter, the HTML can be loaded and the applet executed by the browser.
C:\Users\username\bluej\HW11\Calculator.html
