Archive for September, 2007

Chapter 9 Object-Oriented Programming 495 1 // Fig.

Sunday, September 30th, 2007

Chapter 9 Object-Oriented Programming 495 1 // Fig. 9.27: Shape.java 2 // Definition of interface Shape 3 4 public interface Shape { 5 6 // calculate area 7 public abstract double area(); 8 9 // calculate volume 10 public abstract double volume(); 11 12 // return shape name 13 public abstract String getName(); 14 } Fig. 9.27 Point, circle, cylinder hierarchy with a Shapeinterface. 1 // Fig. 9.28: Point.java 2 // Definition of class Point 3 4 public class Point extends Object implements Shape { 5 protected int x, y; // coordinates of the Point 6 7 // no-argument constructor 8 public Point() 9 { 10 setPoint( 0, 0 ); 11 } 12 13 // constructor 14 public Point( int xCoordinate, int yCoordinate ) 15 { 16 setPoint( xCoordinate, yCoordinate ); 17 } 18 19 // Set x and y coordinates of Point 20 public void setPoint( int xCoordinate, int yCoordinate ) 21 { 22 x = xCoordinate; 23 y = yCoordinate; 24 } 25 26 // get x coordinate 27 public int getX() 28 { 29 return x; 30 } 31 Fig. 9.28 Pointimplementation of interface Shape(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

494 Object-Oriented Programming Chapter (Disney web site) 9 0. Finally, the

Saturday, September 29th, 2007

494 Object-Oriented Programming Chapter 9 0. Finally, the string “Cylinder:”, the coordinates of object cylinder, the radius of object cylinder and the height of object cylinder (arrayOfShapes[2]) are output; the area of cylinder is calculated and the volume of cylinder is calculated. All the method calls to getName, toString, area and volume are resolved at runtime with dynamic binding. 9.19 Case Study: Creating and Using Interfaces Our next example (Fig. 9.27 Fig. 9.31) reexamines the Point, Circle, Cylinderhierarchy one last time, replacing abstract superclass Shape with the interface Shape (Fig. 9.27). An interface definition begins with the keyword interface and contains a set of publicabstract methods. Interfaces may also contain publicstaticfinal data. To use an interface, a class must specify that it implements the interface and the class must define every method in the interface with the number of arguments and the return type specified in the interface definition. If the class leaves any method of the interface undefined, the class becomes an abstract class and must be declared abstract in the first line of its class definition. Implementing a interface is like signing a contract with the compiler that states, I will define all the methods specified by the interface. Common Programming Error 9.9 Leaving a method of an interface undefined in a class that implements the interface results in a compile error indicating that the class must be declared abstract. Software Engineering Observation 9.30 Declaring a final reference indicates that the reference always refers to the same object. However, this does not affect the object to which the reference refers the object s data still can be modified. We started using the concept of an interface when we introduced GUI event handling in Chapter 6, Methods. Recall that our applet class included implements Action- Listener (an interface in package java.awt.event). The reason we were required to define actionPerformed in the applets with event handling is that ActionListener is an interface that specifies that actionPerformed must be defined. Interfaces are an important part of GUI event handling, as we will discuss in the next section. An interface is typically used in place of an abstract class when there is no default implementation to inherit i.e., no instance variables and no default method implementations. Like publicabstract classes, interfaces are typically public data types, so they are normally defined in files by themselves with the same name as the interface and the .java extension. The definition of interface Shape begins in Fig. 9.27 at line 4. Interface Shape has abstract methods area, volume and getName. By coincidence, all three methods take no arguments. However, this is not a requirement of methods in an interface. In Fig. 9.28, line 4 indicates that class Point extends class Object and implements interface Shape. Class Point provides definitions of all three methods in the interface. Method area is defined at lines 45 48. Method volume is defined at lines 51 54. Method getName is defined at lines 57 60. These three methods satisfy the implementation requirement for the three methods defined in the interface. We have fulfilled our contract with the compiler. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Shared web hosting - Chapter 9 Object-Oriented Programming 493 54 55 System.exit(

Friday, September 28th, 2007

Chapter 9 Object-Oriented Programming 493 54 55 System.exit( 0 ); 56 } 57 58 } // end class Test Fig. 9.26 Shape, Point, Circle, Cylinderhierarchy (part 2 of 2). Lines 33 through 36 invoke methods getNameand toString to illustrate that the objects are initialized correctly (as in the first three lines of the screen capture). Next, the forstructure at lines 42 49 walks through arrayOfShapes, and the following calls are made during each iteration of the loop: arrayOfShapes[ i ].getName() arrayOfShapes[ i ].toString() arrayOfShapes[ i ].area() arrayOfShapes[ i ].volume() Each of these method calls is invoked on the object to which arrayOfShapes[i] currently refers. When the compiler looks at each of these calls, it is simply trying to determine whether a Shapereference (arrayOfShapes[ i]) can be used to call these methods. For methods getName, areaand volumethe answer is yes, because each of these methods is defined in class Shape. For method toString, the compiler first looks at class Shapeto determine that toStringis not defined there, then the compiler proceeds to Shape s superclass (Object) to determine whether Shape inherited a toString method that takes no arguments (as it did, because all Objects have a toStringmethod). The screen capture illustrates that all four methods are invoked properly based on the type of the referenced object. First, the string “Point:”and the coordinates of the object point (arrayOfShapes[0]) are output; the area and volume are both 0. Next, the string “Circle:”, the coordinates of object circle, and the radius of object circle (arrayOfShapes[1]) are output; the area of circleis calculated and the volume is Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web server iis - 492 Object-Oriented Programming Chapter 9 1 // Fig.

