Review:
CSE148 Object Oriented Programming Homework Set 06
1. OOP principle 1: Encapsulation
2. OOP principle 2: Inheritance
3. OOP principle 3: Polymorphism
4. Overriding & overloading
5. Polymorphism and typecasting (down-casting, up-casting), dynamic binding.
6. Generic programming
Write a Java program to:
1. Redefine class Animal:
Data fields:
name: String
age: int
weight: double
id: int // unique ID of an animal
// implement a method to generate and assign a unique id to // each animal object.
Methods:
Constructor(s)
All setter/getter methods to get or set a data field/member.
SCCC CSE148, Spring 2021
getClass(): String getSpecies(): String toString(): String
// return ¡°Animal¡± as class name
// return ¡°Animal Species¡± as general species name // return information of this animal object
2. Define class Dog which is inherited from Animal: Data fields:
hasFur: double
Methods:
Constructor(s)
All setter/getter methods to get or set a data field/member.
getClass (): String getSpecies(): String hasFur(): boolean toString(): String
// override method defined in Animal. Return ¡°Mammal¡± // override method defined in Animal. Return ¡°Dog¡±
// return info if this dog has or not has fur
// return information of dog
3. Define class Cat which is inherited from Animal: Data fields:
coat: int // valid values are: 0: short, 1: medium, 2: long.
Methods:
Constructor(s)
All setter/getter methods to get or set a data field/member.
4. Define class Bird which is inherited from Animal: Data fields:
canFly: boolean
Methods:
Constructor
All setter/getter methods to get or set a data field/member.
SCCC CSE148, Spring 2021
getClass (): String getSpecies(): String getCoat(): String toString(): String
// override method defined in Animal. Return ¡°Mammal¡± // override method defined in Animal. Return ¡°Cat¡±
// return text message of coat
// return information of a cat
getClass (): String getSpecies(): String canFly(): boolean toString(): String
5. Test
// override method defined in Animal. Return ¡°Bird¡± // override method defined in Animal. Return ¡°Bird¡± // return info if this dog has or not has fur
// return information of this bird object
(1) Define an Animal array of size 10. Randomly generate some animal objects and store them in the array.
(Clue: generate a random number in range [1, 3], map 1 to dog, 2 to cat, and 3 to bird).
(2) Define a method to search for animals of a specific class in the array, for example to
search for all mammal animals. Return a list of animals of that class.
Call above method to search for ¡°Mammal¡± animals. Display returned animals. If an animal in the list is ¡°dog¡±, display message that this dog has or not has fur; if an animal is ¡°cat¡±, display coat information.
(3) Define a method to calculate the average weight of animals of different species in the array. Display average weight for each species inside method.
Message will be displayed like:
Average weight: Bird: 3.16
Cat: 12.35
Dog: 25.10 Fish: 1.17
(4) Define a method to sort animals in Animal array. After sorting, animals will be stored in the order of: bird first, followed is fish, then cat, then dog. Return sorted Animal array. Display returned array.
SCCC CSE148, Spring 2021