next up previous
Next: Lecture 9 Up: Home

Readings


Drawing shapes by using upper the left corner reference. example circle.

coord.gif

setSize(width, height);
page.drawRect(x, y, width, height);
page.drawLine(x, y, x_end, y_end);
Storm works by repainting the applet each time. Bouncing_Ball and Rotating_Disk example uses setXORMode.
//*******************************************************************
//
//   Rotating_Disk.java          In Text          Applet
//
//   Authors:  Lewis and Loftus
//
//   Classes:  Rotating_Disk
//
//*******************************************************************

import java.applet.Applet;
import java.awt.*;

public class Rotating_Disk extends Applet {

   //===========================================================
   //  Uses ovals to simulate a rotating disk.
   //===========================================================
   public void paint (Graphics page) {

      int width = 0, height = 40;
      int x = 100, y = 100, warp = 1;
       
      page.setXORMode (getBackground());

      for (int change=1; change < 200; change++) {
         width += warp * 2;
         x -= warp;
         if (width == 0 || width == 40)
            warp *= -1;  //switch between growing and shrinking 
        
         page.fillOval (x, y, width, height);
         for (int pause=1; pause <= 100000; pause++);
         page.fillOval (x, y, width, height); // erase oval
      }

   } // method paint
   
} // class Rotating_Disk

Inheritance

Motivation for inheritance


Inheritance


Super, Defined and Inherited


Overriding inherited methods

Class hierarchies

Hierarchies organization - chosen with an eye to dynamic use in programs

Object class

Polymorphism - references and class hierarchies


Programs

Words.java    
Creating subclasses
Words2.java   
The Super Reference
Eating.java   
Defined versus Inherited
School.java   
Student Example
Messages.java 
Overriding methods
Firm.java     
Employee Example (*)
Accounts.java 
Savings Accounts Example (*)
Accounts2.java
3 Accounts Example (*)
Messages2.java
Polymorphism
Variety.java  
Polymorphism instanceof operator (*)
Firm2.java    
Paying Employee Example (*)

Enhanced Class Design


Abstract classes and inheritance

Abstract methods

Interfaces - Encapsulation

interface interfaceName {
  constant-declarations-implicitly-public-final
  all-methods-implicitly-public-abstract
}

Interfaces

classes that implement an interface

class name implements interface1, interface2 {
  overrides-all-methods-in-interface
}
an interface can extend another interface
interface extendedName extends interfaceName {
...
}

Encapsulation

Interfaces and abstract classes encapsulate information

Hiding information because


Packages - further encapsulation


import java.io.*;

class Histogram { // ch6p23
   int size;
  int[] bins;
  Histogram () {
    size = 51;
    bins = new int[size];
    for (int i=0;i < size;i++) bins[i] = 0;
  }
  void add (int number) {
    if (-1 < number && number < size) bins[number]++;
  }
  void print () {
   System.out.println( "bin\tcount");
   for (int i=0;i < size;i++)
     if (bins[i] != 0)
       System.out.println (" " + i + "\t" + bins[i]);
  }
}

import java.io.*;

class HistogramCh6p24 extends Histogram { // ch6p24
  int hi, low;
  //int size; inherited from Histogram class
  //int[] bins;
  HistogramCh6p24 (int low, int hi) {
    this.low = low;
    this.hi = hi;
    size = hi - low + 1;
    bins = new int[size];
    for (int i=0;i < size;i++) bins[i] = 0;
  }
  void add (int number) {
    if (low <= number && number <= hi) bins[number-low]++;}
  void print () {
   System.out.println( "bin\tcount");
   for ... if ...
       System.out.println (" " + (i+low) + "\t" + bins[i]);
}}



Programs
Dinner.java         
abstract inheritance
Printer.java        
file structure
Soap_Box.java       
interface - method
Protections.java    
interfaces - constant
Printer2.java       
interfaces and formal parameters
Readable_Files.java 
implements multiple interfaces (*)
Simple_IO           
packages Reader and Writer
Simple_IO_Test.java 
imports packages from another file
Simple_IO_Test2.java
classes in one file
Simple_IO_Test3.java
disambiguate pkg.clss.mthd();


next up previous
Next: Lecture 9 Up: Home
Craig MacDonald

Wed Jul 8 15:01:44 EDT 1998