chapter day 19. © 2007 pearson addison-wesley. all rights reserved7-2 agenda day 19 problem set 3...

27
Chapter Day 19

Post on 22-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

ChapterDay 19

Page 2: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-2

Agenda Day 19

• Problem set 3 Corrected 1 A, 2 B’s and 1 D

• Problem set 4 Posted 10 problems from chapters 7 & 8 Due Nov 21 (right before

November break)

• Capstones Schedule 1st Progress reports OVERdue Only got one

• Quiz 2 on November 10 (next Class) Chap 5, 6 & 7 25 M/C 50 mins

• We are far behind the anticipated schedule There will be only 5 problems sets

and 3 quiz's May not get to Chap 12,

Collections

• New Course Grading rubric Exams (3 @ 10% each)

30% Problem Sets (5 @ 8% each) 40% Group Project 0% Capstone Project 20% Pre-professional Conduct (see Contract

on Classroom Behavior) 10%

• Today we will discuss polygons and polylines mouse events and keyboard events Inheritance

Page 3: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

Chapter7Arrays

5TH EDITION

Lewis & Loftus

java Software Solutions Foundations of Program Design

© 2007 Pearson Addison-Wesley. All rights reserved

Page 4: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-4

Outline

Declaring and Using Arrays

Arrays of Objects

Variable Length Parameter Lists

Two-Dimensional Arrays

The ArrayList Class

Polygons and Polylines

Mouse Events and Key Events

Page 5: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-5

Polygons and Polylines

• Arrays can be helpful in graphics processing

• For example, they can be used to store a list of coordinates

• A polygon is a multisided, closed shape

• A polyline is similar to a polygon except that its endpoints do not meet, and it cannot be filled

• See Rocket.java (page 411)• See RocketPanel.java (page 412)

Page 6: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-6

The Polygon Class

• The Polygon class can also be used to define and draw a polygon

• It is part of the java.awt pacakage

• Versions of the overloaded drawPolygon and fillPolygon methods take a single Polygon object as a parameter instead of arrays of coordinates

• A Polygon object encapsulates the coordinates of the polygon

Page 7: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-7

Outline

Declaring and Using Arrays

Arrays of Objects

Variable Length Parameter Lists

Two-Dimensional Arrays

The ArrayList Class

Polygons and Polylines

Mouse Events and Key Events

Page 8: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-8

Mouse Events

• Events related to the mouse are separated into mouse events and mouse motion events

• Mouse Events:

mouse pressed the mouse button is pressed down

mouse released the mouse button is released

mouse clicked the mouse button is pressed down and released without moving the mouse in between

mouse entered the mouse pointer is moved onto (over) a component

mouse exited the mouse pointer is moved off of a component

Page 9: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-9

Mouse Events

• Mouse Motion Events:

mouse moved the mouse is moved

mouse dragged the mouse is moved while the mouse button is pressed down

• Listeners for mouse events are created using the MouseListener and MouseMotionListener interfaces

• A MouseEvent object is passed to the appropriate method when a mouse event occurs

Page 10: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-10

Mouse Events

• For a given program, we may only care about one or two mouse events

• To satisfy the implementation of a listener interface, empty methods must be provided for unused events

• See Dots.java (page 415)• See DotsPanel.java (page 416)

Page 11: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-11

Mouse Events

• Rubberbanding is the visual effect in which a shape is "stretched" as it is drawn using the mouse

• The following example continually redraws a line as the mouse is dragged

• See RubberLines.java (page 419)• See RubberLinesPanel.java (page 420)

Page 12: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-12

Key Events

• A key event is generated when the user types on the keyboard

key pressed a key on the keyboard is pressed down

key released a key on the keyboard is released

key typed a key on the keyboard is pressed down and released

• Listeners for key events are created by implementing the KeyListener interface

• A KeyEvent object is passed to the appropriate method when a key event occurs

Page 13: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-13

Key Events

• The component that generates a key event is the one that has the current keyboard focus

• Constants in the KeyEvent class can be used to determine which key was pressed

