Chapter 12 Reusable Classes and Packages 2000 McGraw-Hl‖ Introduction to Object-Oriented Programming with Java-Wu Chapter 12-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 1 Chapter 12 Reusable Classes and Packages
Chapter 12 objectives After you have read and studied this chapter, you should be able to e Describe four different object categories and use them effectively in designing classes e Define a package and place reusable classes in it e Write a method that calls the superclass's method explicitly by using the reserved word super e Define overloaded methods C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 12-2
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 2 Chapter 12 Objectives After you have read and studied this chapter, you should be able to Describe four different object categories and use them effectively in designing classes. Define a package and place reusable classes in it. Write a method that calls the superclass’s method explicitly by using the reserved word super. Define overloaded methods
Reusability r We say a class is reusable if it can be used in different programs Classes in the javabook package is highly reusable r Not all classes have to be reusable. We may design a class specifically for a given program. Such class is not reusable r We must design the classes accordingly. If we intend the class to be reusable, then we need to design it so that the class is indeed reusable r One way to increase the reusability is to design a class that implements a single well-defined task C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 12-3
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 3 Reusability We say a class is reusable if it can be used in different programs. Classes in the javabook package is highly reusable. Not all classes have to be reusable. We may design a class specifically for a given program. Such class is not reusable. We must design the classes accordingly. If we intend the class to be reusable, then we need to design it so that the class is indeed reusable. One way to increase the reusability is to design a class that implements a single well-defined task
Object Categories r To aid in designing a single-task class, we divide object tasks into four categories o A user interface object handles the interaction between the end user and the program o A controller object supervises other objects in the program An appllication logic object captures the logic of the real world for which the program is written o a storage object takes care of file input and output. C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12-4
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 4 Object Categories To aid in designing a single-task class, we divide object tasks into four categories: A user interface object handles the interaction between the end user and the program. A controller object supervises other objects in the program. An application logic object captures the logic of the real world for which the program is written. A storage object takes care of file input and output
Method Overriding r Two techniques to make a class more reusable are method overriding and method overloading r We say a method of a subclass overrides the inherited method when we redefine the method in the subclass Example: setvisible of drawing board overrides setvisible of MainWindow public class DrawingBoard extends MainWindow public void setvisible( boolean state The reserved word super setVisible( state )i super refers to the graphic= getGr aphics()i superclass } C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12-5
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 5 Method Overriding Two techniques to make a class more reusable are method overriding and method overloading. We say a method of a subclass overrides the inherited method when we redefine the method in the subclass. Example: setVisible of DrawingBoard overrides setVisible of MainWindow public class DrawingBoard extends MainWindow { . . . public void setVisible( boolean state ) { super.setVisible( state ); graphic = getGraphics( ); } . . . } The reserved word super refers to the superclass
Method Overloading r The signature of a method is determined by the method name and the number and data types of parameters. r If multiple methods have the same method name but different signatures the method name is overloaded and we call these methods overloaded public void drawRectangle( int x, int y, int width, int length public void drawRectangle( Rectangle rect drawRectangle is overloaded C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 12-6
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 6 Method Overloading The signature of a method is determined by the method name and the number and data types of parameters. If multiple methods have the same method name but different signatures, the method name is overloaded and we call these methods overloaded. public void drawRectangle( int x, int y, int width, int length ) { . . . } public void drawRectangle( Rectangle rect ) { . . . } drawRectangle is overloaded
Overloading constructors r As we can overload regular methods we can overload constructors a class with multiple constructors allows the programmer a flexibility in creating instances. This improves the usability of the class public Drawing Board( this(DRAWING board")i This calls the other constructor public DrawingBoard( string title setTitle( title setResizable( false setBackground( Color. white )i C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12-7
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 7 Overloading Constructors As we can overload regular methods, we can overload constructors. A class with multiple constructors allows the programmer a flexibility in creating instances. This improves the usability of the class. public DrawingBoard( ) { this( “DRAWING BOARD”); } public DrawingBoard( String title ) { setTitle( title ); setResizable( false ); setBackground( Color.white ); } This calls the other constructor
Reusable eggypeggy r Problem statement Write a Class that converts a given string to an Eggy-peggy string by placing the programmer-specified word in front of all vowels in the given string. If the programmer does not specify the word, then the default prefix egg"will be used. r Capabilities e The class is not tied to any particular program or user interface e The class provides an application logic of playing the Eggy-Peggy game. C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12-8
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 8 Reusable EggyPeggy Problem Statement Write a class that converts a given string to an Eggy-Peggy string by placing the programmer-specified word in front of all vowels in the given string. If the programmer does not specify the word, then the default prefix “egg” will be used. Capabilities The class is not tied to any particular program or user interface. The class provides an application logic of playing the Eggy-Peggy game
EggyPeggy Development Steps 1. Start with a class skeleton. Define the necessary data members and constructors 2. Implement the setPrefix Word and convert methods Add any private methods as necessary C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 12-9
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 9 EggyPeggy Development Steps 1. Start with a class skeleton. Define the necessary data members and constructors. 2. Implement the setPrefixWord and convert methods. Add any private methods as necessary
Reusable hilo r Problem statement Write an application ogic class that will play Hi-Lo games. The objective of the game is for the user to guess the computer-generated secret number in the least number of tries. The programmer can set the low and high bounds of a secret number and the number of guesses the user is allowed. If the number of guesses allowed is not set by the programmer then it is set by the HiLo object based on the designated low and high bounds. The default ow and high bounds are 1 and 100, The default number of guesses for the default low and high bounds is. C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12-10
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 10 Reusable HiLo Problem Statement Write an application logic class that will play Hi-Lo games. The objective of the game is for the user to guess the computer-generated secret number in the least number of tries. The programmer can set the low and high bounds of a secret number and the number of guesses the user is allowed. If the number of guesses allowed is not set by the programmer, then it is set by the HiLo object based on the designated low and high bounds. The default low and high bounds are 1 and 100. The default number of guesses for the default low and high bounds is 7