Homework 11
Inheritance

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

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.

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:

When completed, the class diagram appear similar to that at right.

Assignment

  1. HW11 - create a new project named HW11.
  2. UserInterface
    1. Create a new class named UserInterface.
    2. Copy the above UserInterface.
    3. Do not modify UserInterface class.
  3. Calculator
    1. Create a new class named Calculator.
    2. Copy the the above Calculator.
    3. Test inside BlueJ (see below instructions).
    4. Complete the Calculator class to implement the remaining digits and operations of -, *, /, and CR for clear. 
  4. MemoryCalculator
    1. Inherit the basic four function calculator of Calculator class to implement a MemoryCalculator with unary operations of:
      • SM - store to memory
      • RM - recover from memory
      • MC - memory clear.
    2. You should not need to modify the Calculator class.
  5. ScientificCalculator
    1. Inherit the Calculator class to implement a ScientificCalculator with an unary operation of your choice (e.g. sine, absolute value).
    2. You should not need to modify the Calculator class.
  6. ScientificMemoryCalcuator
    1. Inherit the MemoryCalculator class in the ScientificCalculator to implement a ScientificMemoryCalcuator.
    2. You should not need to make any other modifications.
  7. Your choice.
    1. Inherit any of the classes (UserInterface, Calculator, MemoryCalculator, ScientificCalculator, ScientificMemoryCalculator) to produce a new application of your choice. 
    2. You should not need to modify any inherited class.
  8. Extra credit
    1. Add a binary operation to any class that inherits from Calculator. This will require overloading equal() method.
    2. You should not modify any inherited class.

Turn In

C201 OnCourse Dropbox - Create a HW11 folder in the C201 OnCourse DropBox. Copy the following:

  1. UserInterface, Calculator, MemoryCalculator, ScientificMemoryCalculator and your new class JAVA files.
  2. Use Alt Prnt Scrn to capture the applet window, then paste into Word:
    1. Applet window after performing 1 / 3  = + 4.
    2. Applet window after performing 4 + 5 = SM CR 6 + RM =.
    3. For some unary function FUN, applet window after performing operations such as 4 + 5 = SM CR 6 + RM = FUN.
    4. Applet window showing execution of your new class.

 

 

Testing inside BlueJ

  1. Right-click on the class to test as an applet. For example, click on Calculator.
  2. Select Run Applet.
  3. Select Run Applet in appletviewer.
  4. Click OK.
  5. For the Calculator you should see the Applet window at right.

 

          

 

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.

  1. Right-click on the class to test as an applet.
  2. Select Run Applet.
  3. Select Run Applet in web browser.
  4. Set the width and height to 100.
  5. Click OK.

Thereafter, the HTML can be loaded and the applet executed by the browser.