Chapter 8 - Object-Based Programming

Chapter 8 - Object-Based Programming

Chapter 8 Object-Based Programming Outline 8.1 Introduction 8.2 Implementing a Time Abstract Data Type with a Class 8.3 Class Scope 8.4 Controlling Access to Members 8.5 Referring to the Current Objects Members with this 8.6 Initializing Class Objects: Constructors 8.7 Using Overloaded Constructors 8.8 Using Set and Get Methods 8.9 Composition 8.10 Garbage Collection 8.11 Static Class Members 8.12 Final Instance Variables 8.13 Creating Packages 8.14 Package Access 8.15 Software Reusability 8.16 Data Abstraction and Encapsulation 2003 Prentice Hall, Inc. All rights reserved. 1 2 8.1 Introduction Object Oriented Programming (OOP) Encapsulates data (attributes) and methods (behaviors) Objects Allows objects to communicate

Well-defined interfaces Information hiding 2003 Prentice Hall, Inc. All rights reserved. 3 8.1 Introduction (cont.) Procedural programming language C is an example Action-oriented Functions are units of programming Object-oriented programming language Java is an example Object-oriented Classes are units of programming Functions, or methods, are encapsulated in classes 2003 Prentice Hall, Inc. All rights reserved. 4 8.1 Introduction (cont.) Object Based Programming = creating and using classes and objects Object Oriented Programming = Inheritance and Polymorphism 2003 Prentice Hall, Inc. All rights reserved. 8.2 Implementing a Time Abstract Data Type with a Class We introduce classes Time1 and TimeTest Time1.java declares class Time1 TimeTest.java declares class TimeTest public classes must be declared in separate files Class Time1 will not execute by itself Does not have method main TimeTest, which has method main, creates (instantiates) and uses Time1 object TimeTest.java does NOT import Time1

import not needed for class in same package (directory) 2003 Prentice Hall, Inc. All rights reserved. 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Fig. 8.1: Time1.java // Time1 class declaration maintains the time in 24-hour format. import java.text.DecimalFormat; Time1 (subclass) Outline 6 extends superclass java.lang.Object Time1.java public class Time1 extends Object { (Chapter 9 discusses inheritance) private int hour; // 0 - 23 Line 5

private int minute; // 0 - 59 (subclass) private int second; // 0 - 59 private variables (andTime1 methods) are extends accessible only to methods in thissuperclass class // Time1 constructor initializes each instance variable to zero; java.lang.Objec // ensures that each Time1 object starts in a consistent state t public Time1() Lines 6-8 { private variables setTime( 0, 0, 0 ); } Time1 constructor creates Lines 12-15 Time1 constructor // set a new time value using universal time; Time1 perform object then invokes then invokes method // validity checks on the data; set invalid values method to zero setTime setTime public void setTime( int h, int m, int s ) Line 19 { hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); public methods minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); Method setTime sets private 19-24 public methods (andLines variables) variables according to arguments second = ( ( s >= 0 && s < 60 ) ? s : 0 ); Method setTime are accessible wherever

program } sets private has Time1 reference variables according to arguments 2003 Prentice Hall, Inc. All rights reserved. 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 // convert to String in universal-time format public String toUniversalString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); Outline Time1.java return twoDigits.format( hour ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ); } // convert to String in standard-time format public String toStandardString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ) +

( hour < 12 ? " AM" : " PM" ); } } // end class Time1 2003 Prentice Hall, Inc. All rights reserved. 7 8.2 Implementing a Time Abstract Data Type with a Class (cont.) Every Java class must extend another class Time1 extends java.lang.Object If class does not explicitly extend another class, class implicitly extends Object Class constructor Same name as class Initializes instance variables of a class object Called when program instantiates an object of that class Can take arguments, but cannot return values Class can have several constructors through overloading Class Time1 constructor (lines 12-15) 2003 Prentice Hall, Inc. All rights reserved. 8 1 2 3 4 5 6 7 8 9 10 11 12 13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 8.2: TimeTest1.java Declare and create instance of class // Class TimeTest1 to exercise class Time1. Time1 by calling Time1 constructor import javax.swing.JOptionPane; public class TimeTest1 { Outline 9 TimeTest1.java Line 9 Declare and create interacts with Time1 instance of class by calling Time1 public methods Time1 by calling // append String version of time to String output Time1 constructor String output = "The initial universal time is: " + public static void main( String args[] ) { TimeTest1 Time1 time = new Time1(); // calls Time1 constructor time.toUniversalString() + "\nThe initial standard time is: " + time.toStandardString(); // change time and append updated time to output time.setTime( 13, 27, 6 ); output += "\n\nUniversal time after setTime is: " +

