0% found this document useful (0 votes)
47 views7 pages

Ir Javalab Assignment3

Uploaded by

Binit Karmakar
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)
47 views7 pages

Ir Javalab Assignment3

Uploaded by

Binit Karmakar
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/ 7

ASSIGNMENT-III

Polymorphism, Inheritance, Abstract Class and Interface

1. Write a Java program to create a base class Animal with methods move() and
makeSound(). Create two subclasses Bird and Panthera. Override the move() method in
each subclass to describe how each animal moves. Also, override the makeSound() method
in each subclass to make a specific sound for each animal.

SOURCE CODE:
class Animal {
void move() {
System.out.println("Animal moves ");
}
void makeSound() {
System.out.println("Animal makes a sound\n");
}
}
class Bird extends Animal {
void move() {
System.out.println("Birds fly");
}
}
class Panthera extends Animal{
void makeSound() {
System.out.println("Pather makes Sound");
}
}
public class Main {
public static void main(String args[]) {
Animal bird = new Bird();
Animal panthera = new Panthera();
performAction(bird);
performAction(panthera);
}
static void performAction(Animal animal) {
animal.move();
animal.makeSound();
}
}

OUTPUT:

Birds fly
Animal makes a sound

Animal moves
Pather makes Sound

11
2. Write a Java program to create an abstract class Shape3D with abstract methods
calculateVolume() and calculateSurfaceArea(). Create subclasses Sphere and Cube that
extend the Shape3D class and implement the respective methods to calculate the volume
and surface area of each shape.

Cube Sphere

Total Surface Area = 6*(side)^2 Total Surface Area = 4*∏ *(radius)^2

Volume = (Side)^3 Volume = 4/3 * ∏ * (radius)^3

SOURCE CODE:

abstract class Shape3D {


abstract double calculateVolume();
abstract double calculateSurfaceArea();
}

class Sphere extends Shape3D {


double radius;
Sphere(double radius) {
this.radius=radius;
}
double calculateSurfaceArea() {
return (4.0)* Math.PI*Math.pow(radius,2);
}
double calculateVolume() {
return (4.0/3.0)* Math.PI*Math.pow(radius,3);
}
}

class Cube extends Shape3D {


double side;
Cube(double side) {
this.side=side;
}
double calculateVolume() {
return Math.pow(side,3);
}
double calculateSurfaceArea() {
return 6*Math.pow(side,2);
}
}

12
public class Test {
public static void main(String args[]) {
Sphere sphere = new Sphere(7.0);
Cube cube = new Cube(3.0);
System.out.println("Sphere Volume : "+sphere.calculateVolume());
System.out.println("Sphere Surface Area : "+sphere.calculateSurfaceArea());

System.out.println("\n\nCube Volume : "+sphere.calculateVolume());


System.out.println("Cube Volume : "+sphere.calculateSurfaceArea());
}
}

OUTPUT:

Sphere Volume : 1436.7550402417319


Sphere Surface Area : 615.7521601035994

Cube Volume : 1436.7550402417319


Cube Volume : 615.7521601035994

3. Write a Java program to create an interface Shape with the getArea() method. Create three
classes Rectangle, Circle, and Triangle that implement the Shape interface. Implement the
getArea() method for each of the three classes.

SOURCE CODE:

import java.util.*;
interface Shape {
double getArea();
}
class Rectangle implements Shape {
double length;
double width;
Rectangle(double length,double width) {
this.length=length;
this.width=width;
}
public double getArea() {
return length*width;
}
}
class Circle implements Shape {
double radius ;

13
Circle(double radius) {
this.radius=radius;
}
public double getArea() {
return 2.0 * Math.PI * Math.pow(radius,2);
}
}
class Triangle implements Shape {
double height,base;

Triangle(double height,double base) {


this.height=height;
this.base=base;
}
public double getArea() {
return (0.50) *base * height;
}
}
public class Calculation{
public static void main (String args[]) {
Scanner sc = new Scanner(System.in);
double r,l,w,b,h;
System.out.println("Enter the radius of the circle : ");
r=sc.nextDouble();
Circle circle = new Circle(r);
System.out.println("Area of the Circle : " + circle.getArea());

System.out.println("Enter the height of the triangle : ");


h=sc.nextDouble();
System.out.println("Enter the base of the triangle : ");
b=sc.nextDouble();
Triangle triangle = new Triangle(h,b);
System.out.println("Area of the Triangle : " + triangle.getArea());

System.out.println("Enter the length of the rectangle : ");


l=sc.nextDouble();
System.out.println("Enter the width of the rectangle : ");
w=sc.nextDouble();
Rectangle rectangle = new Rectangle(l,w);
System.out.println("Area of the Rectangle : " + rectangle.getArea());
}
}

14
OUTPUT:

Enter the radius of the circle :


10
Area of the Circle : 628.3185307179587
Enter the height of the triangle :
2
Enter the base of the triangle :
5
Area of the Triangle : 5.0
Enter the length of the rectangle :
30
Enter the width of the rectangle :
4
Area of the Rectangle : 120.0

4. Write a Java program to create a class called Shape with methods called getPerimeter()
and getArea(). Create a subclass called Circle that overrides the getPerimeter() and
getArea() methods to calculate the area and perimeter of a circle.

SOURCE CODE:
class Shape {
double getPerimeter() {
return 0.0;
}
double getArea() {
return 0.0;
}
}
class Circle extends Shape {
double radius;
public Circle(double radius) {
this.radius = radius;
}
double getPerimeter() {
return 2 * Math.PI * radius;
}
double getArea() {
return Math.PI * Math.pow(radius,2);
}
}

15
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(7);
System.out.println("Perimeter of the circle : " + circle.getPerimeter());
System.out.println("Area of the circle : " + circle.getArea());
}
}

OUTPUT:

Perimeter of the circle : 43.982297150257104


Area of the circle : 153.93804002589985

5. Write a Java program to create a class called Employee with methods called work() and
getSalary(). Create a subclass called HRManager that overrides the work() method and
adds a new method called addEmployee().

SOURCE CODE:

class Employee {
String name;
double salary;
public Employee(String name, double salary) {
this.name=name;
this.salary=salary;
}
public void work() {
System.out.println(name+" is working");
}
public double getsalary() {
return salary;
}
}
class HRManager extends Employee {
public HRManager(String name,double salary) {
super(name,salary);
}
public void work() {
System.out.println(name +"is managing HR tasks");
}

16
public void addEmployee(String name) {
System.out.println(name + " is new Employee");
}
}

public class Main {


public static void main(String[] args) {
Employee employee= new Employee("Ramesh",500000.0);
HRManager hrm = new HRManager("Jenny",600000.0);

employee.work();
System.out.println("Employee salary: " + employee.getsalary());
hrm.work();
System.out.println("Employee salary: " + hrm.getsalary());
hrm.addEmployee("Alice");
}
}

OUTPUT:

Ramesh is working
Employee salary: 500000.0

Jenny is managing HR tasks


Employee salary: 600000.0

Alice is new Employee

17

You might also like