Archive for August, 2007

Chapter 9 (Web design programs) Object-Oriented Programming 447 Software reusability saves

Saturday, August 25th, 2007

Chapter 9 Object-Oriented Programming 447 Software reusability saves time in program development. It encourages reuse of proven and debugged high-quality software, thus reducing problems after a system becomes operational. These are exciting possibilities. Polymorphism enables us to write programs in a general fashion to handle a wide variety of existing and yet-to-be-specified related classes. Polymorphism makes it easy to add new capabilities to a system. Inheritance and polymorphism are effective techniques for dealing with software complexity. When creating a new class, instead of writing completely new instance variables and instance methods, the programmer can designate that the new class is to inherit the instance variables and instance methods of a previously defined superclass. The new class is referred to as a subclass. Each subclass itself becomes a candidate to be a superclass for some future subclass. The direct superclass of a class is the superclass from which the class explicitly inherits (via the keyword extends). An indirect superclass is inherited from two or more levels up the class hierarchy. For example, class JApplet (package javax.swing) extends class Applet (package java.applet). Thus, each applet class we have defined is a direct subclass of JApplet and an indirect subclass of Applet. With single inheritance, a class is derived from one superclass. Java does not support multiple inheritance (as C++ does) but it does support the notion of interfaces. Interfaces help Java achieve many of the advantages of multiple inheritance without the associated problems. We will discuss the details of interfaces in this chapter. We consider both general principles and a detailed specific example of creating and using interfaces. A subclass normally adds instance variables and instance methods of its own, so a subclass is generally larger than its superclass. A subclass is more specific than its superclass and represents a smaller, more specialized group of objects. With single inheritance, the subclass starts out essentially the same as the superclass. The real strength of inheritance comes from the ability to define in the subclass additions to, or replacements for, the features inherited from the superclass. Every subclass object is also an object of that class s superclass. For example, every applet we have defined is considered to be an object of class JApplet. Also, because JApplet extends Applet, every applet we have defined is considered to be an Applet. This information is critical when developing applets, because an applet container can execute a program only if it is an Applet. Although a subclass object always can be treated as one of its superclass types, superclass objects are not considered to be objects of their subclass types. We will take advantage of this subclass-object-is-a-superclass-object relationship to perform some powerful manipulations. For example, a drawing application can maintain a list of shapes to display. If all the shape types extend the same superclass directly or indirectly, the drawing program can store all the shapes in an array (or other data structure) of superclass objects. As we will see in this chapter, this ability to process a set of objects as a single type is a key thrust of object-oriented programming. We add a new form of member access control in this chapter, namely protected access. Subclass methods and methods of other classes in the same package as the superclass can access protected superclass members. Experience in building software systems indicates that significant portions of the code deal with closely related special cases. It becomes difficult in such systems to see the big picture because the designer and the programmer become preoccupied with the special cases. Object-oriented programming provides several ways of seeing the forest through the trees. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

446 Object-Oriented Programming Chapter 9 Outline (Dedicated web hosting) 9.1 Introduction

Friday, August 24th, 2007

446 Object-Oriented Programming Chapter 9 Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship between Superclass Objects and Subclass Objects 9.5 Constructors and Finalizers in Subclasses 9.6 Implicit Subclass-Object-to-Superclass-Object Conversion 9.7 Software Engineering with Inheritance 9.8 Composition vs. Inheritance 9.9 Case Study: Point, Circle, Cylinder 9.10 Introduction to Polymorphism 9.11 Type Fields and switch Statements 9.12 Dynamic Method Binding 9.13 final Methods and Classes 9.14 Abstract Superclasses and Concrete Classes 9.15 Polymorphism Examples 9.16 Case Study: A Payroll System Using Polymorphism 9.17 New Classes and Dynamic Binding 9.18 Case Study: Inheriting Interface and Implementation 9.19 Case Study: Creating and Using Interfaces 9.20 Inner Class Definitions 9.21 Notes on Inner Class Definitions 9.22 Type-Wrapper Classes for Primitive Types 9.23 (Optional Case Study) Thinking About Objects: Incorporating Inheritance into the Elevator Simulation 9.24 (Optional) Discovering Design Patterns: Introducing Creational, Structural and Behavioral Design Patterns Summary Terminology Self-Review Exercises Answers to Self-Review Exercises Exercises 9.1 Introduction In this chapter, we discuss object-oriented programming (OOP) and its key component technologies inheritance and polymorphism. Inheritance is a form of software reusability in which new classes are created from existing classes by absorbing their attributes and behaviors and adding new capabilities the new classes require. Inheritance takes advantage of class relationships where objects of a certain class such as a class of vehicles have the same characteristics. Newly created classes of objects are derived by absorbing characteristics of existing classes and adding unique characteristics of their own. An object of class convertible certainly has the characteristics of the more general class automobile, but a convertible s roof goes up and down. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

