0% found this document useful (0 votes)
23 views15 pages

Task 1 Extended Animal Class Hierarchy With Pet Interface - Done

Uploaded by

Dur Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views15 pages

Task 1 Extended Animal Class Hierarchy With Pet Interface - Done

Uploaded by

Dur Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

DEPARTMENT OF COMPUTER & SOFTWARE ENGINEERING

COLLEGE OF E&ME, NUST, RAWALPINDI

LAB REPORT X

SUBJECT NAME:

OOP

INSTRUCTOR:

MAM MAHNOOR

SUBMITTED BY:

NAME: Dur E Haider Hussain Fayyaz

REG NO: 404018

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;

public abstract class Animal implements Pet {


protected String name;
protected int age;
public Animal(String n, int a)
{
name=n;
age=a;
}
public String gate_name()
{
return name;
}
public int get_age()
{
return age;
}
public abstract void play();

public abstract void displayInfo()


;
}
Cat class:
package lab11ask1;

import java.util.Scanner;

public class Cat extends Animal implements Pet{


public String color;

public Cat(String n, int a) {


super(n, a);

Scanner obj= new Scanner(System.in);


System.out.println("Enter color of Cat:");
color=obj.nextLine();
}

@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;

public class Dog extends Animal implements Pet{


public String breed;
public Dog(String name, int age)
{
super (name, age);
Scanner obj= new Scanner(System.in);

System.out.println("Enter breed of Dog:");


breed=obj.nextLine();
}
@Override
public void play()
{
System.out.println("Dog says woof");
}
@Override
public void displayInfo()
{
System.out.println("Dog attributes: ");
System.out.println("Dog name: "+name);
System.out.println("Dog age: "+ age);
System.out.println("Dog breed: "+ breed);
}
}

Fish class:
package lab11ask1;

import java.util.Scanner;

public class Fish extends Animal {


public String watertype;
public Fish(String name, int age)
{
super (name, age);
Scanner obj= new Scanner(System.in);
System.out.println("Enter watertype of Fish:");
//
watertype=obj.nextLine();
}

@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;

public class Tiger extends Animal {


public String habitat;
public Tiger(String name, int age)
{
super (name, age);
Scanner obj= new Scanner(System.in);
System.out.println("Enter habitat of Tiger:");
habitat=obj.nextLine();
}

@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;

public interface Pet {


public void play();
}

Main class:
package lab11ask1;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Animal a[]=new Animal[10];


a[0]=new Dog("husk", 12);

a[1]=new Cat("kitts", 15);


a[2]=new Tiger("Mufasa", 43);
a[3]=new Fish("F", 3);

for(int i=0;i<a.length;i++)
{if(i==0 || i==1)
{
a[i].play();
}
a[i].displayInfo();
}
}

Output:

Task: Shape Class Hierarchy for Circle and Square


• Shape Class:
o Create a base class named Shape with methods calculateArea and
calculatePerimeter.
o Implement default behavior for these methods by returning 0.0.
• Circle Class (extends Shape):
o Create a derived class named Circle that extends the Shape class.
o Include an attribute radius for the circle.
o Implement the calculateArea and calculatePerimeter methods specific to
a circle.
• Square Class (extends Shape):
o Create another derived class named Square that extends the Shape class.
o Include an attribute side for the square.
o Implement the calculateArea and calculatePerimeter methods specific to
a square.
o Implement the draw method to draw a square using a Turtle API
https://sites.google.com/a/asmsa.org/java-turtle/home.

• 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;

public class Shape {

public double calculate_Area()


{
return 0.0;
}

public double calculate_Perimeter()


{
return 0.0;
}

Square class:
package lab11task2;

public class Square extends Shape implements Drawable{

protected double l;

public Square(double a)
{
l=a;
}

public double calculate_Area()


{
double Area;
Area=l*l;

return Area;

public double calculate_Perimeter()


{
double P;
P=4*l;

return P;
}

public void draw()


{
Turtle d=new Turtle();
d.forward(l);
d.left(90);
d.forward(l);
d.left(90);
d.forward(l);
d.left(90);
d.forward(l);

Circle class:
package lab11task2;

public class Circle extends Shape {


protected double radius;

public Circle(double r)
{
radius=r;
}

public double calculate_Area()


{
double Area;
Area=3.14*radius*radius;

return Area;
}

public double calculate_Perimeter()


{//it's circumference
double t;
t=2*3.14*radius;

return t;
}

Output:

Task 3: Student Grades Analysis


Implement a Java program to analyze student grades using a 2D array. The program should
perform the following operations:
o Read a 2D array representing student grades. Each row represents a student,
and each column represents a subject.
o Calculate the average grade for each student.
o Calculate the average grade for each subject.
o Find and display the student with the highest average grade.
o Find and display the subject with the highest average grade.
o Allow the user to search for a specific student by providing their ID.
o Display the entire 2D array with student IDs and grades.

Code:

Student Class:
package lab11task3;
import java.util.Scanner;

public class Student {


private static double student_marks;
private double subject_marks;
private int student_ID;
private int a;
private static int student_counter;
private static int subject_counter;
private double max_stud;
private double max_subj;
private String Name;

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();
}

public double get_total_marks() {


return student_marks;
}

public double get_subj_info() {


Scanner obj1 = new Scanner(System.in);
double c = 0;
System.out.println("Enter subject marks:");
c = obj1.nextDouble();
// Initialize subject_marks to 0 in the constructor if not already
done
subject_marks += c;
student_marks = subject_marks;
System.out.println("Your total marks are: " + student_marks);
subject_counter++;
return c;
}

public String get_stud_name() {


return Name;
}

public double get_stud_avg_grade() {


double j=subject_marks / subject_counter;
return j;
}

public double get_subj_avg_grade() {


return (student_marks / student_counter);
}

public double get_ID() {


return a;
}

public void set_max_stud_grade(double maxx) {


max_stud = maxx;
}

public void set_max_subj_grade(double maxxx) {


max_subj = maxxx;
}

public void displayinfo() {


System.out.println("Name: " + Name + " ID: " + a);
double student_avg_grade = get_stud_avg_grade();
System.out.println("Student Average Grade: " + student_avg_grade);

if (student_avg_grade >= 90) {


System.out.println("Grade: A");
} else if (student_avg_grade >= 80) {
System.out.println("Grade: B");
} else if (student_avg_grade >= 70) {
System.out.println("Grade: C");
} else if (student_avg_grade >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
}
}

Main class:
package lab11task3;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {


int total_subj = 0;
int total_stud = 0;
double max_subj = 0;
double checker = 0;
double checker2 = 0;
double max_stud = 0;

Scanner obj1 = new Scanner(System.in);


System.out.println("Enter total number of subjects:");

total_subj = obj1.nextInt();
System.out.println("Enter total number of students");

total_stud = obj1.nextInt();

Student[][] s = new Student[total_stud][total_subj];

for (int i = 0; i < total_stud; i++) {


int j = 0;s[i][j] = new Student();

for (; j < total_subj; j++) {

s[i][j].get_subj_info();
}

checker = s[i][total_subj - 1].get_subj_avg_grade();


if (checker > max_subj) {
max_subj = checker;
}
}

for (int h = 0; h < total_stud; h++) {


for (int u = 0; u < total_subj; u++) {
checker2 = s[h][u].get_stud_avg_grade();
if (checker2 > max_stud) {
max_stud = checker2;
}
}
}

for (int e = 0; e < total_stud; e++) {


for (int x = 0; x < total_subj; x++) {
checker = s[e][x].get_subj_avg_grade();
if (checker > max_subj) {
max_subj = checker;
}
}
}

System.out.println("Max average grade of student: " + max_stud);


System.out.println("Max average grade of subj: " + max_subj);
int g = 0;
int value = 0;
Scanner obj4 = new Scanner(System.in);
System.out.println("Enter 1 for finding ID of a specific student"
+ " or '0' for ignoring or quitting:");
value = obj4.nextInt();
do {
Scanner obj5 = new Scanner(System.in);
System.out.println("Enter ID: ");
g = obj5.nextInt();
for (int w = 0; w < total_stud; w++) {
if (g == s[w][0].get_ID()) {
System.out.println("Student has been found");
break; // exit the loop once the student is found
}
}

System.out.println("Enter 1 for finding ID of a specific student"


+ " or '0' for ignoring or quitting:");
value = obj4.nextInt();
} while (value != 0);

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:

You might also like