time.toUniversalString() + "\nStandard time after setTime is: " + time.toStandardString(); Lines 12-26 TimeTest1 interacts with Time1 by calling Time1 public methods // set time with invalid values; append updated time to output time.setTime( 99, 99, 99 ); output += "\n\nAfter attempting invalid settings: " + "\nUniversal time: " + time.toUniversalString() + "\nStandard time: " + time.toStandardString(); 2003 Prentice Hall, Inc. All rights reserved. 28 29 30 31 32 33 34 35 JOptionPane.showMessageDialog( null, output, "Testing Class Time1", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); Outline 10 TimeTest1.java } // end main } // end class TimeTest1 2003 Prentice Hall, Inc. All rights reserved. 11 8.3 Class Scope Class scope Class variables and methods

Members are accessible to all class methods Members can be referenced by name 2003 Prentice Hall, Inc. All rights reserved. 12 8.4 Controlling Access to Members Member access modifiers Control access to classs variables and methods public Variables and methods accessible to clients of the class private Variables and methods not accessible to clients of the class Typical = private data members and public methods 2003 Prentice Hall, Inc. All rights reserved. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // Fig. 8.3: TimeTest2.java // Errors resulting from attempts to access private members of Time1. public class TimeTest2 { public static void main( String args[] ) { Time1 time = new Time1(); time.hour = 7; // error: time.minute = 15; // error: time.second = 30; // error: }

Outline TimeTest2.java Lines 9-11 Compiler error hour is a private instance variable TimeTest2 cannot minute is a private instance variable directly access second is a private instance variable Time1s private Compiler error TimeTest2 cannot data directly access Time1s private data } // end class TimeTest2 TimeTest2.java:9: hour has private access in Time1 time.hour = 7; 13 // error: hour is a private instance variable ^ TimeTest2.java:10: minute has private access in Time1 time.minute = 15; // error: minute is a private instance variable ^ TimeTest2.java:11: second has private access in Time1 time.second = 30; // error: second is a private instance variable ^ 3 errors 2003 Prentice Hall, Inc. All rights reserved. 8.5 Referring to the Current Objects Members with this Keyword this Allows an object to refer to itself this refers to the object for which the method was called More important use later for things like supervisor.ID = this.ID; 2003 Prentice Hall, Inc. All rights reserved. 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // Fig. 8.4: ThisTest.java // Using the this reference to refer to instance variables and methods. import javax.swing.*; import java.text.DecimalFormat; Outline ThisTest.java public class ThisTest { public static void main( String args[] ) { SimpleTime time = new SimpleTime( 12, 30, 19 ); JOptionPane.showMessageDialog( null, time.buildString(), "Demonstrating the \"this\" Reference", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } } // end class ThisTest // class SimpleTime demonstrates the "this" reference class SimpleTime {

private int hour; private int minute; private int second; 2003 Prentice Hall, Inc. All rights reserved. 15 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 // constructor uses parameter names identical to instance variable // names; "this" reference required to distinguish between names public SimpleTime( int hour, int minute, int second ) { this.hour = hour; // set "this" object's hour this used to distinguish this.minute = minute; // set "this" object's minute between arguments and

this.second = second; // set "this" object's second ThisTest variables } // use explicit and implicit "this" to call toStandardString public String buildString() { return "this.toStandardString(): " + this.toStandardString() + "\ntoStandardString(): " + toStandardString(); } // return String representation of SimpleTime public String toStandardString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); Outline 16 ThisTest.java Lines 31-33 this used to distinguish between arguments and variables Lines 39-40 use explicit and implicit this to call Use explicit and toStandarsString implicit this to call toStandardString // "this" is not required here, because method does not // have local variables with same names as instance variables return twoDigits.format( this.hour ) + ":" + twoDigits.format( this.minute ) + ":" + twoDigits.format( this.second ); } } // end class SimpleTime 2003 Prentice Hall, Inc. All rights reserved. 8.6 Initializing Class Objects: Constructors Class constructor Same name as class Initializes instance variables of a class object Call class constructor to instantiate object of that class

new ClassName( argument1, argument2, , arugmentN ); new indicates that new object is created ClassName indicates type of object created arguments specifies constructor argument values If no constructor declared, compiler creates default constructor If any constructor(s) declared, compiler does not create default constructor 2003 Prentice Hall, Inc. All rights reserved. 17 18 8.7 Using Overloaded Constructors Overloaded constructors Methods (in same class) may have same name Must have different signatures (parameter lists) To call one constructor from another this (parameters); which must be the first statement of the constructor Calling one constructor from another is a little inefficient, but an excellent idea in case the object representation changes 2003 Prentice Hall, Inc. All rights reserved. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

20 21 22 23 24 25 26 27 28 // Fig. 8.5: Time2.java // Time2 class declaration with overloaded constructors. import java.text.DecimalFormat; public class Time2 { private int hour; private int minute; private int second; Outline 19 Time2.java // 0 - 23 // 0 - 59 // 0 - 59 Lines 12-15 No-argument (default) Use this to invoke the Time2 constructor constructor declared at lines 30-33 // Time2 constructor initializes each instance variable to zero; Line 14 // ensures that Time object starts in a consistent state Use this to invoke the public Time2() Time2 constructor { Overloaded constructor declared at lines 30-33 this( 0, 0, 0 ); // invoke Time2 constructor withint three

