0% found this document useful (0 votes)
35 views

By Stu: Belqes Bolghith Ahmad ID: 201917136 Class Number: 869

The document describes a Java program that creates a jungle with different animal classes like Lion, Elephant, and Monkey. The program uses inheritance, polymorphism, and array lists. It creates animal objects, adds them to a jungle array list, and then displays the details of each animal.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

By Stu: Belqes Bolghith Ahmad ID: 201917136 Class Number: 869

The document describes a Java program that creates a jungle with different animal classes like Lion, Elephant, and Monkey. The program uses inheritance, polymorphism, and array lists. It creates animal objects, adds them to a jungle array list, and then displays the details of each animal.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

By Stu: Belqes Bolghith Ahmad

ID: 201917136
Class Number: 869

Java Code :
import java.util.*;

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) {
animals.add(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
obj.display(); // child
classes(Lion, Elephants and Monkey) display method will be
called
System.out.println();
}
}
}

class Animal {

private String color;


private boolean isVegeterian;

// this constructor set the isVegeterian attribute


public Animal(boolean isVegeterian) {
this.isVegeterian = isVegeterian;
}

//setter method for color


public void setColor(String color) {
this.color = color;
}

// get method for isVegeterian


public boolean isVegeterian() {
return this.isVegeterian;
}

//get method for color


public String getColor() {
return this.color;
}

// 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
System.out.println("Color :
"+this.getColor());
System.out.println("isVegeterain :
"+this.isVegeterian());
}
}

// 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() {


super.display(); // calls the display
method of parent class
System.out.println("Number of Legs :
"+this.getNumOfLegs());
}
}

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() {


System.out.println("Animal Name : Lion");
super.display();
System.out.println("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() {


System.out.println("Animal Name :
Elephant");
super.display();
System.out.println("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() {


System.out.println("Animal Name : Monkey");
super.display(); // super class
display method is called
System.out.println("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


lion.setColor("Ash Brown");

elephant1.setColor("Dark grey");
elephant2.setColor("Dark grey");

monkey1.setColor("Grey");
monkey2.setColor("Red");
monkey3.setColor("Black");

// jungle object
Jungle jungle = new Jungle();

// adding the animal object to jungle


jungle.add(lion);
jungle.add(elephant1);
jungle.add(elephant2);
jungle.add(monkey1);
jungle.add(monkey2);
jungle.add(monkey3);

//printing the details of every animal in


the jungle
jungle.show();
}
}

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 :

You might also like