Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Chapter Day 19

2 © 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  10 problems from chapters 7 & 8  Due Nov 21 (right before November break) Capstones Schedule  1 st 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 Project0%  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

3 Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved

4 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

5 © 2007 Pearson Addison-Wesley. All rights reserved7-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) Rocket.java See RocketPanel.java (page 412) RocketPanel.java

6 © 2007 Pearson Addison-Wesley. All rights reserved7-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

7 © 2007 Pearson Addison-Wesley. All rights reserved7-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

8 © 2007 Pearson Addison-Wesley. All rights reserved7-8 Mouse Events Events related to the mouse are separated into mouse events and mouse motion events Mouse Events: mouse pressedthe mouse button is pressed down mouse releasedthe mouse button is released mouse clickedthe mouse button is pressed down and released without moving the mouse in between mouse enteredthe mouse pointer is moved onto (over) a component mouse exitedthe mouse pointer is moved off of a component

9 © 2007 Pearson Addison-Wesley. All rights reserved7-9 Mouse Events Mouse Motion Events: mouse movedthe mouse is moved mouse draggedthe 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

10 © 2007 Pearson Addison-Wesley. All rights reserved7-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) Dots.java See DotsPanel.java (page 416) DotsPanel.java

11 © 2007 Pearson Addison-Wesley. All rights reserved7-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) RubberLines.java See RubberLinesPanel.java (page 420) RubberLinesPanel.java

12 © 2007 Pearson Addison-Wesley. All rights reserved7-12 Key Events A key event is generated when the user types on the keyboard key presseda key on the keyboard is pressed down key releaseda key on the keyboard is released key typeda 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

13 © 2007 Pearson Addison-Wesley. All rights reserved7-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) Direction.java See DirectionPanel.java (page 424) DirectionPanel.java Modified with rubber walls  DirectionPanelB.java DirectionPanelB.java  DirectionB.java DirectionB.java

14 © 2007 Pearson Addison-Wesley. All rights reserved7-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

15 Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved

16 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

17 © 2007 Pearson Addison-Wesley. All rights reserved7-17 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance and Visibility Designing for Inheritance Inheritance and GUIs The Timer Class

18 © 2007 Pearson Addison-Wesley. All rights reserved7-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

19 © 2007 Pearson Addison-Wesley. All rights reserved7-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

20 © 2007 Pearson Addison-Wesley. All rights reserved7-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

21 © 2007 Pearson Addison-Wesley. All rights reserved7-21 Deriving Subclasses In Java, we use the reserved word extends to establish an inheritance relationship See Words.java (page 442) Words.java See Book.java (page 443) Book.java See Dictionary.java (page 444) Dictionary.java class Car extends Vehicle { // class contents }

22 © 2007 Pearson Addison-Wesley. All rights reserved7-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

23 © 2007 Pearson Addison-Wesley. All rights reserved7-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

24 © 2007 Pearson Addison-Wesley. All rights reserved7-24 Class Diagram for Words Book # pages : int + pageMessage() : void Dictionary - definitions : int + definitionMessage() : void Words + main (args : String[]) : void

25 © 2007 Pearson Addison-Wesley. All rights reserved7-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) Words2.java See Book2.java (page 448) Book2.java See Dictionary2.java (page 449) Dictionary2.java

26 © 2007 Pearson Addison-Wesley. All rights reserved7-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

27 © 2007 Pearson Addison-Wesley. All rights reserved7-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


Download ppt "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."

Similar presentations


Ads by Google