Describing Similar Things In English, we commonly describe things by - - PowerPoint PPT Presentation

describing similar things
SMART_READER_LITE
LIVE PREVIEW

Describing Similar Things In English, we commonly describe things by - - PowerPoint PPT Presentation

Describing Similar Things In English, we commonly describe things by saying it is like x except... For example: Mt. Holyoke is like other colleges except it only admits women. Java is like Flash except you create graphics by


slide-1
SLIDE 1

Describing Similar Things

In English, we commonly describe things by saying “it is like x except... ” For example:

  • Mt. Holyoke is like other colleges except it
  • nly admits women.

Java is like Flash except you create graphics by programming.

Types of Variation

Extra behaviors: A strobe light is a light that flashes. Behavior has a different effect: A poisonous snake is a snake that delivers venom when it bites. Extra properties: A textbook is a book that includes exercises.

Is-A Hierarchies

Animal Vertebrate Invertebrate Insects Worms Mammals Birds Fish

1 2 3 Tuesday, February 5, 13

slide-2
SLIDE 2

Java Class Hierarchies

Person Student K12Student CollegeStudent UML Notation meaning is-a

Java Class Hierachy

public class Person { private String name; public Person (String theName) {…} public String getName() {…} } public class Student extends Person { public Student (String theName) {…} } Superclass Subclass

Inheritance

Student “inherits” the getName method. Student s = new Student(“Alice”); System.out.println (s.getName()); public class Person { private String name; public Person (String theName) {…} public String getName() {…} } public class Student extends Person { public Student (String theName) {…} }

4 5 6 Tuesday, February 5, 13

slide-3
SLIDE 3

Adding Behavior to a Subclass

public class Student extends Person { private double gpa; public Student (string theName) {…} public void setGPA (double newGPA){…} } Student s = new Student(“Alice”); System.out.println(s.getName()); s.setGPA (3.6); Person p = new Person(“Bob”); System.out.println(p.getName()); p.setGPA (3.6);

Overriding Methods

public class Person { public Person (String theName) {…} public String toString() { return name; } public String getName() {…} private String name; } public class Student extends Person { private double gpa; public Student (String theName) {…} public void setGPA (double newGPA){…} public String toString() { return getName() + “ has gpa “ + gpa; } }

A subclass can provide its own definition of an inherited method.

Overridden Methods

Person p = new Person(“Lucy”); System.out.println (p.toString()); public class Person { public Person (String theName) {…} public String toString() { return name; } public String getName() {…} private String name; } public class Student extends Person { private double gpa; public Student (String theName) {…} public void setGPA (double newGPA){…} public String toString() { return getName() + “ has gpa “ + gpa; } }

7 8 9 Tuesday, February 5, 13

slide-4
SLIDE 4

Student s = new Student(“Linus”); s.setGPA(3.1); System.out.println (s.toString()); public class Person { public Person (String theName) {…} public String toString() { return name; } public String getName() {…} private String name; } public class Student extends Person { private double gpa; public Student (String theName) {…} public void setGPA (double newGPA){…} public String toString() { return getName() + “ has gpa “ + gpa; } }

Overridden Methods What is a Student?

It is like a Person except that: It has a GPA The GPA can be set Since it is like a Person, it must have a name How does the name get set?

Constructing a Student

public Student (String theName) { super (theName); gpa = 0.0; }

The Student constructor calls the Person constructor. This must be the first statement in the subclass’ s constructor.

10 11 12 Tuesday, February 5, 13

slide-5
SLIDE 5

extends JComponent

public class HelloFromVenus extends JComponent { public void paintComponent (Graphics g) { super.paintComponent(g); int width = getWidth(); … } public static void main (String[] args) { … contentPane.add(new HelloFromVenus(), BorderLayout.CENTER); }

What is a JComponent?

Something we can paint by overriding: public void paintComponent (Graphics g) Something that has a width, which we can find

  • ut by calling:

public int getWidth () Something we can add to a panel, the content pane, … public Component add(Component comp)

JComponent Hierarchy

JComponent Component Container means “extends” can also be read from bottom to top as “is a”. A JComponent “is a” Container. HelloFromVenus

13 14 15 Tuesday, February 5, 13

slide-6
SLIDE 6

extends JPanel

public class PizzaOrder extends JPanel { public PizzaOrder () { add (new JLabel (“Name”)); … } public static void main (String[] args) { … contentPane.add(new PizzaOrder(), BorderLayout.CENTER); }

What is a JPanel?

Something we can add Swing components to: public Component add(Component comp) Something we can add to a panel, the content pane, … public Component add(Component comp)

JPanel Hierarchy

JComponent Component Container JPanel PizzaOrder

16 17 18 Tuesday, February 5, 13

slide-7
SLIDE 7

Animation

Animation Works the same way as a movie - scene changes every 30 milliseconds To program an animation: Say that your class “extends Animation” Define the nextFrame method to update the scene. This is called every 30 milliseconds: protected void nextFrame() Call the start method to start the animation public void start()

19 Tuesday, February 5, 13