Friday, September 28th, 2007

492 Object-Oriented Programming Chapter 9 1 // Fig. 9.26: Test.java 2 // Class to test Shape, Point, Circle, Cylinder hierarchy 3 4 // Java core packages 5 import java.text.DecimalFormat; 6 7 // Java extension packages 8 import javax.swing.JOptionPane; 9 10 public class Test { 11 12 // test Shape hierarchy 13 public static void main( String args[] ) 14 { 15 // create shapes 16 Point point = new Point( 7, 11 ); 17 Circle circle = new Circle( 3.5, 22, 8 ); 18 Cylinder cylinder = new Cylinder( 10, 3.3, 10, 10 ); 19 20 // create Shape array 21 Shape arrayOfShapes[] = new Shape[ 3 ]; 22 23 // aim arrayOfShapes[ 0 ] at subclass Point object 24 arrayOfShapes[ 0 ] = point; 25 26 // aim arrayOfShapes[ 1 ] at subclass Circle object 27 arrayOfShapes[ 1 ] = circle; 28 29 // aim arrayOfShapes[ 2 ] at subclass Cylinder object 30 arrayOfShapes[ 2 ] = cylinder; 31 32 // get name and String representation of each shape 33 String output = 34 point.getName() + “: ” + point.toString() + “n” + 35 circle.getName() + “: ” + circle.toString() + “n” + 36 cylinder.getName() + “: ” + cylinder.toString(); 37 38 DecimalFormat precision2 = new DecimalFormat( “0.00″ ); 39 40 // loop through arrayOfShapes and get name, 41 // area and volume of each shape in arrayOfShapes 42 for ( int i = 0; i < arrayOfShapes.length; i++ ) { 43 output += “nn” + arrayOfShapes[ i ].getName() + 44 “: ” + arrayOfShapes[ i ].toString() + 45 “nArea = ” + 46 precision2.format( arrayOfShapes[ i ].area() ) + 47 “nVolume = ” + 48 precision2.format( arrayOfShapes[ i ].volume() ); 49 } 50 51 JOptionPane.showMessageDialog( null, output, 52 “Demonstrating Polymorphism”, 53 JOptionPane.INFORMATION_MESSAGE ); Fig. 9.26 Shape, Point, Circle, Cylinderhierarchy (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 9 Object-Oriented Programming 491 21 (How to cite a web site) 22 setHeight(

Thursday, September 27th, 2007

Chapter 9 Object-Oriented Programming 491 21 22 setHeight( cylinderHeight ); 23 } 24 25 // set height of Cylinder 26 public void setHeight( double cylinderHeight ) 27 { 28 height = ( cylinderHeight >= 0 ? cylinderHeight : 0 ); 29 } 30 31 // get height of Cylinder 32 public double getHeight() 33 { 34 return height; 35 } 36 37 // calculate area of Cylinder (i.e., surface area) 38 public double area() 39 { 40 return 2 * super.area() + 2 * Math.PI * radius * height; 41 } 42 43 // calculate volume of Cylinder 44 public double volume() 45 { 46 return super.area() * height; 47 } 48 49 // convert Cylinder to a String representation 50 public String toString() 51 { 52 return super.toString() + “; Height = ” + height; 53 } 54 55 // return shape name 56 public String getName() 57 { 58 return “Cylinder”; 59 } 60 61 } // end class Cylinder Fig. 9.25 Cylindersubclass of Circle indirect subclass of abstract class Shape(part 2 of 2). Method mainof class Test(Fig. 9.26) instantiates Pointobject point, Circle object circleand Cylinderobject cylinder(lines 16 18). Next, array arrayOf- Shapes is instantiated (line 21). This array of superclass Shape references will refer to each subclass object instantiated. At line 24, the reference pointis assigned to array element arrayOfShapes[0]. At line 27, the reference circleis assigned to array element arrayOfShapes[1]. At line 30, the reference cylinder is assigned to array element arrayOfShapes[2]. Now, each superclass Shape reference in the array refers to a subclass object of type Point, Circleor Cylinder. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Net web server - 490 Object-Oriented Programming Chapter 9 42 // convert