arguments has one argument } Lines 18-21 Overloaded // Time2 constructor: hour supplied, minute and second defaulted to 0 constructor has one public Time2( int h ) Second overloaded constructor int argument { has with two int Lines 24-27 this( h, 0, 0 ); // invoke Time2 constructor threearguments arguments } Second overloaded constructor has two // Time2 constructor: hour and minute supplied, second defaulted to 0 int arguments public Time2( int h, int m ) No-argument (default) constructor { this( h, m, 0 ); // invoke Time2 constructor with three arguments } 2003 Prentice Hall, Inc. All rights reserved. 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 Outline // Time2 constructor: hour, minute and second supplied public Time2( int h, int m, int s ) Third overloaded constructor { has three int arguments setTime( h, m, s ); // invoke setTime to validate time Time2.java } // Time2 constructor: another Time2 object supplied public Time2( Time2 time ) { // invoke Time2 constructor with three arguments this( time.hour, time.minute, time.second ); Fourth overloaded } constructor has Time2 argument // set a new time value using universal time; perform // validity checks on data; set invalid values to zero public void setTime( int h, int m, int s ) { hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); second = ( ( s >= 0 && s < 60 ) ? s : 0 ); } Lines 30-33 Third overloaded

constructor has three int arguments Lines 36-40 Fourth overloaded constructor has Time2 argument // convert to String in universal-time format public String toUniversalString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return twoDigits.format( hour ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ); } 2003 Prentice Hall, Inc. All rights reserved. 20 59 60 61 62 63 64 65 66 67 68 69 70 // convert to String in standard-time format public String toStandardString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); Outline Time2.java return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ) + ( hour < 12 ? " AM" : " PM" ); } } // end class Time2 2003 Prentice Hall, Inc.

All rights reserved. 21 Outline 1 // Fig. 8.6: TimeTest3.java 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Overloaded constructors used to initialize Time2 objects. import javax.swing.*; public class TimeTest3 { public static { Time2 t1 = Time2 t2 = Time2 t3 = Time2 t4 = Time2 t5 = Time2 t6 =

void main( String args[] ) new new new new new new String output "\nt1: all "\n " "\n " Time2(); Time2( 2 ); Time2( 21, 34 ); Time2( 12, 25, 42 ); Time2( 27, 74, 99 ); Time2( t4 ); // // // // // // 22 TimeTest3.java Instantiate each Time2 reference Lines 9-14 using a different constructor Instantiate each Time2 reference using a different 00:00:00 constructor 02:00:00 21:34:00 12:25:42 00:00:00 12:25:42

= "Constructed with: " + arguments defaulted" + + t1.toUniversalString() + + t1.toStandardString(); output += "\nt2: hour specified; minute and second defaulted" + "\n " + t2.toUniversalString() + "\n " + t2.toStandardString(); output += "\nt3: hour and minute specified; second defaulted" + "\n " + t3.toUniversalString() + "\n " + t3.toStandardString(); 2003 Prentice Hall, Inc. All rights reserved. 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 output += "\nt4: hour, minute and second specified" + "\n " + t4.toUniversalString() + "\n " + t4.toStandardString(); Outline

23 TimeTest3.java output += "\nt5: all invalid values specified" + "\n " + t5.toUniversalString() + "\n " + t5.toStandardString(); output += "\nt6: Time2 object t4 specified" + "\n " + t6.toUniversalString() + "\n " + t6.toStandardString(); JOptionPane.showMessageDialog( null, output, "Overloaded Constructors", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main } // end class TimeTest3 2003 Prentice Hall, Inc. All rights reserved. 24 8.8 Using Set and Get Methods Accessor method (get method) public method Allow clients to access private data BUT, class creator decides which private data to make available Mutator method (set method) public method Allow clients to modify private data BUT, class creator decides which private data to make modifiable and to control how it is modified (validity checking) 2003 Prentice Hall, Inc. All rights reserved. 1 2 3 4 5 6

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 // Fig. 8.7: Time3.java // Time3 class declaration with set and get methods. import java.text.DecimalFormat; public class Time3 { private int hour; private int minute; private int second; Outline 25 Time3.java // 0 - 23 // 0 - 59 // 0 - 59 private variables cannot be accessed directly by objects in differenttoclasses instance variable zero; // Time3 constructor initializes each // ensures that Time object starts in a consistent state