• The following example "moves" an image of an arrow as the user types the keyboard arrow keys

• See Direction.java (page 423)• See DirectionPanel.java (page 424)• Modified with rubber walls

DirectionPanelB.java DirectionB.java

Page 14: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-14

Summary

• Chapter 7 has focused on:

array declaration and use bounds checking and capacity arrays that store object references variable length parameter lists multidimensional arrays the ArrayList class polygons and polylines mouse events and keyboard events

Page 15: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

Chapter8Inheritance

5TH EDITION

Lewis & Loftus

java Software Solutions Foundations of Program Design

© 2007 Pearson Addison-Wesley. All rights reserved

Page 16: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-16

Inheritance

• Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes

• Chapter 8 focuses on:

deriving new classes from existing classes the protected modifier creating class hierarchies abstract classes indirect visibility of inherited members designing for inheritance the GUI component class hierarchy extending listener adapter classes the Timer class

Page 17: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-17

Outline

Creating Subclasses

Overriding Methods

Class Hierarchies

Inheritance and Visibility

Designing for Inheritance

Inheritance and GUIs

The Timer Class

Page 18: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-18

Inheritance

• Inheritance allows a software developer to derive a new class from an existing one

• The existing class is called the parent class, or superclass, or base class

• The derived class is called the child class or subclass

• As the name implies, the child inherits characteristics of the parent

• That is, the child class inherits the methods and data defined by the parent class

Page 19: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-19

Inheritance

• Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent class

Vehicle

Car

• Proper inheritance creates an is-a relationship, meaning the child is a more specific version of the parent

Page 20: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-20

Inheritance

• A programmer can tailor a derived class as needed by adding new variables or methods, or by modifying the inherited ones

• Software reuse is a fundamental benefit of inheritance

• By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software

Page 21: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-21

Deriving Subclasses

• In Java, we use the reserved word extends to establish an inheritance relationship

• See Words.java (page 442)• See Book.java (page 443)• See Dictionary.java (page 444)

class Car extends Vehicle

{

// class contents

}

Page 22: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-22

The protected Modifier

• Visibility modifiers affect the way that class members can be used in a child class

• Variables and methods declared with private visibility cannot be referenced by name in a child class

• They can be referenced in the child class if they are declared with public visibility -- but public variables violate the principle of encapsulation

• There is a third visibility modifier that helps in inheritance situations: protected

Page 23: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-23

The protected Modifier

• The protected modifier allows a child class to reference a variable or method directly in the child class

• It provides more encapsulation than public visibility, but is not as tightly encapsulated as private visibility

• A protected variable is visible to any class in the same package as the parent class

• The details of all Java modifiers are discussed in Appendix E

• Protected variables and methods can be shown with a # symbol preceding them in UML diagrams

Page 24: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-24

Class Diagram for Words

Book

# pages : int

+ pageMessage() : void

Dictionary

- definitions : int

+ definitionMessage() : void

Words

+ main (args : String[]) : void

Page 25: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-25

The super Reference

• Constructors are not inherited, even though they have public visibility

• Yet we often want to use the parent's constructor to set up the "parent's part" of the object

• The super reference can be used to refer to the parent class, and often is used to invoke the parent's constructor

• See Words2.java (page 447)• See Book2.java (page 448)• See Dictionary2.java (page 449)

Page 26: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-26

The super Reference

• A child’s constructor is responsible for calling the parent’s constructor

• The first line of a child’s constructor should use the super reference to call the parent’s constructor

• The super reference can also be used to reference other variables and methods defined in the parent’s class

Page 27: Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted

© 2007 Pearson Addison-Wesley. All rights reserved 7-27

Multiple Inheritance

• Java supports single inheritance, meaning that a derived class can have only one parent class

• Multiple inheritance allows a class to be derived from two or more classes, inheriting the members of all parents

• Collisions, such as the same variable name in two parents, have to be resolved

• Java does not support multiple inheritance

• In most cases, the use of interfaces gives us aspects of multiple inheritance without the overhead