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
- 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.
- Download
the files jpt.jar and jpfalt.jar to the
folder.
- Open
the Metrowork Codewarrior. To do it
- Go to
Start menu àPrograms à Metrowerk CodeWarrioir àCodewarrior
DE.
- InFile
menu select New
- SelectJava2SE
Stationery
- Type
in the name for your project, select Lab3 folder for it, and hit return
- Expand
the Generic item and select Java Application
- In the
Project menu select CreateGroup
and name it CodeBase
- InProject
menu select Add Files and select the file jpfalt.jar
- The
window asks where to add files - select Java Application Release
only
- InProject
menu select Add Files and select the file jpt.jar
- The
window asks where to add files - select Java Application Release
only
- Expand
the Sources tab in the project file list.
- Delete TrivialApplication.
- InFile
menu select New
- SelectFile
tab, then Text File – choose where you want to store it.
- Give
the file the name of the class you will write: Animal.java
for Class Animal (spelling preserve).
- 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.
- 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.
- In
order to get the GUI to run the AnimalTest you have to make the following
changes to the paths:
- In Edit menu select Java Application Release
Settings near the bottom
- On the left select Java Target and replace TrivialApplication with the name of your test class AnimalTest.
- On the left select Java Output and replace TrivialApplication with the name of your test class AnimalTest.
- Now
you are ready to start writing your project.
- 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
- 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)
- Define
the constructor for this class.
- Define
the abstract class Animal, which contains as member data the name
(String), weight (int), and owner (Person).
- No
constructor for the abstract class!
- Define
the toString() method for this class.
- Develop
the abstract method String voice()
which returns the voice this animal produces.
- Develop
a method getOwnerName()that returns the
name of the owner. (Hint: Notice that owner is an object of
class Person.)
- STOP!
Run your project to make sure you do not have any spelling errors!
Part B.2
- Define
class Cat that is a subclass of class Animal.
- Define
the constructor for this class (Hint: Use the same
member data that are in the superclass).
- Make
examples of cats.
- 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.
- STOP!
Run your project to make sure you do not have any spelling error.
Part B.
- Define
class Dog that is a subclass of class Animal.
- Define
a constructor for this class.
- Make
examples of dogs.
- Add a
new member data isTrained which
determines if this dog has been trained.
- Design
a method String sit()that returns “I am
sitting” if the dog is trained and “R-r-r!” if it’s not.
- 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.
- STOP!
Run your project to make sure you do not have any spelling errors!
Part B.4
- 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.
- Run
you project.
Part B.5
- Draw a
UML for this project.
- See UML for this project.