Lab 3: Designing Class Hierarchy.


 
 

Part A: Building your own project
 

 

In this part you will open a new project AnimalProject. Follow the instructions to build the new project.
 
 

1.      Download the files jpt.jar and base.jar to your Desktop.

2.      Open the Metrowork Codewarrior. To do it

a.       Go to Start menu àPrograms à Metrowerk CodeWarrioir àCodewarrior DE.

3.      In File menu select New

4.      Select Java2SE Stationery

5.      Type in the name for your project and hit return

6.      Expand the Generic item and select Java Application

7.      In the Project menu select CreateGroup and name it CodeBase

8.      In Project menu select Add Files and select the file Base.jar

9.      The window asks where to add files - select Java Application Release only

10.  In Project menu select Add Files and select the file jpt.jar

11.  The window asks where to add files - select Java Application Release only

12.  If needed, move base.jar and jpt.jar into the CodeBase folder (not important, just nice)

13.  Expand the Sources tab in the project file list.

14.  Delete TrivialApplication.

15.  Copy the skeleton of the test class (e.g. ExerciseSet1A.java) and re-name it in the file named AnimalTest.java.

16.  In three places change the Exerciseset1A to AnimalTest.

17.  Delete all methods, but the main method. 

18.  In Edit menu select Java Application Release Settings near the bottom

19.  On the left select Java Target and replace TrivialApplication with the name of your test class AnimalTest.

20.  In File menu select New

21.  Select File tab, then Text File - make sure you see where is it stored.

22.  Give the file the name of the class you will write: Animal.java for Class Animal (spelling preserve)

23.  Add a simple skeleton for your class Animal.

/**

* Class represents an animal

*/ 

public class Animal{

}

24.  Now run your project. You will see nothing, but you should check if it might be compiled and run.

 

Part B: Designing-Building a Hierarchy of Classes

In this part we will develop a class hierarchy to represent three different types of animals: dogs, cats, and cows. For each type of Animal we will record its name, weight, and its owner.

Part B.1

  1. Define the class Person, which contains as member data name and gender.
  2. Define the constructor for this class.
  3. Define the abstract class Animal, which contains as member data the name (String), weight (int), and owner (Person). (Hint: You will need a separate file named for this class. Create a new file using the instructions from the previous part. You will need a new file for each of your new classes)
  4. No constructor for the abstract class!
  5. Define the toString() method for this class.
  6. Develop the abstract method String voice() which returns the voice this animal produces.
  7. Develop a method getOwnerName() that returns the name of the owner. (Hint: Notice that owner is an object of class Person.)
  8. STOP! Run your project to make sure you do not have any spelling errors!

 

Part B.2

  1. Define class Cat that is a subclass of class Animal.
  2. Define the constructor for this class (Hint:  Use the same member data that are in the superclass).
  3. Define method String voice() that overrides the abstract method in the superclass and returns the sound a cat produces. (Hint: “Meeooww”)
  4. STOP! Run your project to make sure you do not have any spelling errors!

 

Part B.3

  1. Define class Dog that is a subclass of class Animal.
  2. Define a constructor for this class.
  3. Add a new member data isTrained which determines if this dog has been trained.
  4. Add a new method String sit() that returns “I am sitting” if the dog is trained and “R-r-r!” if it’s not.
  5. Define method String voice() that overrides the abstract method in the superclass and returns the sound a dog produces (“Rruff”).
  6. STOP! Run your project to make sure you do not have any spelling errors!

 

Part B.4

  1. Develop the test suites for this collection of classes. (Hint: Make sure your test suites actually have all 2 different types of animals and all methods for each animal.)
  2. Run you project. 

 

Part B.5

  1. Draw a UML for this project.
  2. See UML for this project.