Inheritance and Interface Lab

advertisement
Inheritance and Interface Lab
1
Define an inheritance hierarchy with the following classes. The top of this hierarchy
should consist of abstract classes. Only specific types of animals should be concrete
classes. Include obvious getter and setter methods.
An Animal has




weight, length : double
age: int
isDomestic : boolean
favoriteFood : string
A Mammal is a type of Animal but mammals have hair so include

haircolor: String
A Dog is a type of mammal and dogs have



breed: String
bark :String ("woof" "arf" etc)
name: String ("Fido" etc)
A Dolphin is a mammal and Dolphins have


species: String (Common dolphin or Bottle nose)
hearingRange:int (dolphins have a range of 1 to 150 kHz-- far greater than
humans)
A Reptile is a type of Animal . A reptile has a

body temperature : int
A Lizard is a type of a reptile. A lizard

laysEggs :boolean ( some do, some don't)
A snake is a type of reptile. A snake is

poisonous: boolean (some are, some aren’t)
Your hierarchy should implement:



toString()--give the kind of animal and ALL of its characteristics
equals(Object o) --two animals of the same type are considered equal if they
have the same weight.
Implement the Comparable interface and compare objects based on weight
Also

Define and implement an interface (AnimalInterface) with two methods:
1. boolean awake()
2. void eating() -- Say the critter is eating and mentions favorite food
Additionally

construct one array of 6 Animals by prompting the user for the specific type of
Animal and making an object of that type.
Sort the array by weight
print the content of the sorted array using toString()
print all mammals in the array
print all reptiles in the array
Lable your output (“all Animals in the array are: ….”)
------------------------------------------------------------------------------------------------





2. A Simple Inheritance Hierarchy
Implement an class Employee such that a member of Employee has a name, an
ID number (String), and a salary (int).
a. Override toString() -- the returned string should contain all employee
information
b. Override equals(Object o) -- two employees are equal if they have the
same ID number
c. getters and setters
d. implement the Comparable interface based on ID number--- remember ID i
is a String
Implement a subclass of Employee, called Manager. A manager is an employee
who supervises a department. The record of a manager includes all the
information included in a regular employee’s record plus the name of the
department he/she supervises. Override toString() for Managers. (Use super)
Executive extends Manager. An executive is a manager who gets a bonus at the
end of each year equal to a percentage of company profits. Include a field called
bonus (double). Implement Executive. You should redefine getSalary() to include the
bonus and again override toString()
Write another class TestEmployee that take an array of five employees (regular ,
managers, and executives), sorts by ID and prints the pertinent information for each
employee.
Download