Lab 3: Designing Class Hierarchy.


 
  You do NOT need to download the .exe/.zip for this lab, however, it is a great reference material for syntax of class hierarchy organization/declaration (MyPoint class).

Part A: Building your own project
 

 

  1. In this part you will open a new project AnimalProject. Follow the instructions to build the new project.
     
    Create a folder yourname on the Desktop.
  2. Download the files jpt.jar and jpfalt.jar to the folder.
  3. Open the Metrowork Codewarrior. To do it
  4. Go to Start menu àPrograms à Metrowerk CodeWarrioir àCodewarrior DE.
  5. InFile menu select New
  6. SelectJava2SE Stationery
  7. Type in the name for your project, select Lab3 folder for it, and hit return
  8. Expand the Generic item and select Java Application
  9. In the Project menu select CreateGroup and name it CodeBase
  10. InProject menu select Add Files and select the file jpfalt.jar
  11. The window asks where to add files - select Java Application Release only
  12. InProject menu select Add Files and select the file jpt.jar
  13. The window asks where to add files - select Java Application Release only
  14. Expand the Sources tab in the project file list.
  15. Delete TrivialApplication.
  16. InFile menu select New
  17. SelectFile tab, then Text File – choose where you want to store it.
  18. Give the file the name of the class you will write: Animal.java for Class Animal (spelling preserve).
  19. While you are in the Animal.java window in the menu click on Project->add Animal.java to this project, so it would appear in the Sources.
  20. Repeat the above steps (16-19) to create a file AnimalTest.java, where you are going to put all the tests for the Animal class.
  21. In order to get the GUI to run the AnimalTest you have to make the following changes to the paths:
    1. In Edit menu select Java Application Release Settings near the bottom
    2. On the left select Java Target and replace TrivialApplication with the name of your test class AnimalTest.
    3. On the left select Java Output and replace TrivialApplication with the name of your test class AnimalTest.
  22. Now you are ready to start writing your project.
  23. Add a simple skeleton for your class Animal.

/**

* Class represents an animal

*/ 

public abstract class Animal{

}

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. For each type of Animal we will record its name, weight, and its owner.  If you are uncomfortable with syntax of definition of abstract classes, refer to the Point.mcp (download is available with this lab) that has a great declaration of a simple hierarchy.

Part B.1

  1. Define the class Person, which contains as member data name and gender. (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)
  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).
  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. Make examples of cats.
  4. Design method String voice() that overrides the abstract method in the superclass and returns the sound a cat produces. (Hint: “Meeooww”). Remeber to start with examples.
  5. STOP! Run your project to make sure you do not have any spelling error.

 

 

Part B.

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

 

Part B.4

  1. Go over the test suites for this collection of classes. 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.