Lab 15 Manual CSE215 Summer 2024
Lab 15 Manual CSE215 Summer 2024
Classwork
Problem: Animal Kingdom Hierarchy
You are required to model an animal kingdom hierarchy for a zoo. The base class will be
Animal, and derived classes will be Mammal, Bird, and Fish. Each animal has a name, age, and
weight, and can make a sound. Different animals also have unique behaviors. Additionally,
mammals can run, birds can fly, and fish can swim. You will implement method overloading for
the makeSound method to take an additional parameter.
Problem Set:
1. Create a base class Animal with attributes name, age, and weight. Define the method
makeSound() that prints a generic animal sound and another overloaded version of
makeSound(String soundType) that takes a sound type and prints it.
2. Create three subclasses:
○ Mammal: Implements makeSound() to print "Mammal sound" and has a method
run().
○ Bird: Implements makeSound() to print "Bird sound" and has a method fly().
○ Fish: Implements makeSound() to print "Fish sound" and has a method swim().
3. Implement a test class that creates instances of Mammal, Bird, and Fish, assigns values to
their attributes, and calls their makeSound() methods, as well as their specific behaviors
(run(), fly(), swim()).
Homework
Problem: Transportation System
You are tasked with designing a more complex transportation system hierarchy. The base class
will be Vehicle, and derived classes will be Car, Bike, and Bus. Each vehicle has a speed, fuel,
and a capacity. Each vehicle can display its type and current state using polymorphism (method
overriding), and method overloading will allow setting speed in different units.
Problem Set:
1. Create a base class Vehicle with attributes speed, fuel, and capacity. Define two
overloaded setSpeed() methods, one for setting speed in integer format (km/h) and
another for double format (m/s). Also, create a displayInfo() method that is overridden by
subclasses.
2. Create three subclasses:
○ Car: Overrides displayInfo() to print "Car is running at {speed} km/h with {fuel}
liters of fuel", and has an additional method playMusic().
○ Bike: Overrides displayInfo() to print "Bike is running at {speed} km/h with
{fuel} liters of fuel", and has a method ringBell().
○ Bus: Overrides displayInfo() to print "Bus is running at {speed} km/h with {fuel}
liters of fuel and {capacity} passengers", and has a method openDoors().
3. Implement a test class where you create instances of Car, Bike, and Bus. Set their speeds
using both overloaded setSpeed() methods, assign fuel levels and capacities, and call
displayInfo() for each vehicle. Also, demonstrate each vehicle’s specific behavior.