9 Object-Oriented Programming (Shared web hosting) Objectives To understand inheritance

Thursday, August 23rd, 2007

9 Object-Oriented Programming Objectives To understand inheritance and software reusability. To understand superclasses and subclasses. To appreciate how polymorphism makes systems extensible and maintainable. To understand the distinction between abstract classes and concrete classes. To learn how to create abstract classes and interfaces. Say not you know another entirely, till you have divided an inheritance with him. Johann Kasper Lavater This method is to define as the number of a class the class of all classes similar to the given class. Bertrand Russell Good as it is to inherit a library, it is better to collect one. Augustine Birrell General propositions do not decide concrete cases. Oliver Wendell Holmes A philosopher of imposing stature doesn t think in a vacuum. Even his most abstract ideas are, to some extent, conditioned by what is or is not known in the time when he lives. Alfred North Whitehead Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

444 Object-Based Programming Chapter 8 The data for (Ipower web hosting)

Wednesday, August 22nd, 2007

444 Object-Based Programming Chapter 8 The data for class MyLine should include x1, y1, x2 and y2 coordinates. Method drawLine method of class Graphics will connect the two points supplied with a line. The data for classes MyRect and MyOval should include an upper-left x-coordinate value, an upper-left y-coordinate value, a width (must be nonnegative) and a height (must be nonnegative). All data in each class must be private. In addition to the data, each class should define at least the following public methods: a) A constructor with no arguments that sets the coordinates to 0. b) A constructor with arguments that sets the coordinates to the supplied values. c) Set methods for each individual piece of data that allow the programmer to independently set any piece of data in a shape (e.g., if you have an instance variable x1, you should have a method setX1). d) Get methods for each individual piece of data that allow the programmer to independent ly retrieve any piece of data in a shape (e.g., if you have an instance variable x1, you should have a method getX1). e) A draw method with the first line public void draw( Graphics g ) will be called from the applet s paint method to draw a shape onto the screen. The preceding methods are required. If you would like to provide more methods for flexibility, please do so. Begin by defining class MyLine and an applet to test your classes. The applet should have a MyLine instance variable line that can refer to one MyLine object (created in the applet s init method with random coordinates). The applet s paint method should draw the shape with a statement like line.draw( g ); where line is the MyLine reference and g is the Graphicsobject that the shape will use to draw itself on the applet. Next, change the single MyLine reference into an array of MyLine references and hard code several MyLine objects into the program for drawing. The applet s paint method should walk through the array of MyLine objects and draw every one. After the preceding part is working, you should define the MyOval and MyRect classes and add objects of these classes into the MyRect and MyOval arrays. The applet s paint method should walk through each array and draw every shape. Create five shapes of each type. Once the applet is running, select Reload from the appletviewer s Applet menu to reload the applet. This will cause the applet to choose new random numbers for the shapes and draw the shapes again. In Chapter 9, we will modify this exercise to take advantage of the similarities between the classes and to avoid reinventing the wheel. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Chapter 8 Object-Based Programming 443 8.15 Create a (Cheapest web hosting)

Tuesday, August 21st, 2007