Wednesday, September 26th, 2007

490 Object-Oriented Programming Chapter 9 42 // convert Circle to a String represention 43 public String toString() 44 { 45 return “Center = ” + super.toString() + 46 “; Radius = ” + radius; 47 } 48 49 // return shape name 50 public String getName() 51 { 52 return “Circle”; 53 } 54 55 } // end class Circle Fig. 9.24 Circlesubclass of Point indirect subclass of abstractclass Shape(part 2 of 2). Software Engineering Observation 9.29 A subclass always inherits the most recently defined version of each public and protected method from its direct and indirect superclasses. Class Cylinder (Fig. 9.25) is derived from Circle. A Cylinder has area and volume different from those of class Circle, so the areaand volumemethods are both overridden. Method getName is an implementation of the abstract method in the superclass. If this method had not been overridden here, the Circleversion of getName would be inherited. Other methods include setHeight to assign a new height to a Cylinderand getHeightto return the heightof a Cylinder. 1 // Fig. 9.25: Cylinder.java 2 // Definition of class Cylinder. 3 4 public class Cylinder extends Circle { 5 protected double height; // height of Cylinder 6 7 // no-argument constructor 8 public Cylinder() 9 { 10 // implicit call to superclass constructor here 11 setHeight( 0 ); 12 } 13 14 // constructor 15 public Cylinder( double cylinderHeight, 16 double cylinderRadius, int xCoordinate, 17 int yCoordinate ) 18 { 19 // call superclass constructor 20 super( cylinderRadius, xCoordinate, yCoordinate ); Fig. 9.25 Cylindersubclass of Circle indirect subclass of abstract class Shape(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 9 Object-Oriented Programming (Cpanel web hosting) 489 Class Circle(Fig. 9.24)

Wednesday, September 26th, 2007

Chapter 9 Object-Oriented Programming 489 Class Circle(Fig. 9.24) is derived from Point. A Circlehas a volume of 0.0, so superclass method volume is not overridden it is inherited from class Point, which inherited it from Shape. A Circle has an area different from that of a Point, so the area method is overridden. Method getName is an implementation of the abstract method in the superclass. If this method is not overridden here, the Pointversion of get- Name would be inherited. Other methods include setRadius to assign a new radius to a Circleand getRadiusto return the radiusof a Circle. 1 // Fig. 9.24: Circle.java 2 // Definition of class Circle 3 4 public class Circle extends Point { // inherits from Point 5 protected double radius; 6 7 // no-argument constructor 8 public Circle() 9 { 10 // implicit call to superclass constructor here 11 setRadius( 0 ); 12 } 13 14 // constructor 15 public Circle( double circleRadius, int xCoordinate, 16 int yCoordinate ) 17 { 18 // call superclass constructor 19 super( xCoordinate, yCoordinate ); 20 21 setRadius( circleRadius ); 22 } 23 24 // set radius of Circle 25 public void setRadius( double circleRadius ) 26 { 27 radius = ( circleRadius >= 0 ? circleRadius : 0 ); 28 } 29 30 // get radius of Circle 31 public double getRadius() 32 { 33 return radius; 34 } 35 36 // calculate area of Circle 37 public double area() 38 { 39 return Math.PI * radius * radius; 40 } 41 Fig. 9.24 Circlesubclass of Point indirect subclass of abstractclass Shape(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web hosting services - 488 Object-Oriented Programming Chapter 9 1 // Fig.

Tuesday, September 25th, 2007