public Time3() { this( 0, 0, 0 ); // invoke Time3 constructor with three arguments } Lines 6-8 private variables cannot be accessed directly by objects in different classes // Time3 constructor: hour supplied, minute and second defaulted to 0 public Time3( int h ) { this( h, 0, 0 ); // invoke Time3 constructor with three arguments } // Time3 constructor: hour and minute supplied, second defaulted to 0 public Time3( int h, int m ) { this( h, m, 0 ); // invoke Time3 constructor with three arguments } 2003 Prentice Hall, Inc. All rights reserved. 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

52 53 54 55 56 57 // Time3 constructor: hour, minute and second supplied public Time3( int h, int m, int s ) { setTime( h, m, s ); } // Time3 constructor: another Time3 object supplied public Time3( Time3 time ) { // invoke Time3 constructor with three arguments this( time.getHour(), time.getMinute(), time.getSecond() ); } // Set Methods // set a new time value using universal time; perform // validity checks on data; set invalid values to zero public void setTime( int h, int m, int s ) { setHour( h ); // set the hour setMinute( m ); // set the minute setSecond( s ); // set the second } Outline 26 Time3.java Lines 45-68 Set methods allows objects to manipulate private variables Set methods allows objects to manipulate private variables // validate and set hour public void setHour( int h ) { hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); }

2003 Prentice Hall, Inc. All rights reserved. 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 // validate and set minute public void setMinute( int m ) { minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); } // validate and set second public void setSecond( int s ) { second = ( ( s >= 0 && s < 60 ) ? s : 0 ); } // Get Methods // get hour value public int getHour() { return hour; } // get minute value public int getMinute() {

return minute; } Outline Time3.java Lines 72-87 Get methods allow objects to read private variables Get methods allow objects to read private variables 2003 Prentice Hall, Inc. All rights reserved. 27 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 // get second value

public int getSecond() { return second; } Outline Time3.java // convert to String in universal-time format public String toUniversalString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return twoDigits.format( getHour() ) + ":" + twoDigits.format( getMinute() ) + ":" + twoDigits.format( getSecond() ); } // convert to String in standard-time format public String toStandardString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return ( ( getHour() == 12 || getHour() == 0 ) ? 12 : getHour() % 12 ) + ":" + twoDigits.format( getMinute() ) + ":" + twoDigits.format( getSecond() ) + ( getHour() < 12 ? " AM" : " PM" ); } } // end class Time3 2003 Prentice Hall, Inc. All rights reserved. 28 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

17 18 19 20 21 22 23 24 25 26 27 28 Outline // Fig. 8.8: TimeTest4.java // Demonstrating the Time3 class set and get methods. import java.awt.*; import java.awt.event.*; import javax.swing.*; TimeTest4.java public class TimeTest4 extends JApplet implements ActionListener { private Time3 time; Declare and instantiate private JLabel hourLabel, minuteLabel, secondLabel; Time3 objectdisplayField; private JTextIField hourField, minuteField, secondField, private JButton tickButton; // create Time3 object and set up GUI public void init() { time = new Time3(); // create Time3 object Lines 8 and 17 Declare and instantiate Time3 object Lines 25 and 31 JTextField allows user to specify hour. // get applet's content pane and change its layout to FlowLayout Container container = getContentPane(); container.setLayout( new FlowLayout() ); // set up hourLabel and hourField

hourLabel = new JLabel( "Set Hour" ); hourField = new JTextField( 10 ); container.add( hourLabel ); container.add( hourField ); 29 JTextField allows user to specify hour 2003 Prentice Hall, Inc. All rights reserved. 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 // set up minuteLabel and minuteField minuteLabel = new JLabel( "Set Minute" ); minuteField = new JTextField( 10 ); container.add( minuteLabel ); container.add( minuteField );

// set up secondLabel and secondField secondLabel = new JLabel( "Set Second" ); secondField = new JTextField( 10 ); container.add( secondLabel ); container.add( secondField ); Outline JTextField allows user to specify minute JTextField allows user to specify second // set up displayField displayField = new JTextField( 30 ); displayField.setEditable( false ); container.add( displayField ); 30 TimeTest4.java Line 31 JTextField allows user to specify minute Line 37 JTextField allows user to specify second // set up tickButton tickButton = new JButton( "Add 1 to Second" ); container.add( tickButton ); // register event handlers; this applet is the ActionListener, // which contains method actionPerformed that will be called to // handle action events generated by hourField, minuteField, // secondField and tickButton hourField.addActionListener( this ); minuteField.addActionListener( this ); secondField.addActionListener( this ); tickButton.addActionListener( this ); 2003 Prentice Hall, Inc. All rights reserved. 58 59 60 61 62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 displayTime(); // update text in displayField and status bar } // end method init // event handler for button and textfield events public void actionPerformed( ActionEvent event ) { // process tickButton event if ( event.getSource() == tickButton ) tick(); // process hourField event else if ( event.getSource() == hourField ) { time.setHour( Integer.parseInt( event.getActionCommand() ) ); hourField.setText( "" ); } Outline 31 TimeTest4.java Lines 71-74 Lines 77-80 Lines 83-86 TimeTest5 uses

