11:45 PM PST 6/5/2012
Objective: To learn how to use an inner class, object-type class attributes, StringBuffer, null values, and self-referential classes.
Steps:
(i) Create a new folder called 'assign72' under C:\Java\Lesson7. Copy all files (except [login to view URL]) of assign 7.1 into this folder.
(ii) Modify classes as follows:
Class Name: Home
Attributes:
• Remove the attributes: 'value' and 'commission'
• Add a new attribute called 'hd' of type: HomeDerivedData
• Add a new attribute called 'nextHome' of type: Home
Constructor:
• constructor (homeType, age, sqft):
Modify this constructor to instantiate an object of the inner class HomeUtil and call computeDerivedData() on it, to update the 'hd' class attribute.
Set 'nextHome' attribute to 'null' (without the quotes). Note that null is valid to use as a value of any object, and means the object is 'undefined'.
Methods:
• Getters for all attributes
• Setter for 'nextHome' attribute only
Inner Class: Class Name: HomeUtil
Methods:
• Copy computeDerivedData() method from [login to view URL] of assign 7.1 into this inner class, and modify it so that it doesn't take any input parameters and has a return type of 'void'. Inside the method body, update the 'hd' attribute of the Home class, to set its 'value' and 'commission', using same formulae as in assign 7.1.
Class Name: HomeType
Attributes:
No change
Constructor:
No need to implement any constructor (as before)
Methods:
• Modify getTypeDescr() to return a StringBuffer instead of a String
Class Name: HomeDerivedData
Attributes:
No change
Constructor:
Add a default constructor
Methods:
No change
NOTE: [login to view URL]: NOT to be used anymore
(iii) Modify the TestHome test driver so that it instantiates three Home type objects, as follows:
• Home object # 1: Type: Single Family, age = 15 yrs, and sqft = 2000.
• Home object # 2: Type: Condo, age = 8 yrs, and sqft = 1500.
• Home object # 3: Type: Twon Home, age = 10 yrs, and sqft = 2500.
The 'nextHome' for Home object # 1 should be Home object # 2; similarly, the 'nextHome' for Home object # 2 should be Home object # 3. To achieve this, call setNextHome() on Home object # 1, sending it Home object # 2. Similary, call setNextHome() on Home object # 2, sending it Home object # 3.
Write a static method called 'printReport()' that takes a Home object, and a 'homeNbr' parameter of type: int. This method should print the content of the Home object (same as in assign 7.1, except that now the title for each home should include the home nbr. For example, for Home object # 1, it should say "Summary Report for Home # 1", and so on. Begin by copying the [login to view URL] lines from [login to view URL] of assign 7.1 into this method. Replace all [login to view URL] lines with String concatenation using the += operator, each time adding one line of output. Do NOT use one long String to concatenate all pieces, instead use += operator for each attribute of the Home object, and a SINGLE [login to view URL]() at the end of the method to dump the resulting String containing the full report for the Home object.
Inside main(), declare a temporary Home type object called 'hTmp', and assign it the first Home object. Declare a loop index called 'i' and initialize to 1. Then use a while loop that loops as long as hTmp is not null. In each iteration of the loop, call 'printReport()' passing it the 'hTmp' object and a home nbr (the loop index), then set 'hTmp' to be the next Home object each time. Increment 'i' at the end of the loop.
(iv) Compile [login to view URL], [login to view URL], [login to view URL], and then [login to view URL] and run the code. Verify that the output session looks similar to this:
C:\Java\Lesson7\assign72>javac [login to view URL]
C:\Java\Lesson7\assign72>javac [login to view URL]
C:\Java\Lesson7\assign72>javac [login to view URL]
C:\Java\Lesson7\assign72>javac [login to view URL]
C:\Java\Lesson7\assign72>java TestHome
Summary Report for Home # 1
Type: Single Family
Age: 15
I have been programming Java for 4 years, and am about to finish my Bachelor Degree in Computer Science. I am capable of quickly completing this project.