Chapter 8 Object-Based Programming 443 8.15 Create a Date class with the following capabilities: a) Output the date in multiple formats such as MM/DD/YYYY June 14, 1992 DDD YYYY b) Use overloaded constructors to create Date objects initialized with dates of the formats in part a). [Hint: You can compare Strings using method equals. Suppose you have two String references s1 and s2, if those Strings are equal, s1.equals( s2 ) returns true; otherwise, it returns false.] 8.16 Create class SavingsAccount. Use a static class variable to store the annualInterestRate for all account holders. Each object of the class contains a private instance variable savingsBalance indicating the amount the saver currently has on deposit. Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalanceby annualInterestRatedivided by 12; this interest should be added to savingsBalance. Provide a static method modifyInterestRate that sets the annualInterestRate to a new value. Write a driver program to test class SavingsAccount. Instantiate two savingsAccount objects, saver1and saver2, with balances of $2000.00 and $3000.00, respectively. Set annualInterestRate to 4%, then calculate the monthly interest and print the new balances for each of the savers. Then set the annualInterestRate to 5% and calculate the next month s interest and print the new balances for each of the savers. 8.17 Create class IntegerSet. Each object of the class can hold integers in the range 0 through 100. A set is represented internally as an array of booleans. Array element a[i]is true if integer i is in the set. Array element a[j]is false if integer j is not in the set. The no-argument constructor initializes a set to the so-called empty set (i.e., a set whose array representation contains all false values). Provide the following methods: Method unionOfIntegerSets creates a third set which is the set-theoretic union of two existing sets (i.e., an element of the third set s array is set to trueif that element is truein either or both of the existing sets; otherwise, the element of the third set is set to false). Method intersectionOfIntegerSets creates a third set which is the set-theoretic intersection of two existing sets i.e., an element of the third set s array is set to false if that element is false in either or both of the existing sets; otherwise, the element of the third set is set to true). Method insertElement inserts a new integer k into a set (by setting a[k]to true). Method deleteElement deletes integer m (by setting a[m] to false). Method setPrint prints a set as a list of numbers separated by spaces. Print only those elements that are present in the set. Print —for an empty set. Method isEqualTo determines if two sets are equal. Write a program to test your IntegerSet class. Instantiate several IntegerSet objects. Test that all your methods work properly. 8.18 It would be perfectly reasonable for the Time1 class of Figure 8.1 to represent the time internally as the number of seconds since midnight rather than the three integer values hour, minute and second. Clients could use the same public methods and get the same results. Modify the Time1 class of Figure 8.1 to implement the Time1 as the number of seconds since midnight and show that there is no change visible to the clients of the class. 8.19 (Drawing Program) Create a drawing applet that randomly draws lines, rectangles and ovals. For this purpose, create a set of smart shape classes where objects of these classes know how to draw themselves if provided with a Graphics object that tells them where to draw (i.e., the applet s Graphics object allows a shape to draw on the applet s background). The class names should be MyLine, MyRectand MyOval. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Web server address - 442 Object-Based Programming Chapter 8 8.6 Combine the

Tuesday, August 21st, 2007

442 Object-Based Programming Chapter 8 8.6 Combine the modified Time3 class of Exercise 8.5 and the modified Date class of Exercise 8.5 into one class called DateAndTime. Modify the tick method to call the nextDay method if the time is incremented into the next day. Modify methods toString and toUniversal- String() to output the date in addition to the time. Write a driver program to test the new class DateAndTime. Specifically test incrementing the time to the next day. 8.7 Modify the set methods in class Time3 of Fig. 8.8 to return appropriate error values if an attempt is made to set one of the instance variables hour, minuteor second of an object of class Timeto an invalid value. (Hint: Use boolean return types on each method.) 8.8 Create a class Rectangle. The class has attributes lengthand width, each of which defaults to 1. It has methods that calculate the perimeter and the area of the rectangle. It has set and get methods for both length and width. The set methods should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0. Write a program to test class Rectangle. 8.9 Create a more sophisticated Rectangle class than the one you created in Exercise 8.8. This class stores only the Cartesian coordinates of the four corners of the rectangle. The constructor calls a set method that accepts four sets of coordinates and verifies that each of these is in the first quadrant with no single x-or y-coordinate larger than 20.0. The set method also verifies that the supplied coordinates do, in fact, specify a rectangle. Provide methods to calculate the length, width, perimeter and area. The length is the larger of the two dimensions. Include a predicate method isSquare which determines whether the rectangle is a square. Write a program to test class Rectangle. 8.10 Modify the Rectangle class of Exercise 8.9 to include a draw method that displays the rectangle inside a 25-by-25 box enclosing the portion of the first quadrant in which the rectangle resides. Use the methods of the Graphics class to help output the Rectangle. If you feel ambitious, you might include methods to scale the size of the rectangle, rotate it and move it around within the designated portion of the first quadrant. 8.11 Create a class HugeInteger which uses a 40-element array of digits to store integers as large as 40 digits each. Provide methods inputHugeInteger, outputHugeInteger, add- HugeIntegers and substractHugeIntegers. For comparing HugeInteger objects, provide methods isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, IsGreater- ThanOrEqualTo and isLessThanOrEqualTo each of these is a predicate method that simply returns true if the relationship holds between the two HugeIntegers and returns false if the relationship does not hold. Provide a predicate method isZero. If you feel ambitious, also provide the method multiplyHugeIntegers, the method divideHugeIntegers and the method modulusHugeIntegers. 8.12 Create a class TicTacToe that will enable you to write a complete program to play the game of Tic-Tac-Toe. The class contains as private data a 3-by-3 double array of integers. The constructor should initialize the empty board to all zeros. Allow two human players. Wherever the first player moves, place a 1 in the specified square; place a 2 wherever the second player moves. Each move must be to an empty square. After each move determine whether the game has been won and whether the game is a draw. If you feel ambitious, modify your program so that the computer makes the moves for one of the players automatically. Also, allow the player to specify whether he or she wants to go first or second. If you feel exceptionally ambitious, develop a program that will play three-dimensional Tic-Tac-Toe on a 4-by-4-by-4 board [Note: This is a challenging project that could take many weeks of effort!]. 8.13 Explain the notion of package access in Java. Explain the negative aspects of package access as described in the text. 8.14 What happens when a return type, even void, is specified for a constructor? Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Chapter 8 Object-Based Programming 441 ment constructor with (Make my own web site)