Time3 set methods to set Time3 private variables // process minuteField event else if ( event.getSource() == minuteField ) { time.setMinute( Integer.parseInt( event.getActionCommand() ) ); minuteField.setText( "" ); TimeTest5 uses Time3 set methods } set Time3 private variables to // process secondField event else if ( event.getSource() == secondField ) { time.setSecond( Integer.parseInt( event.getActionCommand() ) ); secondField.setText( "" ); } 2003 Prentice Hall, Inc. All rights reserved. 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

112 113 114 115 116 117 displayTime(); // update text in displayField and status bar } // end method actionPerformed // update displayField and applet container's status bar public void displayTime() { displayField.setText( "Hour: " + time.getHour() + "; Minute: " + time.getMinute() + "; Second: " + time.getSecond() ); showStatus( "Standard time is: " + time.toStandardString() + "; Universal time is: " + time.toUniversalString() ); Outline TimeTest4.java Lines 95-96 TimeTest5 uses Time3 get methods to read Time3 private variables } // end method updateDisplay // add one to second and update hour/minute if necessary TimeTest5 uses Time3 get methods public void tick() to read Time3 private variables { time.setSecond( ( time.getSecond() + 1 ) % 60 ); if ( time.getSecond() == 0 ) { time.setMinute( ( time.getMinute() + 1 ) % 60 ); if ( time.getMinute() == 0 ) time.setHour( ( time.getHour() + 1 ) % 24 ); } } // end method tick } // end class TimeTest4 32 2003 Prentice Hall, Inc. All rights reserved. Outline

33 TimeTest4.java 2003 Prentice Hall, Inc. All rights reserved. Outline 34 TimeTest4.java 2003 Prentice Hall, Inc. All rights reserved. Outline 35 TimeTest4.java 2003 Prentice Hall, Inc. All rights reserved. 36 8.9 Composition Composition Class contains references to objects of other classes These references are members Neither class is a subclass of the other 2003 Prentice Hall, Inc. All rights reserved. 1 2 3 4 5 6 7 8 9 10 11 12 13

14 15 16 17 18 19 20 21 22 23 24 25 26 // Fig. 8.9: Date.java // Date class declaration. public class Date { private int month; private int day; private int year; Outline Class Date encapsulates data that describes date Date.java // 1-12 // 1-31 based on month // any year // constructor: call checkMonth to confirm proper value for month; // call checkDay to confirm proper value for day public Date( int theMonth, int theDay, int theYear ) { month = checkMonth( theMonth ); // validate month year = theYear; // could validate year day = checkDay( theDay ); // validate day Date constructor System.out.println( "Date object constructor for toDateString() ); 37 Line 4 Class Date

encapsulates data that describes date Lines 11-20 Date constructor instantiates Date object based on instantiates specified arguments Date object based on date " + specified arguments } // end Date constructor // utility method to confirm proper month value private int checkMonth( int testMonth ) { if ( testMonth > 0 && testMonth <= 12 ) // validate month return testMonth; 2003 Prentice Hall, Inc. All rights reserved. 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

