Chapter 5: Enhancing Classes

Slides:



Advertisements
Similar presentations
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Advertisements

Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Interfaces A Java interface is a collection
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
Java Software Solutions
Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
Chapter 5: Enhancing Classes
Event Handling Events and Listeners Timers and Animation.
Chapter 5: Keyboard I/O & Simple GUI’s Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Primitive Types vs. Reference Types Variables of primitive types hold the values of the primitive types directly. Variables of reference types hold references.
Enhancing classes Visibility modifiers and encapsulation revisited
Abstract Classes.
Applet Class Hierarchy. Applet Methods called by the system public void init() When the applet is first loaded. Do initialization here. public void start()
COMP 14: Applets June 21, 2000 Nick Vallidis. Announcements zP6 is due Friday.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
INF 523Q Chapter 5: Enhancing Classes (Examples).
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Interfaces A Java interface is a collection of constants and abstract methods with a name that looks like a class name, i.e. first letter is capitalized.
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 5: Enhancing Classes
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
1 Enhancing Classes  Now we can explore various aspects of classes and objects in more detail  Chapter 5 focuses on: object references and aliases passing.
Chapter 5 Introduction to Defining Classes
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
(c) by Elizabeth Sugar Boese.1 Chapter 6 Events - Lecture Slides.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 7 Object-Oriented Design
Chapter 5: Enhancing Classes
Variables in Java A variable holds either
Interfaces.
Chapter 5: Enhancing Classes
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
CSI 1102 Introduction to Software Design
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Chapter 5: Enhancing Classes
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Java Classes Aliases & Null & This
Outline Creating Objects The String Class The Random and Math Classes
Object-Oriented Design Part 2
ITEC220 GUI Lecture – Part 2 References
Presentation transcript:

Chapter 5: Enhancing Classes

Enhancing Classes We can now explore various aspects of classes and objects in more detail Chapter 5 focuses on: object references and aliases passing objects as parameters the static modifier nested classes interfaces and polymorphism events and listeners animation

References An object reference holds the memory address of an object Chess_Piece bishop1 = new Chess_Piece(); All interaction with an object occurs through a reference variable References have an effect on actions such as assignment bishop1

Assignment The act of assignment takes a copy of a value and stores it in a variable For primitive types: num2 = num1; Before num1 5 num2 12 After num1 5 num2

Reference Assignment For object references, the value of the memory location is copied: bishop2 = bishop1; Before bishop1 bishop2 After bishop1 bishop2

Aliases Aliases Two or more references that refer to the same object One object (and its data) can be accessed using different variables Aliases can be useful, but should be managed carefully Changing the object’s state (its variables) through one reference changes it for all of its aliases

Garbage Collection Garbage Automatic garbage collection when an object no longer has any valid references to it, it can no longer be accessed by the program it is useless. Automatic garbage collection Java performs periodically, returning an object's memory to the system for future use In other languages, the programmer has the responsibility for performing garbage collection

Passing Objects to Methods Parameters in a Java method are passed by value A copy of the actual parameter is stored into the formal parameter (in the method header) Passing parameters is essentially an assignment When an object is passed to a method, the actual parameter and the formal parameter become aliases of each other

Passing Objects to Methods What you do to a parameter inside a method may or may not have a permanent effect (outside the method) See ParameterPassing.java (page 226) See ParameterTester.java (page 228) See Num.java (page 230) Note the difference between changing the reference and changing the object that the reference points to

