By Stu: Belqes Bolghith Ahmad
ID: 201917136
Class Number: 869
Java Code :
import [Link].*;
class Jungle {
private ArrayList<Animal> animals;
// this constructor initialize the ArrayList
public Jungle() {
animals = new ArrayList<Animal>();
}
// adds the animal object to the ArrayList
public void add(Animal animal) {
[Link](animal);
}
// prints the detail of each animal in the Jungle
public void show() {
// enhanced for loop
for(Animal obj : animals) {
// to print the details of
particular animal
[Link](); // child
classes(Lion, Elephants and Monkey) display method will be
called
[Link]();
}
}
}
class Animal {
private String color;
private boolean isVegeterian;
// this constructor set the isVegeterian attribute
public Animal(boolean isVegeterian) {
[Link] = isVegeterian;
}
//setter method for color
public void setColor(String color) {
[Link] = color;
}
// get method for isVegeterian
public boolean isVegeterian() {
return [Link];
}
//get method for color
public String getColor() {
return [Link];
}
// display the details of an animal
public void display() {
//// getColor return the color of an
animal, this keyword is used to refer the current object
[Link]("Color :
"+[Link]());
[Link]("isVegeterain :
"+[Link]());
}
}
// Mammals class is a subclass for animal
class Mammals extends Animal {
private int num_of_legs;
public Mammals(boolean isVegeterian, int legs) {
// call the parent class constructor
super(isVegeterian);
this.num_of_legs = legs; // set the value
of num_Of_legs
}
// get method for num_of_legs
public int getNumOfLegs() {
return this.num_of_legs;
}
public void display() {
[Link](); // calls the display
method of parent class
[Link]("Number of Legs :
"+[Link]());
}
}
class Lion extends Mammals{
// by making variable as final it act as a constant
private final int AVERAGE_SPEED = 80;
public Lion(boolean isVegeterian, int legs) {
super(isVegeterian, legs); //
calls the parent class constructor to set the values
}
public void display() {
[Link]("Animal Name : Lion");
[Link]();
[Link]("Average Speed : "+
this.AVERAGE_SPEED);
}
}
class Elephant extends Mammals{
private final int AVERAGE_SPEED = 40;
public Elephant(boolean isVegeterian, int legs) {
super(isVegeterian, legs);
}
public void display() {
[Link]("Animal Name :
Elephant");
[Link]();
[Link]("Average Speed : "+
this.AVERAGE_SPEED);
}
}
class Monkey extends Mammals{
private final int AVERAGE_SPEED = 55;
public Monkey(boolean isVegeterian, int legs) {
super(isVegeterian, legs);
}
public void display() {
[Link]("Animal Name : Monkey");
[Link](); // super class
display method is called
[Link]("Average Speed : "+
this.AVERAGE_SPEED);
}
}
public class BlueJ {
// driver code
public static void main(String[] args) {
// lion object
Lion lion = new Lion(false, 4);
//elephant objects
Elephant elephant1 = new Elephant(true,4);
Elephant elephant2 = new Elephant(true,4);
//monkey objects
Monkey monkey1 = new Monkey(true,2);
Monkey monkey2 = new Monkey(true,2);
Monkey monkey3 = new Monkey(true,2);
// setting the color of an animals
[Link]("Ash Brown");
[Link]("Dark grey");
[Link]("Dark grey");
[Link]("Grey");
[Link]("Red");
[Link]("Black");
// jungle object
Jungle jungle = new Jungle();
// adding the animal object to jungle
[Link](lion);
[Link](elephant1);
[Link](elephant2);
[Link](monkey1);
[Link](monkey2);
[Link](monkey3);
//printing the details of every animal in
the jungle
[Link]();
}
}
Output :
Animal Name : Lion
Color : Ash Brown
isVegeterain : false
Number of Legs : 4
Average Speed : 80
Animal Name : Elephant
Color : Dark grey
isVegeterain : true
Number of Legs : 4
Average Speed : 40
Animal Name : Elephant
Color : Dark grey
isVegeterain : true
Number of Legs : 4
Average Speed : 40
Animal Name : Monkey
Color : Grey
isVegeterain : true
Number of Legs : 2
Average Speed : 55
Animal Name : Monkey
Color : Red
isVegeterain : true
Number of Legs : 2
Average Speed : 55
Animal Name : Monkey
Color : Black
isVegeterain : true
Number of Legs : 2
Average Speed : 55
Output ScreenShot :
Program Screenshot :