488 Object-Oriented Programming Chapter 9 1 // Fig. 9.23: Point.java 2 // Definition of class Point 3 4 public class Point extends Shape { 5 protected int x, y; // coordinates of the Point 6 7 // no-argument constructor 8 public Point() 9 { 10 setPoint( 0, 0 ); 11 } 12 13 // constructor 14 public Point( int xCoordinate, int yCoordinate ) 15 { 16 setPoint( xCoordinate, yCoordinate ); 17 } 18 19 // set x and y coordinates of Point 20 public void setPoint( int xCoordinate, int yCoordinate ) 21 { 22 x = xCoordinate; 23 y = yCoordinate; 24 } 25 26 // get x coordinate 27 public int getX() 28 { 29 return x; 30 } 31 32 // get y coordinate 33 public int getY() 34 { 35 return y; 36 } 37 38 // convert point into String representation 39 public String toString() 40 { 41 return “[”+ x + “, ” + y + “]”; 42 } 43 44 // return shape name 45 public String getName() 46 { 47 return “Point”; 48 } 49 50 } // end class Point Fig. 9.23 Pointsubclass of abstract class Shape. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web design course - 484 Object-Oriented Programming Chapter 9 1 CommissionWorker commisionWorker

Tuesday, September 18th, 2007

484 Object-Oriented Programming Chapter 9 1 CommissionWorker commisionWorker = 2 new CommissionWorker( 3 “Sue”, “Jones”, 400.0, 3.0, 150 ); 4 5 PieceWorker pieceWorker = 6 new PieceWorker( “Bob”, “Lewis”, 2.5, 200 ); 7 8 HourlyWorker hourlyWorker = 9 new HourlyWorker( “Karen”, “Price”, 13.75, 40 ); 10 11 DecimalFormat precision2 = new DecimalFormat( “0.00″ ); 12 13 // Employee reference to a Boss 14 employee = boss; 15 16 output += employee.toString() + ” earned $” + 17 precision2.format( employee.earnings() ) + “n” + 18 boss.toString() + ” earned $” + 19 precision2.format( boss.earnings() ) + “n”; 20 21 // Employee reference to a CommissionWorker 22 employee = commissionWorker; 23 24 output += employee.toString() + ” earned $” + 25 precision2.format( employee.earnings() ) + “n” + 26 commissionWorker.toString() + ” earned $” + 27 precision2.format( 28 commissionWorker.earnings() ) + “n”; 29 30 // Employee reference to a PieceWorker 31 employee = pieceWorker; 32 33 output += employee.toString() + ” earned $” + 34 precision2.format( employee.earnings() ) + “n” + 35 pieceWorker.toString() + ” earned $” + 36 precision2.format( pieceWorker.earnings() ) + “n”; 37 38 // Employee reference to an HourlyWorker 39 employee = hourlyWorker; 40 41 output += employee.toString() + ” earned $” + 42 precision2.format( employee.earnings() ) + “n” + 43 hourlyWorker.toString() + ” earned $” + 44 precision2.format( hourlyWorker.earnings() ) + “n”; 45 46 JOptionPane.showMessageDialog( null, output, 47 “Demonstrating Polymorphism”, 48 JOptionPane.INFORMATION_MESSAGE ); 49 50 System.exit( 0 ); 51 } 52 53 } // end class Test Fig. 9.21 Testing the Employeeclass hierarchy using an abstractsuperclass. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Mac os x web server - Chapter 9 Object-Oriented Programming 483 16 17 //

Tuesday, September 18th, 2007

Chapter 9 Object-Oriented Programming 483 16 17 // Set the wage 18 public void setWage( double wagePerHour ) 19 { 20 wage = ( wagePerHour > 0 ? wagePerHour : 0 ); 21 } 22 23 // Set the hours worked 24 public void setHours( double hoursWorked ) 25 { 26 hours = ( hoursWorked >= 0 && hoursWorked < 168 ? 27 hoursWorked : 0 ); 28 } 29 30 // Get the HourlyWorker’s pay 31 public double earnings() { return wage * hours; } 32 33 public String toString() 34 { 35 return “Hourly worker: ” + super.toString(); 36 } 37 38 } // end class HourlyWorker Fig. 9.20 HourlyWorkerextends abstractclass Employee(part 2 of 2). Method main of the Test application (Fig. 9.21) begins by declaring Employee reference, ref. Each of the types of Employees is handled similarly in main, so we will discuss only the case in which maindeals with a Bossobject. 1 // Fig. 9.21: Test.java 2 // Driver for Employee hierarchy 3 4 // Java core packages 5 import java.text.DecimalFormat; 6 7 // Java extension packages 8 import javax.swing.JOptionPane; 9 10 public class Test { 11 12 // test Employee hierarchy 13 public static void main( String args[] ) 14 { 15 Employee employee; // superclass reference 16 String output = “”; 17 18 Boss boss = new Boss( “John”, “Smith”, 800.0 ); 19 Fig. 9.21 Testing the Employeeclass hierarchy using an abstractsuperclass. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01