Monday, August 20th, 2007

Chapter 8 Object-Based Programming 441 ment constructor with default values in case no initializers are provided. Provide public methods for each of the following: a) Addition of two Complexnumbers: The real parts are added together and the imaginary parts are added together. b) Subtraction of two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand and the imaginary part of the right operand is subtracted from the imaginary part of the left operand. c) Printing Complex numbers in the form (a,b), where a is the real part and b is the imaginary part. 8.3 Create a class called Rational for performing arithmetic with fractions. Write a driver program to test your class. Use integer variables to represent the private instance variables of the class the numerator and the denominator. Provide a constructor method that enables an object of this class to be initialized when it is declared. The constructor should store the fraction in reduced form (i.e., the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator). Provide a no- argument constructor with default values in case no initializers are provided. Provide public meth ods for each of the following: a) Addition of two Rational numbers. The result of the addition should be stored in reduced form. b) Subtraction of two Rational numbers. The result of the subtraction should be stored in reduced form. c) Multiplication of two Rational numbers. The result of the multiplication should be stored in reduced form. d) Division of two Rational numbers. The result of the division should be stored in reduced form. e) Printing Rational numbers in the form a/b, where a is the numeratorand b is the denominator. f) Printing Rational numbers in floating-point format. (Consider providing formatting capabilities that enable the user of the class to specify the number of digits of precision to the right of the decimal point.) 8.4 Modify the Time3 class of Fig. 8.8 to include the tick method that increments the time stored in a Time3 object by one second. Also provide method incrementMinute to increment the minute and method incrementHour to increment the hour. The Time3 object should always remain in a consistent state. Write a driver program that tests the tick method, the increment- Minute method and the incrementHour method to ensure that they work correctly. Be sure to test the following cases: a) incrementing into the next minute. b) incrementing into the next hour. c) incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM). 8.5 Modify the Date class of Fig. 8.13 to perform error-checking on the initializer values for instance variables month, day and year (currently it validates only the month and day). Also, provide a method nextDay to increment the day by one. The Date object should always remain in a consistent state. Write a driver program that tests the nextDay method in a loop that prints the date during each iteration of the loop to illustrate that the nextDay method works correctly. Be sure to test the following cases: a) incrementing into the next month. b) incrementing into the next year. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Web server - 440 Object-Based Programming Chapter 8 object-based programming (OBP)

Sunday, August 19th, 2007

440 Object-Based Programming Chapter 8 object-based programming (OBP) rapid applications development (RAD) object-oriented programming (OOP) reusable code package access services of a class package statement set method predicate method software reusability principle of least privilege static class variable private static method programmer-defined type this reference public user-defined type public interface of a class utility method query method SELF-REVIEW EXERCISES 8.1 Fill in the blanks in each of the following statements: a) Class members are accessed via the operator in conjunction with a reference to an object of the class. b) Members of a class specified as are accessible only to methods of the class. c) A is a special method used to initialize the instance variables of a class. d) A method is used to assign values to private instance variables of a class. e) Methods of a class are normally made and instance variables of a class are normally made . f) A method is used to retrieve values of privatedata of a class. g) The keyword introduces a class definition. h) Members of a class specified as are accessible anywhere an object of the class is in scope. i) The operator dynamically allocates memory for an object of a specified type and returns a to that type. j) A instance variable represents class-wide information. k) The keyword specifies that an object or variable is not modifiable after it is initialized. l) A method declared static cannot access class members. ANSWERS TO SELF-REVIEW EXERCISES 8.1 a) dot (.). b) private. c) constructor. d) set. e) public, private. f) get. g) class. h) public. i) new, reference. j) static. k) final. l) nonstatic. EXERCISES 8.2 Create a class called Complex for performing arithmetic with complex numbers. Write a driver program to test your class. Complex numbers have the form realPart + imaginaryPart * i where i is -1 Use floating-point variables to represent the private data of the class. Provide a constructor method that enables an object of this class to be initialized when it is declared. Provide a no-argu Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Web hosts - Chapter 8 Object-Based Programming 439 If a

