Exploring Inheritance - Object-Oriented Design - Lab 2 | CS 302, Lab Reports of Computer Science

Material Type: Lab; Class: Object-Oriented Design; Subject: Computer Science; University: University of Alabama - Birmingham; Term: Unknown 1989;

Typology: Lab Reports

Pre 2010

Uploaded on 04/12/2010

koofers-user-gtw-1
koofers-user-gtw-1 🇺🇸

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS-302
LAB 2
PART I : Exploring Inheritance
File Dog.java contains a declaration for a Dog class. Save this file to your directory and study it—notice
what instance variables and methods are provided. Files Labrador.java and Yorkshire.java contain
declarations for classes that extend Dog. Save and study these files as well.
File DogTest.java contains a simple driver program that creates a dog and makes it speak. Study
DogTest.java, save it to your directory, and compile and run it to see what it does. Now modify these files
as follows:
1. Add statements in DogTest.java after you create and print the dog to create and print a Yorkshire and
a Labrador. Note that the Labrador constructor takes two parameters: the name and color of the
labrador, both strings. Don't change any files besides DogTest.java. Now recompile DogTest.java;
you should get an error saying something like
./Labrador.java:18: cannot resolve symbol
symbol : constructor Dog ()
location: class Dog
But if you look at line 18 of Labrador.java, it's just a {. In fact, the constructor the compiler can't find
(Dog()) isn't called anywhere in this file.
a. What's going on? (Hint: What did we say about constructors in subclasses?)
=>
b. Fix the problem (which really is in Labrador) so that DogTest.java creates and makes the Dog,
Labrador, and Yorkshire all speak.
2. Add code to DogTest.java to print the average breed weight for both your Labrador and your
Yorkshire. Use the avgBreedWeight() method for both. What error do you get? Why?
=>
Fix the problem by adding the needed code to the Yorkshire class.
3. Add an abstract int avgBreedWeight() method to the Dog class. Remember that this means that the
word abstract appears in the method header after public, and that the method does not have a body
(just a semicolon after the parameter list). It makes sense for this to be abstract, since Dog has no idea
what breed it is. Now any subclass of Dog must have an avgBreedWeight method; since both
Yorkshire and Labrador do, you should be all set.
Save these changes and recompile DogTest.java. You should get an error in Dog.java (unless you
made more changes than described above). Figure out what's wrong and fix this error, then recompile
DogTest.java. You should get another error, this time in DogTest.java. Read the error message
carefully; it tells you exactly what the problem is. Fix this by changing DogTest (which will mean
taking some things out).
pf3
pf4

Partial preview of the text

Download Exploring Inheritance - Object-Oriented Design - Lab 2 | CS 302 and more Lab Reports Computer Science in PDF only on Docsity!

CS-

LAB 2

PART I : Exploring Inheritance

File Dog.java contains a declaration for a Dog class. Save this file to your directory and study it—notice what instance variables and methods are provided. Files Labrador.java and Yorkshire.java contain declarations for classes that extend Dog. Save and study these files as well.

File DogTest.java contains a simple driver program that creates a dog and makes it speak. Study DogTest.java , save it to your directory, and compile and run it to see what it does. Now modify these files as follows:

  1. Add statements in DogTest.java after you create and print the dog to create and print a Yorkshire and a Labrador. Note that the Labrador constructor takes two parameters: the name and color of the labrador, both strings. Don't change any files besides DogTest.java. Now recompile DogTest.java; you should get an error saying something like

./Labrador.java:18: cannot resolve symbol symbol : constructor Dog () location: class Dog

But if you look at line 18 of Labrador.java , it's just a {. In fact, the constructor the compiler can't find ( Dog() ) isn't called anywhere in this file. a. What's going on? (Hint: What did we say about constructors in subclasses?) =>

b. Fix the problem (which really is in Labrador ) so that DogTest.java creates and makes the Dog , Labrador , and Yorkshire all speak.

  1. Add code to DogTest.java to print the average breed weight for both your Labrador and your Yorkshire. Use the avgBreedWeight() method for both. What error do you get? Why?

Fix the problem by adding the needed code to the Yorkshire class.

  1. Add an abstract int avgBreedWeight() method to the Dog class. Remember that this means that the word abstract appears in the method header after public , and that the method does not have a body (just a semicolon after the parameter list). It makes sense for this to be abstract, since Dog has no idea what breed it is. Now any subclass of Dog must have an avgBreedWeight method; since both Yorkshire and Labrador do, you should be all set.

Save these changes and recompile DogTest.java. You should get an error in Dog.java (unless you made more changes than described above). Figure out what's wrong and fix this error, then recompile DogTest.java. You should get another error, this time in DogTest.java. Read the error message carefully; it tells you exactly what the problem is. Fix this by changing DogTest (which will mean taking some things out).

Part II : A Sorted Integer List

File IntList.java contains code for an integer list class. Save it to your directory and study it; notice that the only things you can do are create a list of a fixed size and add an element to a list. If the list is already full, a message will be printed. File ListTest.java contains code for a class that creates an IntList , puts some values in it, and prints it. Save this to your directory and compile and run it to see how it works.

Now write a class SortedIntList that extends IntLis t. SortedIntList should be just like IntList except that its elements should always be sorted. This means that when an element is inserted into a SortedIntList , it should be put into its sorted place, not just at the end of the array. Think carefully about what methods and instance variables you have to define in SortedIntList and what you can inherit directly fro m IntList — don't override anything you don't have to.

To test your class, modify ListTest.java so that after it creates and prints the IntList , it creates and prints a SortedIntList containing the same elements.

Part IV: Overriding the equals Method

File Player.java contains a class that holds information about an athlete: name, team, and uniform number. File ComparePlayers.java contains a skeletal program that uses the Player class to read in information about two baseball players and determine whether or not they are the same player.

  1. Fill in the missing code in ComparePlayers so that it reads in two players and prints "Same player" if they are the same, "Different players" if they are different. Use the equals method, which Player inherits from the Object class, to determine whether two players are the same. Are the results what you expect?
  2. The problem above is that as defined in the Object class, equals does an address comparison. It says that two objects are the same if they live at the same memory location, that is, if the variables that hold references to them are aliases. The two Player objects in this program are not aliases, so even if they contain exactly the same information they will be "not equal." To make equals compare the actual information in the object, you can override it with a definition specific to the class. It might make sense to say that two players are "equal" (the same player) if they are on the same team and have the same uniform number.

Use this strategy to define an equals method for the Player class. Your method should take a Player object and return true if it is equal to the current object, false otherwise.

Test your ComparePlayers program using your modified Player class. It should give the results you would expect.