50 51 52 53 54 55 else { // month is invalid System.out.println( "Invalid month (" + testMonth + ") set to 1." ); return 1; // maintain object in consistent state } Outline Date.java } // end method checkMonth // utility method to confirm proper day value based on month and year private int checkDay( int testDay ) { int daysPerMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // check if day in range for month if ( testDay > 0 && testDay <= daysPerMonth[ month ] ) return testDay; // check for leap year if ( month == 2 && testDay == 29 && ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) ) ) return testDay; System.out.println( "Invalid day (" + testDay + ") set to 1." ); return 1; // maintain object in consistent state } // end method checkDay 2003 Prentice Hall, Inc. All rights reserved. 38 56 57 58 59 60 61 62

63 // return a String of the form month/day/year public String toDateString() { return month + "/" + day + "/" + year; } Outline Date.java } // end class Date 2003 Prentice Hall, Inc. All rights reserved. 39 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 Outline

// Fig. 8.10: Employee.java // Employee class declaration. public class Employee { private String firstName; private String lastName; private Date birthDate; private Date hireDate; Employee.java Employee is composed of two references to Date objects // constructor to initialize name, birth date and hire date public Employee( String first, String last, Date dateOfBirth, Date dateOfHire ) { firstName = first; lastName = last; birthDate = dateOfBirth; hireDate = dateOfHire; } Lines 7-8 Employee is composed of two references to Date objects // convert Employee to String format public String toEmployeeString() { return lastName + ", " + firstName + " Hired: " + hireDate.toDateString() + " Birthday: " + birthDate.toDateString(); } } // end class Employee 2003 Prentice Hall, Inc. All rights reserved. 40 1 2 3 4 5

6 7 8 9 10 11 12 13 14 15 16 17 18 19 // Fig. 8.11: EmployeeTest.java // Demonstrating an object with a member object. import javax.swing.JOptionPane; public class EmployeeTest { Outline 41 EmployeeTest.ja va public static void main( String args[] ) { Date birth = new Date( 7, 24, 1949 ); Date hire = new Date( 3, 12, 1988 ); Employee employee = new Employee( "Bob", "Jones", birth, hire ); JOptionPane.showMessageDialog( null, employee.toEmployeeString(), "Testing Class Employee", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } } // end class EmployeeTest Date object constructor for date 7/24/1949 Date object constructor for date 3/12/1988 2003 Prentice Hall, Inc. All rights reserved. 42 8.10

Garbage Collection Memory no longer accessible is called a memory leak Garbage collection Returns memory to system Java performs this automagically object marked for garbage collection if no references to object Finalizer method (called a destructor in C++) Returns resources to system Java provides method finalize Defined in java.lang.Object Receives no parameters Returns void 2003 Prentice Hall, Inc. All rights reserved. 43 8.11 Static Class Members Each object has its own copy of all the class instance variables static keyword static class variable Class-wide information All class objects share same copy of this data Access to a classs public static members Qualify the member name with the class name e.g., Math.random() Can also use any object name e.g., e3.getCount() 2003 Prentice Hall, Inc. All rights reserved. 1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // Fig. 8.12: Employee.java // Employee class declaration. public class Employee { private String firstName; private String lastName; private static int count = 0; Outline Employee objects share one Employee.java instance of count // number of objects in memory // initialize employee, add 1 to static count and // output String indicating that constructor was called public Employee( String first, String last ) { firstName = first; lastName = last; ++count; // increment static count of employees System.out.println( "Employee constructor: " + firstName + " " + lastName ); } 44 Line 6

Employee objects share one instance of count Lines 23-28 Called when Employee is marked for garbage collection Called when Employee is marked for garbage collection collector // subtract 1 from static count when garbage // calls finalize to clean up object and output String // indicating that finalize was called protected void finalize() { --count; // decrement static count of employees System.out.println( "Employee finalizer: " + firstName + " " + lastName + "; count = " + count ); } 2003 Prentice Hall, Inc. All rights reserved. 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 Outline // get first name

public String getFirstName() { return firstName; } // get last name public String getLastName() { return lastName; } Employee.java static method accesses static variable count Lines 43-46 static method accesses static variable count // static method to get static count value public static int getCount() { return count; } } // end class Employee 2003 Prentice Hall, Inc. All rights reserved. 45 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

17 18 19 20 21 22 23 24 25 26 27 28 // Fig. 8.13: EmployeeTest.java // Test Employee class with static class variable, // static class method, and dynamic memory. import javax.swing.*; Outline 46 EmployeeTest can invoke Employee EmployeeTest.ja va static method, even though public class EmployeeTest { Employee has not been instantiated Line 12 public static void main( String args[] ) { EmployeeTest can // prove that count is 0 before creating Employees invoke Employee String output = "Employees before instantiation: " + static method, even Employee.getCount(); though Employee has not been // create two Employees; count should be 2 Employee e1 = new Employee( "Susan", "Baker" ); instantiated Employee e2 = new Employee( "Bob", "Jones" ); // prove that count is 2 after creating two Employees output += "\n\nEmployees after instantiation: " + "\nvia e1.getCount(): " + e1.getCount() + "\nvia e2.getCount(): " + e2.getCount() + "\nvia Employee.getCount(): " + Employee.getCount();

// get names of Employees output += "\n\nEmployee 1: " + e1.getFirstName() + " " + e1.getLastName() + "\nEmployee 2: " + e2.getFirstName() + " " + e2.getLastName(); 2003 Prentice Hall, Inc. All rights reserved. 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 // // // e1 e2 decrement reference count for each Employee object; in this example, there is only one reference to each Employee, so these statements mark each Employee object for garbage collection Calls Javas automatic garbage= null; collection mechanism = null; System.gc(); // suggest call to garbage collector // show Employee count after calling garbage collector; count // displayed may be 0, 1 or 2 based on whether garbage collector // executes immediately and number of Employee objects collected output += "\n\nEmployees after System.gc(): " +

Employee.getCount(); Outline EmployeeTest.ja va Line 35 Calls Javas automatic garbage-collection mechanism JOptionPane.showMessageDialog( null, output, "Static Members", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } } // end class EmployeeTest Employee Employee Employee Employee 47 constructor: Susan Baker constructor: Bob Jones finalizer: Susan Baker; count = 1 finalizer: Bob Jones; count = 0 2003 Prentice Hall, Inc. All rights reserved. 48 8.12 Final Instance Variables final keyword (similar to const in C++) Indicates that variable is not modifiable Any attempt to modify final variable results in error private final int INCREMENT = 5; Declares variable INCREMENT as a constant Enforces principle of least privilege Standard programming practice is to capitalize such a constant

2003 Prentice Hall, Inc. All rights reserved. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 // Fig. 8.14: IncrementTest.java // Initializing a final variable. import java.awt.*; import java.awt.event.*; import javax.swing.*; Outline 49 IncrementTest.j ava