Saturday, August 18th, 2007

Chapter 8 Object-Based Programming 439 If a program uses multiple classes from the same package, these classes can access each other s package-access methods and data directly through a reference to an object. Each object has access to a reference to itself called the this reference that can be used inside the methods of the class to refer to the object s data and other methods explicitly. Any time you have a reference in a program (even as the result of a method call), the reference can be followed by a dot operator and a call to one of the methods for the reference type. Java performs automatic garbage collection of memory. When an object is no longer used in the program (i.e., there are no references to the object), the object is marked for garbage collection. Every class in Java can have a finalizer method that returns resources to the system. A class s finalizer method always has the name finalize, receives no parameters and returns no value. Method finalize is originally defined in class Objectas a placeholder that does nothing. This guarantees that every class has a finalizemethod for the garbage collector to call. A static class variable represents class-wide information all objects of the class share the same piece of data. A class s publicstatic members can be accessed through a reference to any object of that class, or they can be accessed through the class name using the dot operator. publicstaticmethod gc from class System suggests that the garbage collector immediately make a best effort attempt to collect garbage objects.The garbage collector is not guaranteed to collect objects in a specific order. A method declared static cannot access nonstatic class members. Unlike nonstatic methods, a static method has no this reference, because static class variables and static class methods exist independent of any objects of a class. static class members exist even when no objects of that class exist they are available as soon as the class is loaded into memory at execution time. TERMINOLOGY abstract data type (ADT) extends access method extensibility aggregation finalizer attribute get method behavior helper method cascaded method calls implementation of a class class information hiding class definition initialize a class object class library instance method class method (static) instance of a class class scope instance variable class variable instantiate an object of a class client of a class interface to a class composition member access control concatenated method calls member access modifiers consistent state for an instance variable member access operator (.) constructor message container class method -dcompiler option method calls data type mutator method default constructor new operator dot operator (.) no-argument constructor encapsulation object Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

438 Object-Based Programming Chapter 8 A constructor

Saturday, August 18th, 2007

438 Object-Based Programming Chapter 8 A constructor is a method with the exact same name as the class that initializes the instance variables of an object of the class when the object is instantiated. Constructor methods can be overloaded for a class. Constructors can take arguments but cannot specify a return value type. Constructors and other methods that change instance variable values should always maintain objects in a consistent state. Method toString takes no arguments and returns a String. The original toStringmethod of class Objectis a placeholder that is normally redefined by a subclass. When an object is instantiated, operator new allocates the memory for the object, then new calls the constructor for the class to initialize the instance variables of the object. If the .class files for the classes used in a program are in the same directory as the class that uses them, import statements are not required. Concatenating a String and any object results in an implicit call to the object s toString method to convert the object to a String, then the Strings are concatenated. Within a class s scope, class members are accessible to all of that class s methods and can be referenced simply by name. Outside a class s scope, class members can only be accessed off a handle (i.e., a reference to an object of the class). If a method defines a variable with the same name as a variable with class scope, the class-scope variable is hidden by the method-scope variable in the method. A hidden instance variable can be accessed in the method by preceding its name with the keyword this and the dot operator. Each class and interface in the Java API belongs to a specific package that contains a group of related classes and interfaces. Packages are actually directory structures used to organize classes and interfaces. Packages provide a mechanism for software reuse and a convention for unique class names. Creating a reusable class requires: defining a public class, adding a package statement to the class definition file, compiling the class into the appropriate package directory structure and importing the class into a program. When compiling a class in a package, the option -d must be passed to the compiler to specify where to create (or locate) all the directories in the package statement. The package directory names become part of the class name when the class is compiled. Use this fully qualified name in programs or import the class and use its short name (the name of the class by itself) in the program. If no constructors are defined for a class, the compiler creates a default constructor. When one object of a class has a reference to another object of the same class, the first object can access all the second object s data and methods. Classes often provide public methods to allow clients of the class to set (i.e., assign values to) or get (i.e., obtain the values of) private instance variables. Get methods are also commonly called accessor methods or query methods. Set methods are also commonly called mutator methods (because they typically change a value). Every event has a source the GUI component with which the user interacted to signal the program to do a task. Use the keyword finalto specify that a variable is not modifiable and that any attempt to modify the variable is an error. A finalvariable cannot be modified by assignment after it is initialized. Such a variable must be initialized in its declaration or in every constructor of the class. With composition, a class has references to objects of other classes as members. When no member access modifier is provided for a method or variable when it is defined in a class, the method or variable is considered to have package access. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01