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

Assignment 4

The document contains 3 programming questions involving OOP concepts like inheritance, polymorphism, packages and importing classes. 1) Defines classes Degree, Undergraduate and Postgraduate to demonstrate inheritance and method overriding. 2) Defines abstract class Shapes and subclasses Circle, Square, Rectangle and Triangle to calculate their areas using the same method name via polymorphism. 3) Shows how to create packages and import classes from a user-defined package into another package.

Uploaded by

rerere
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

Assignment 4

The document contains 3 programming questions involving OOP concepts like inheritance, polymorphism, packages and importing classes. 1) Defines classes Degree, Undergraduate and Postgraduate to demonstrate inheritance and method overriding. 2) Defines abstract class Shapes and subclasses Circle, Square, Rectangle and Triangle to calculate their areas using the same method name via polymorphism. 3) Shows how to create packages and import classes from a user-defined package into another package.

Uploaded by

rerere
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Name: Ricky George Kalathamattathil Reg.no.

: 22BCE7765

Assignment-4 DATE-10/04/2023

1. Create a class 'Degree' having a method 'getDegree' that prints "I got a
degree". It has two subclasses namely 'Undergraduate' and 'Postgraduate'
each having a method with the same name that prints "I am an
Undergraduate" and "I am a Postgraduate" respectively. Call the method
by creating an object of each class and print the student details (name,
regno, degree, years) by creating a method “display” in class Degree.

Code:

class Degree

String name;

String regno;

String degree;

int years;

public Degree(String name, String regno, String degree, int years)

this.name = name;

this.regno = regno;

this.degree = degree;

this.years = years;

public void getDegree()

{
System.out.println("I got a degree");

public void display()

System.out.println("Name of the student "+name);

System.out.println("Registration number of the student "+regno);

System.out.println("Degree of the student "+degree);

System.out.println("Number of years "+years);

class Undergraduate extends Degree

public Undergraduate(String name, String regno, String degree, int years)

super(name, regno, degree, years);

public void getDegree()

System.out.println("I am an Undergraduate");

}
class Postgraduate extends Degree

Postgraduate(String name, String regno, String degree, int years)

super(name, regno, degree, years);

public void getDegree()

System.out.println("I am an Postgraduate");

class Q1

public static void main(String [] args)

Degree obj1 = new Undergraduate("Ricky","18BCE7765","B.Tech CSE", 4);

Degree obj2 = new Postgraduate("Sagar","20BCE7580","M.Tech", 2);

obj1.getDegree();

obj1.display();

obj2.getDegree();

obj2.display();

}
Output:

2. Create a class Shapes with a method “calculatearea()” to find the area of a


circle (πr2), square(a2), rectangle(w X h) and triangle(1/2 b h) using the
same method name “calculatearea” in each class. Declare the input for
calculation in class as private (r,a,w,h,b).

Code:

class Shapes

private double r;

private double a;

private double w;

private double h;

private double b;

Shapes(double r,double a, double w, double h, double b)

this.r = r;

this.a = a;

this.w = w;

this.h = h;

this.b = b;
}

public void setr(double newr)

r = newr;

public double getr()

return r;

public void seta(double newa)

a = newa;

public double geta()

return a;

public void setw(double neww)

w = neww;

public double getw()

{
return w;

public void seth(double newh)

h = newh;

public double geth()

return h;

public void setb(double newb)

b = newb;

public double getb()

return b;

public void calculatearea()

System.out.println("Area is 0");

}
class circle extends Shapes

circle(double r,double a, double w, double h, double b)

super(r,a,w,h,b);

public void calculatearea()

System.out.println("Area of circle is "+(3.14*getr()*getr()));

class square extends Shapes

square(double r,double a, double w, double h, double b)

super(r,a,w,h,b);

public void calculatearea()

System.out.println("Area of square is "+(geta()*geta()));

}
}

class rectangle extends Shapes

rectangle(double r,double a, double w, double h, double b)

super(r,a,w,h,b);

public void calculatearea()

System.out.println("Area of rectangle is "+(getw()*geth()));

class triangle extends Shapes

triangle(double r,double a, double w, double h, double b)

super(r,a,w,h,b);

public void calculatearea()

System.out.println("Area of triangle is "+(0.5*getb()*geth()));


}

class Q2

public static void main(String [] args)

Shapes obj1 = new circle(2.0,0,0,0,0);

Shapes obj2 = new square(0,2.0,0,0,0);

Shapes obj3 = new rectangle(0,0,2.0,4.0,0);

Shapes obj4 = new triangle(0,0,0,2.0,4.0);

obj1.calculatearea();

obj2.calculatearea();

obj3.calculatearea();

obj4.calculatearea();

Output:

3. Write a Java program to implement the concept of importing classes from


user defined package and creating packages.

Code:
package example;

public class code1

public void show()

System.out.println("Hi! How are you?");

public static void main(String [] args)

code1 obj = new code1();

obj.show();

package Questions;

import example.code1;

public class code2

public static void main(String [] args)

code1 obj = new code1();

obj.show();
}

Output:

You might also like