Task 1 Extended Animal Class Hierarchy With Pet Interface - Done
Task 1 Extended Animal Class Hierarchy With Pet Interface - Done
LAB REPORT X
SUBJECT NAME:
OOP
INSTRUCTOR:
MAM MAHNOOR
SUBMITTED BY:
DE-44 (CE)-A
Task: Extended Animal Class Hierarchy with Pet Interface
Requirements:
• Pet Interface:
o Create an interface named Pet with a method play().
o The play() method should represent a generic play behavior that pets can
exhibit.
• Animal Class:
o Design an Animal class to serve as the base class for different types of
animals.
o Include attributes such as name and age to represent common information for
all animals.
o Implement a constructor to initialize these attributes.
o Provide getter methods for name and age.
o Include a method called displayInfo() to display basic information about
the animal.
• Dog Class:
o Extend the Animal class to create a Dog class.
o Include an additional attribute breed specific to dogs.
o Implement a constructor to initialize the attributes of both the base class and
the derived class.
o Override the displayInfo() method to include information about the dog's
breed.
o Implement the play() method from the Pet interface to represent how a dog
plays.
• Cat Class:
o Extend the Animal class to create a Cat class.
o Include an additional attribute color specific to cats.
o Implement a constructor to initialize the attributes of both the base class and
the derived class.
o Override the displayInfo() method to include information about the cat's
color.
o Implement the play() method from the Pet interface to represent how a cat
plays.
• Tiger Class:
o Extend the Animal class to create a Tiger class.
o Include an additional attribute habitat specific to tigers.
o Implement a constructor to initialize the attributes of both the base class and
the derived class.
o Override the displayInfo() method to include information about the tiger's
habitat.
• Fish Class:
o Extend the Animal class to create a Fish class.
o Include an additional attribute waterType specific to fish.
o Implement a constructor to initialize the attributes of both the base class and
the derived class.
o Override the displayInfo() method to include information about the fish's
water type.
• Main Class:
o Create a Main class to test the functionality of the extended animal hierarchy.
o Instantiate objects of Dog, Cat, Tiger, and Fish.
o Display information about each animal using the displayInfo() method.
o Invoke the play() method on pet instances to demonstrate their play
behavior.
Code:
Animal class:
package lab11ask1;
import java.util.Scanner;
@Override
public void play()
{
System.out.println("Cat says Meow");
}
@Override
public void displayInfo()
{
System.out.println("Cat attributes: ");
System.out.println("Cat name: "+name);
System.out.println("Cat age: "+ age);
System.out.println("Cat color: "+ color);
}
}
Dog class:
package lab11ask1;
import java.util.Scanner;
Fish class:
package lab11ask1;
import java.util.Scanner;
@Override
public void displayInfo()
{
System.out.println("Fish attributes: ");
System.out.println("Fish name: "+name);
System.out.println("Fish age: "+ age);
System.out.println("Fish watertype: "+ watertype);
}
@Override
public void play() {
// TODO Auto-generated method stub
}
}
Tiger class:
package lab11ask1;
import java.util.Scanner;
@Override
public void displayInfo()
{
System.out.println("Tiger attributes: ");
System.out.println("Tiger name: "+name);
System.out.println("Tiger age: "+ age);
System.out.println("Tiger habitat: "+ habitat);
}
@Override
public void play() {
// TODO Auto-generated method stub
}
}
Pet class:
package lab11ask1;
Main class:
package lab11ask1;
import java.util.Scanner;
for(int i=0;i<a.length;i++)
{if(i==0 || i==1)
{
a[i].play();
}
a[i].displayInfo();
}
}
Output:
• Drawable Interface:
o Create an interface named Drawable with a method draw.
o The draw method should represent the capability to draw shapes and will be
implemented by classes that can draw various shapes.
Code:
Shape class:
package lab11task2;
Square class:
package lab11task2;
protected double l;
public Square(double a)
{
l=a;
}
return Area;
return P;
}
Circle class:
package lab11task2;
public Circle(double r)
{
radius=r;
}
return Area;
}
return t;
}
Output:
Code:
Student Class:
package lab11task3;
import java.util.Scanner;
public Student() {
subject_marks=0;
student_ID++;
a = student_ID;
Scanner obj = new Scanner(System.in);
System.out.println("Enter your name:");
Name = obj.nextLine();
}
Main class:
package lab11task3;
import java.util.Scanner;
total_subj = obj1.nextInt();
System.out.println("Enter total number of students");
total_stud = obj1.nextInt();
s[i][j].get_subj_info();
}
System.out.println("Displaying info:");
for (int r = 0; r < total_stud; r++) {
for (int t = 0; t < total_subj; t++) {
s[r][t].displayinfo();
}
}
}
}
Output: