Download and unzip the lab3.zip file.
Select "Switch Workspace..." in Eclipse and select the newly unzip'd lab3 directory.
Begin in the Mammals project and open the Dog class. Try to compile this class. There is a problem that you must identify and fix by editing the Mammal class. Once fixed, maintain this fix throughout the remainder of the lab.
Examine the classes Mammal and Human as well. Draw a UML class diagram to show the relationship among these three classes.
When you are ready, call me over and explain the problem you encountered compiling the Dog class and how you fixed it. Also, show me your class diagram.
Examine the Demo1 class in the same project. Predict the output when you compile and run this program. Now compile and run it. How did the hair color get assigned?
When you are ready, call me over and explain what happens when you run the Demo1 class.
Edit the Demo2 class in the same project. Predict the output when you compile and run this program and then do so. Make sure you understand its behavior, and then change the statement
Human [] human = new Human[SIZE];to
Mammal [] human = new Human[SIZE];and try to compile this program. What happens? What is the problem?
Leaving this change in place, change the statement
System.out.println(human[i].getSSN());to
System.out.println(human[i].getHairColor());and try to compile the program. What happens?
To help you understand this, draw a picture of the array called human and think about what the objects in this array are declared to be.
When you are ready, call us over to explain what happens when you make the above changes to Demo2.
Examine class Demo3. What do you think will happen when you compile and run it? Do so and run it several times. Is this an example of polymorphism?
When you are ready, call me over and explain what happens when you run this program and whether or not it is an example of polymorphism.
Finally, examine class Demo4. What do you think will happen when you compile and run it? Do so and run it several times. Is this an example of polymorphism?
When you are ready, call me over and explain what happens when you run this program and whether or not it is an example of polymorphism.
Now turn your attention to the Accessible project and examine the classes Q and X. Observe that an instance of the class X contains an instance of the class Q as a data member.
Examine the class Demo1 and predict what will happen when you run this program. Compile and run Demo1 and verify that your predictions are correct.
Now un-comment the two lines in the main method of the Demo1 class. The purpose of un-commenting this code is to increment the internal value of the Q object before printing its value a second time. Compile this class and observe the error message.
There are two ways to correct the error. One is to change the visibility of the data member q in the definition of the class X, and another is to have the Demo1 class access this data member through the accessor built in to the class X. In each case, you should modify only one line in the file.
Implement both of these ways and compile and run your program. Be sure to restore your class X to its original contents before you make your changes to Demo1 before you recompile Demo1.
When you are ready, call me over and explain your changes and show me your running programs.
Next examine the class Demo2. Try to compile this class and observe the error message you get.
There are two ways to correct the error. One is to change the visibility of the data member q in the definition of the class X to protected. Make this change and compile your Demo2program. Verify that there is no longer an error. After you have done this, restore your class X to its original contents (by making the data member q private).
Another way to correct the error is to keep q private in the class X but to let the class Y access the data member q through the getQ() method in its parent class X. You can do this one of two ways:
Verify that each of these changes to Y result in error-free compiles and correct output. Be prepared to explain why either of these changes solves the problem.
When you are ready, call me over and explain why either of the two changes solves the problem.
This laboratory assignment is based on a similar assignment developed by Susan Haller and Timothy Fossum at UW-Parkside.
© 2001-2006 Dr. Christopher C. Taylor | Office: CC-36C | Phone: 277-7339 |