public class IncrementTest extends JApplet implements ActionListener { private Increment incrementObject; private JButton button; // set up GUI public void init() { incrementObject = new Increment( 5 ); Container container = getContentPane(); button = new JButton( "Click to increment" ); button.addActionListener( this ); container.add( button ); } // add INCREMENT to total when user clicks button public void actionPerformed( ActionEvent actionEvent ) { incrementObject.increment(); showStatus( incrementObject.toIncrementString() ); } } // end class IncrementTest 2003 Prentice Hall, Inc. All rights reserved. 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

57 // class containing constant variable class Increment { private int count = 0; // number of increments private int total = 0; // total of all increments final private final int INCREMENT; // constant variable // initialize constant INCREMENT public Increment( int incrementValue ) { INCREMENT = incrementValue; // intialize } Outline 50 keyword declaresIncrement.java INCREMENT as constant Line 36 final keyword declares INCREMENT constant variable (once) as constant // add INCREMENT to total and add 1 to count public void increment() final variable INCREMENT must { total += INCREMENT; be initialized before using it ++count; } Line 41 final variable INCREMENT must be initialized before using it // return String representation of an Increment object's data public String toIncrementString() { return "After increment " + count + ": total = " + total; }

} // end class Increment 2003 Prentice Hall, Inc. All rights reserved. 51 IncrementTest.java:40: variable INCREMENT might not have been initialized { ^ 1 error 2003 Prentice Hall, Inc. All rights reserved. 52 8.13 Creating Packages We can import packages into programs Group of related classes and interfaces Help manage complexity of application components Facilitate software reuse Provide convention for unique class names Popular package-naming convention Reverse Internet domain name e.g., com.deitel 2003 Prentice Hall, Inc. All rights reserved. 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 8.16: Time1.java // Time1 class declaration maintains the time inClass 24-hour format. Time1 is placed package com.deitel.jhtp5.ch08; in this package import java.text.DecimalFormat; public class Time1 extends Object { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59 Class Time1 is in directory com/deitel/jhtp5/ch08 // Time1 constructor initializes each instance variable to zero; // ensures that each Time1 object starts in a consistent state import class DecimalFormat public Time1() from package java.text { setTime( 0, 0, 0 ); } // set a new time value using universal time; perform // validity checks on the data; set invalid values to zero public void setTime( int h, int m, int s ) {

hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); second = ( ( s >= 0 && s < 60 ) ? s : 0 ); } Outline 53 Time1.java Line 3 Class Time1 is placed in this package Line 3 Class Time1 is in directory com/deitel/jhtp 5/ch08 Line 5 import class DecimalFormat from package java.text 2003 Prentice Hall, Inc. All rights reserved. 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

// convert to String in universal-time format public String toUniversalString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); Outline DecimalFormat from Time1.java package java.text return twoDigits.format( hour ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ); } // convert to String in standard-time format public String toStandardString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); Line 31 DecimalFormat from package java.text return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ) + ( hour < 12 ? " AM" : " PM" ); } } // end class Time1 2003 Prentice Hall, Inc. All rights reserved. 54 1 2 3 4 5 6 7 8 9 10 11 12 13

14 15 16 17 18 19 20 21 22 23 24 25 26 // Fig. 8.17: TimeTest1.java // Class TimeTest1 to exercise class Time1. // Java packages import javax.swing.JOptionPane; // Deitel packages import com.deitel.jhtp5.ch08.Time1; public class TimeTest1 { import class JOptionPane from package javax.swing Outline 55 TimeTest1.java Line 5 import class // import Time1 class JOptionPane from import class Time1 from package package javax.swing com.deitel.jhtp4.ch08 args[] ) public static void main( String { Time1 time = new Time1(); // calls Time1 constructor // append String version of time to String output String output = "The initial TimeTest1 universal time is:

" + Time1 object can declare time.toUniversalString() + "\nThe initial standard time is: " + time.toStandardString(); // change time and append updated time to output time.setTime( 13, 27, 6 ); output += "\n\nUniversal time after setTime is: " + time.toUniversalString() + "\nStandard time after setTime is: " + time.toStandardString(); Line 8 import class Time1 from package com.deitel.jhtp 4.ch08 Line 14 TimeTest1 can declare Time1 object 2003 Prentice Hall, Inc. All rights reserved. 27 28 29 30 31 32 33 34 35 36 37 38 39 40 // set time with invalid values; append updated time to output time.setTime( 99, 99, 99 ); output += "\n\nAfter attempting invalid settings: " + "\nUniversal time: " + time.toUniversalString() + "\nStandard time: " + time.toStandardString(); Outline 56

TimeTest1.java JOptionPane.showMessageDialog( null, output, "Testing Class Time1", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main } // end class TimeTest1 2003 Prentice Hall, Inc. All rights reserved. 57 8.14 Package Access Package access Variable or method does not have member access modifier 2003 Prentice Hall, Inc. All rights reserved. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

