Homework 11
Inheritance 

powered by FreeFind

Modified: 

SI - Next session will give additional discussion of Homework 11.

Java applet Overview

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.

Calculator Overview

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

UserInterface class

No modification is required.

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:

 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( ) { }
 }

Calculator class

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. Notice that 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:

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; }
}

Memory Calculator

m           12.34
1   2   3   +   -
4   5   6   *   /
7   8   9   0   =
CR RM SM MC

A memory calculator requires three buttons to store memory, recall memory, and clear memory. The buttons provide an interface of:

By inheriting the basic four function calculator of Calculator class, the Memory class need add only:

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);
                 }
         }
 }

Scientific Calculator

A scientific calculator requires one or more buttons to provide an interface that:

By inheriting the basic four function calculator of Calculator class the ScientificCalculator class need add:

Assignment

  1. Do not modify UserInterface class.
  2. Complete the Calculator class to implement operations of -, *, /, and CR for clear. 
  3. Inherit the basic four function calculator of Calculator class to implement a MemoryCalculator with unary operations of SM, RM and CM. You should not need to modify the Calculator class.
  4. Inherit the Calculator class to implement a ScientificCalculator with an unary operation of your choice (e.g. sine, absolute value). You should not need to modify the Calculator class.
  5. Inherit the MemoryCalculator class in the ScientificCalculator to implement a ScientificMemoryCalcuator. You should not need to make any other modifications.
  6. Inherit any of the classes (UserInterface, Calculator, MemoryCalculator, ScientificCalculator, ScientificMemoryCalculator) to produce a new application of your choice. 
  7. Extra credit - You should not modify any inherited class.

Turn In

  1. First Page - Contains your name, date and Homework 11. Staple all pages together.
  2. UserInterface, Calculator, MemoryCalculator, ScientificMemoryCalculator and your new class listing.
  3. Screen shot of window after performing 1 / 3  = + 4.
  4. Screen shot of applet window after performing 4 + 5 = SM CR 6 + RM =.
  5. For some unary function FUN, screen shot of applet window after performing operations such as 4 + 5 = SM CR 6 + RM = FUN.
  6. Screen shot of applet window showing execution of your new class.

Testing inside BlueJ

  1. Right-click on the class to test as an applet.
  2. Select Run Applet.
  3. Select Run Applet in appletviewer.
  4. Click OK.

Testing in a browser

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.