ParameterPassing.java class ParameterPassing { public static void main (String[] args) { ParameterTester tester = new ParameterTester(); int a1 = 111; Num a2 = new Num (222); Num a3 = new Num (333); System.out.println ("Before calling changeValues:"); System.out.println ("a1\ta2\ta3"); System.out.println (a1 + "\t" + a2 + "\t" + a3 + "\n"); tester.changeValues (a1, a2, a3); System.out.println ("After calling changeValues:"); }

ParameterTester.java class ParameterTester { public void changeValues (int f1, Num f2, Num f3) { System.out.println ("Before changing the values:"); System.out.println ("f1\tf2\tf3"); System.out.println (f1 + "\t" + f2 + "\t" + f3 + "\n"); f1 = 999; f2.setValue(888); f3 = new Num (777); System.out.println ("After changing the values:"); }

Num.java class Num { private int value; // Sets up the new Num object, storing an initial value. public Num (int value) { this.value = value; } // Sets the stored value to the newly specified value. public void setValue (int value) { // Returns the stored integer value as a string. public String toString () { return value + "";

The static Modifier Static methods (class methods) can be invoked through the class name rather than through a particular object For example, the methods of the Math class are static static modifier to the method definition The static modifier can be applied to variables as well It associates a variable or method with the class rather than an object

Static Methods class Helper public static int triple (int num) { int result; result = num * 3; return result; } class Helper Because it is static, the method could be invoked as: value = Helper.triple (5);

Static Methods The order of the modifiers can be interchanged, but by convention visibility modifiers come first Recall that the main method is static; it is invoked by the system without creating an object Static methods cannot reference instance variables, because instance variables don't exist until an object exists They can reference static variables or local variables

Static Variables Static variables are sometimes called class variables Normally, each object has its own data space If a variable is declared as static, only one copy of the variable exists private static float price; Memory space for a static variable is created as soon as the class in which it is declared is loaded

Static Variables All objects created from the class share access to the static variable Changing the value of a static variable in one object changes it for all others Static methods and variables often work together See CountInstances.java (page 233) See MyClass.java (page 234)

CountInstances.java class CountInstances { // Creates several MyClass objects and prints the number of // objects that were created. public static void main (String[] args) { MyClass obj; for (int scan=1; scan <= 10; scan++) obj = new MyClass(); System.out.println ("Objects created: " + MyClass.getCount()); }

MyClass.java class MyClass { private static int count = 0; // Counts the number of instances created. public MyClass () { count++; } // Returns the number of instances of this class that have been // created. public static int getCount () { return count;

Nested Classes In addition to a class containing data and methods, it can also contain other classes A class declared within another class is called a nested class Outer Class Nested Class

Nested Classes A nested class has access to the variables and methods of the outer class, even if they are declared private In certain situations this makes the implementation of the classes easier because they can easily share information Furthermore, the nested class can be protected by the outer class from external use This is a special relationship and should be used with care

Nested Classes A nested class produces a separate bytecode file If a nested class called Inside is declared in an outer class called Outside, two bytecode files will be produced: Outside.class Outside$Inside.class Nested classes can be declared as static, in which case they cannot refer to instance variables or methods A nonstatic nested class is called an inner class

Interfaces A Java interface is a collection of abstract methods and constants the modifier abstract is not necessary An abstract method is a method header without a method body An interface is used to formally define a set of methods that a class will implement

interface is a reserved word Interfaces interface is a reserved word None of the methods in an interface are given a definition (body) public interface Doable { public void doThis(); public int doThat(); public void doThis2 (float value, char ch); public boolean doTheOther (int num); } A semicolon immediately follows each method header

Interfaces A class that implements an interface must provide implementations for all the methods in the interface This relationship is specified in the header of the class: class class-name implements interface-name { } An interface cannot be instantiated Methods in an interface have public visibility by default

Interfaces public class CanDo implements Doable { public void doThis () // whatever } public void doThat () // etc. implements is a reserved word Each method listed in Doable is given a definition

Interfaces A class that implements an interface can implement other methods as well A class can implement multiple interfaces The interfaces are listed in the implements clause, separated by commas The class must implement all methods in all interfaces listed in the header

Speaker.java interface Speaker { public void speak (); public void announce (String str); }

Philosopher.java class Philosopher implements Speaker { private String philosophy; public Philosopher (String philosophy) { this.philosophy = philosophy; } public void speak () { System.out.println (philosophy); public void announce (String announcement) { System.out.println (announcement); public void pontificate () { for (int count=1; count <= 5; count++)

Dog.java class Dog implements Speaker { // Prints this dog's philosophy. public void speak () { System.out.println ("woof"); } // Prints this dog's philosophy and the specified announcement. public void announce (String announcement) { System.out.println ("woof: " + announcement);

Polymorphism via Interfaces An interface name can be used as the type of an object reference variable Doable obj; The obj reference can be used to point to any object of any class that implements the Doable interface The version of doThis that the following line invokes depends on the type of object that obj is referring to: obj.doThis();

Polymorphism via Interfaces That reference is polymorphic "having many forms” That line of code might execute different methods at different times if the object that obj points to changes Careful use of polymorphic references can lead to elegant, robust software designs Dynamic binding polymorphic references must be resolved at run time; See Talking.java (page 240)

Talking.java class Talking { public static void main (String[] args) { Speaker current; current = new Dog(); current.speak(); current = new Philosopher ("I think, therefore I am."); ((Philosopher) current).pontificate(); }

Interfaces The Java standard class library contians many interfaces that are helpful in certain situations The Comparable interface contains an abstract method called compareTo, which is used to compare to objects The String class implements Comparable gives us the ability to put strings in alphabetical order The Iterator interface contains methods that allow the user to move through a collection of objects easily

Events An event is an object that represents some activity to which we may want to respond For example, we may want our program to perform some action when the following occurs: the mouse is moved a mouse button is clicked the mouse is dragged a graphical button is clicked a keyboard key is pressed a timer expires Often events correspond to user actions, but not always

Events The Java standard class library contains several classes that represent typical events Certain objects, such as an applet or a graphical button, generate (fire) an event when it occurs Other objects, called a listeners, respond to events We can write listener objects to do whatever we want when an event occurs

This object waits for and Events and Listeners Event Generator This object may generate an event Listener This object waits for and responds to an event When an event occurs, the generator calls the appropriate method of the listener, passing an object that describes the event

Listener Interfaces How to create a listener object by writing a class that implements a particular listener interface The Java class library contains several interfaces The MouseListener interface contains methods that correspond to mouse events To set up a formal relationship between the generator and listener add the listener to the component that might generate the event.

Mouse Events The following are mouse events: mouse pressed - the mouse button is pressed down mouse released - the mouse button is released mouse clicked - the mouse button is pressed and released mouse entered - the mouse pointer is moved over a particular component mouse exited - the mouse pointer is moved off of a particular component Any given program can listen for some, none, or all of these See Dots.java (page 246) See DotsMouseListener.java (page 248)

Dots.java import java.applet.Applet; import java.awt.*; // import java.awt.event.*; public class Dots extends Applet { private final int APPLET_WIDTH = 200; private final int APPLET_HEIGHT = 100; private final int RADIUS = 6; private Point clickPoint = null; public void init() { DotsMouseListener listener = new DotsMouseListener(this); addMouseListener(listener); setBackground (Color.black); setSize (APPLET_WIDTH, APPLET_HEIGHT); }

Dots.java // Draws the dot at the appropriate location. public void paint (Graphics page) { page.setColor (Color.green); if (clickPoint != null) page.fillOval (clickPoint.x - RADIUS, clickPoint.y - RADIUS, RADIUS * 2, RADIUS * 2); } // Sets the point at which to draw the next dot. public void setPoint (Point point) { clickPoint = point;

Mouse Motion Events The following are called mouse motion events: mouse moved - the mouse is moved mouse dragged - the mouse is moved while the mouse button is held down There is a corresponding MouseMotionListener interface One class can serve as both a generator and a listener One class can serve as a listener for multiple event types See RubberLines.java (page 249)

RubberLines.java import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class RubberLines extends Applet implements MouseListener, MouseMotionListener { private final int APPLET_WIDTH = 200; private final int APPLET_HEIGHT = 200; private Point point1 = null; private Point point2 = null; // Adds this class as a listener for all mouse related events. public void init() { addMouseListener (this); addMouseMotionListener (this); setBackground (Color.black); setSize (APPLET_WIDTH, APPLET_HEIGHT); }

RubberLines.java // Draws the current line from the intial mouse down point to // the current position of the mouse. public void paint (Graphics page) { page.setColor (Color.green); if (point1 != null && point2 != null) page.drawLine (point1.x, point1.y, point2.x, point2.y); } // Captures the position at which the mouse is initially pushed. public void mousePressed (MouseEvent event) { point1 = event.getPoint(); // Gets the current position of the mouse as it is dragged and // draws the line to create the rubberband effect. public void mouseDragged (MouseEvent event) { point2 = event.getPoint(); repaint();

RubberLines.java // Provide empty definitions for unused event methods. public void mouseClicked (MouseEvent event) {} public void mouseReleased (MouseEvent event) {} public void mouseEntered (MouseEvent event) {} public void mouseExited (MouseEvent event) {} public void mouseMoved (MouseEvent event) {} }

Key Events The following are called key events: key pressed - a keyboard key is pressed down key released - a keyboard key is released key typed - a keyboard key is pressed and released The KeyListener interface handles key events Listener classes are often implemented as inner classes, nested within the component that they are listening to See Direction.java (page 253)

Direction.java import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.Timer; public class Direction extends Applet { private final int APPLET_WIDTH = 200; private final int APPLET_HEIGHT = 200; private final int JUMP = 5; // increment for image movement private final int IMAGE_SIZE = 31; private Image up, down, right, left, currentImage; private AudioClip bonk; private int x, y;

Direction.java // Sets up the applet by creating listeners, loading images, etc. public void init() { requestFocus(); // make sure the applet has the keyboard focus addKeyListener (new DirectionKeyListener()); x = y = 0; up = getImage (getCodeBase(), "cyanUp.gif"); down = getImage (getCodeBase(), "cyanDown.gif"); left = getImage (getCodeBase(), "cyanLeft.gif"); right = getImage (getCodeBase(), "cyanRight.gif"); currentImage = right; bonk = getAudioClip (getCodeBase(), "bonk.au"); setBackground (Color.black); setSize (APPLET_WIDTH, APPLET_HEIGHT); } // Paints the current image in the current location. public void paint (Graphics page) { page.drawImage (currentImage, x, y, this);

Direction.java // Represents a listener for keyboard activity. private class DirectionKeyListener implements KeyListener { public void keyPressed (KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.VK_UP: currentImage = up; if (y > 0) y -= JUMP; break; case KeyEvent.VK_DOWN: currentImage = down; if (y < APPLET_HEIGHT-IMAGE_SIZE) y += JUMP; case KeyEvent.VK_LEFT: currentImage = left; if (x > 0) x -= JUMP; case KeyEvent.VK_RIGHT: currentImage = right; if (x < APPLET_WIDTH-IMAGE_SIZE) x += JUMP; default: bonk.play(); } repaint(); public void keyTyped (KeyEvent event) {} public void keyReleased (KeyEvent event) {}

Animations An animation is a constantly changing series of pictures or images that create the illusion of movement We can create animations in Java by changing a picture slightly over time The speed of a Java animation is usually controlled by a Timer object The Timer class is defined in the javax.swing package

Animations A Timer object generates and ActionEvent every n milliseconds (where n is set by the object creator) The ActionListener interface contains an actionPerformed method Whenever the timer expires (generating an ActionEvent) the animation can be updated See Rebound.java (page 258)

Rebound.java import java.applet.Applet; import java.awt.*; import java.awt.event.*; import javax.swing.Timer; public class Rebound extends Applet { private final int APPLET_WIDTH = 200; private final int APPLET_HEIGHT = 100; private final int IMAGE_SIZE = 35; private final int DELAY = 20; private Timer timer; private Image image; private int x, y, moveX, moveY;

Rebound.java // Sets up the applet, including the timer for the animation. public void init() { addMouseListener (new ReboundMouseListener()); timer = new Timer (DELAY, new ReboundActionListener()); timer.start(); x = 0; y = 40; moveX = moveY = 3; image = getImage (getCodeBase(), "happyFace.gif"); setBackground (Color.black); setSize (APPLET_WIDTH, APPLET_HEIGHT); }

Rebound.java // Draws the image in the current location. public void paint (Graphics page) { page.drawImage (image, x, y, this); } // Represents the mouse listner for the applet. private class ReboundMouseListener implements MouseListener // Stops or starts the timer (and therefore the animation) // when the mouse button is clicked. public void mouseClicked (MouseEvent event) if (timer.isRunning()) timer.stop(); else timer.start();

Rebound.java // Provide empty definitions for unused event methods. public void mouseEntered (MouseEvent event) {} public void mouseExited (MouseEvent event) {} public void mousePressed (MouseEvent event) {} public void mouseReleased (MouseEvent event) {} }

Rebound.java // Represents the action listener for the timer. private class ReboundActionListener implements ActionListener { // Updates the position of the image and possibly the direction // of movement whenever the timer fires an action event. public void actionPerformed (ActionEvent event) { x += moveX; y += moveY; if (x <= 0 || x >= APPLET_WIDTH-IMAGE_SIZE) moveX = moveX * -1; if (y <= 0 || y >= APPLET_HEIGHT-IMAGE_SIZE) moveY = moveY * -1; repaint(); }