COMPOSITION.docx

4
COMPOSITION MAIN POINTS: A class can have references to objects of other classes as members Such a capability is called composition and is sometimes referred has-a relationship . COMPOSITION: Our example of composition contains three classesDate Employee and EmployeeTest. Class Date declares instance variables month, day and ye represent a date. The constructor receives three int parameters. ine !" invo#es utility method chec#$onth %lines &'''( to validate the out)of)ran*e value is set to ! to maintain a consistent state. ine !+ the value for year is correct and does not validate it. ine ! invo#es chec#Day %lines ' +&( to validate the value for day based on the curren year. ines "&"' determine -hether the day is correct based on the numb in the particular month. f the day is not correct, lines " "/ determin month is 0ebruary, the day is &1 and the year is a leap year. f lines return a correct value for day, line +! returns ! to maintain the Date state. 3ote that lines !2!1 in the constructor output the this referenc Since this is a reference to the current Date object, the object4s toSt %lines +++2( is called implicitly to obtain the object4s Strin* represe 1 // Fig. 8.7: Date.java 2 // Date class declaration. 3 4 public class Date 5 { private int !ont"# // 1$12 7 private int da%# // 1$31 based on !ont"

Transcript of COMPOSITION.docx

COMPOSITIONMAIN POINTS: A class can have references to objects of other classes as members. Such a capability is called composition and is sometimes referred to as a has-a relationship.COMPOSITION:Our example of composition contains three classesDate Employee and EmployeeTest. Class Date declares instance variables month, day and year to represent a date. The constructor receives three int parameters.Line 14 invokes utility method checkMonth (lines 2333) to validate the monthan out-of-range value is set to 1 to maintain a consistent state. Line 15 assumes that the value for year is correct and does not validate it. Line 16 invokes utility method checkDay (lines 3652) to validate the value for day based on the current month and year. Lines 4243 determine whether the day is correct based on the number of days in the particular month. If the day is not correct, lines 4647 determine whether the month is February, the day is 29 and the year is a leap year. If lines 4248 do not return a correct value for day, line 51 returns 1 to maintain the Date in a consistent state. Note that lines 1819 in the constructor output the this reference as a String. Since this is a reference to the current Date object, the object's toString method (lines 5558) is called implicitly to obtain the object's String representation.

1 // Fig. 8.7: Date.java 2 // Date class declaration. 3 4 public class Date 5 { 6 private int month; // 1-12 7 private int day; // 1-31 based on month 8 private int year; // any year 910 // constructor: call checkMonth to confirm proper value for month;11 // call checkDay to confirm proper value for day12 public Date( int theMonth, int theDay, int theYear )13 {14 month = checkMonth( theMonth ); // validate month15 year = theYear; // could validate year16 day = checkDay( theDay ); // validate day1718 System.out.printf(19 "Date object constructor for date %s\n", this );20 } // end Date constructor2122 // utility method to confirm proper month value23 private int checkMonth( int testMonth )24 {25 if ( testMonth > 0 && testMonth 0 && testDay