// Fig. 8.18: PackageDataTest.java // Classes in the same package (i.e., the same directory) can // use package access data of other classes in the same package. Instantiate import javax.swing.JOptionPane; Outline 58 reference PackageDataTest to PackageData object .java public class PackageDataTest { Line 10 Instantiate reference to PackageData object public static void main( String args[] ) { PackageData packageData = new PackageData(); // append String representation of packageData to output Lines 13-22 String output = "After instantiation:\n" + packageData.toPackageDataString(); PackageDataTestPackageDataTest can // change package access data in packageData packageData.number = 77; packageData.string = "Goodbye"; can access access PackageData object PackageData data, data, because each class because each class shares same package shares same package // append String representation of packageData to output

output += "\nAfter changing values:\n" + packageData.toPackageDataString(); JOptionPane.showMessageDialog( null, output, "Package Access", JOptionPane.INFORMATION_MESSAGE ); 2003 Prentice Hall, Inc. All rights reserved. 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 Outline System.exit( 0 ); } PackageDataTest .java } // end class PackageDataTest // class with package access instance variables class PackageData { int number; // package-access instance variable String string; // package-access instance variable

// constructor public PackageData() { number = 0; string = "Hello"; } 59 Line 33 No access modifier, so class has packageaccess variables No access modifier, so class has package-access variables // return PackageData object String representation public String toPackageDataString() { return "number: " + number + " string: " + string; } } // end class PackageData 2003 Prentice Hall, Inc. All rights reserved. 60 8.15 Software Reusability Java Framework for achieving software reusability Rapid applications development (RAD) e.g., creating a GUI application quickly 2003 Prentice Hall, Inc. All rights reserved. 8.16 Data Abstraction and Encapsulation Information hiding Stack data structure Last in-first out (LIFO)

Like cafeteria trays on a spring-loaded platform Developer creates stack Hides stacks implementation details from clients Interface are methods push, pop, top, isEmpty? Data abstraction Abstract data types (ADTs) 2003 Prentice Hall, Inc. All rights reserved. 61 8.16 Data Abstraction and Encapsulation (Cont.) Abstract Data Type (ADT) Queue Like line at grocery store checkout First-in, first-out (FIFO) Enqueue to place objects in queue Dequeue to remove object from queue Enqueue and dequeue hide internal data representation 2003 Prentice Hall, Inc. All rights reserved. 62

Recently Viewed Presentations

  • 2017 Summer STM Opportunities Seed Week in New

    2017 Summer STM Opportunities Seed Week in New

    Seed Week in New York City. Place: New York City. Ministry: Unreached . people group ministry . specifically . focused on . building bridges . for the Gospel into the 52 unreached people groups of New York
  • Monday, August 18th, 2008

    Monday, August 18th, 2008

    P.A.D. = post-apocalyptic dystopia . Organizational patterns (flashback, chronological) Cause and effect notes. Context clue notes from today (synonym/antonym) NO NOTES. FCA #6 Results.
  • [13] In omni Gallia eorum hominum, qui aliquo

    [13] In omni Gallia eorum hominum, qui aliquo

    In primis hoc volunt persuadere, non interire animas, sed ab aliis post mortem transire ad alios, atque hoc maxime ad virtutem excitari putant metu mortis neglecto. Multa praeterea de sideribus atque eorum motu, de mundi ac terrarum magnitudine, de rerum...
  • Football History - Amazon Web Services

    Football History - Amazon Web Services

    Average of 900 kids per tournament, or total of 45,000 From this, 52 Regional teams selected to participate in 9 Provincial tournaments 9 Provincial Teams then to play in National tournaments Outcome - Talent Pyramid Pyramid for each age cohort...
  • Abortion: Life? Choice? Right? - God And Science.org

    Abortion: Life? Choice? Right? - God And Science.org

    Abortion: Life? Choice? Right? Secular Arguments Against Abortion Richard Deem ... to include neurulation. The term is not used in this book." (p. 55)." ... Susan Sherwin attempts to define personhood on the basis of interactions of individuals in relationships...
  • Review of Mass Transfer - Rowan University

    Review of Mass Transfer - Rowan University

    Review of Mass Transfer Fick's First Law (one dimensional diffusion) J flux (moles/area/time) At steady-state or any instant in time Fick's Second Law When concentration changes with time Example - Fick's First Law SS valid if a high C1 is...
  • HDF5 Introduction

    HDF5 Introduction

    C. B. A. Datatype conversion is performed before chunked placed in cache on write. Datatype conversion is performed after chunked is placed in application buffer. Chunk is written when evicted from cache. Compression and other filters are applied on eviction...
  • Binary Search Tree AVL Trees and Splay Trees

    Binary Search Tree AVL Trees and Splay Trees

    Binary Search Tree AVL Trees and Splay Trees PUC-Rio Eduardo S. Laber Loana T. Nogueira Binary Search Tree Is a commonly-used data structure for storing and retrieving records in main memory Binary Search Tree Is a commonly-used data structure for...