OOPs Imp Questions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Assignment - 7

Name – Sourasish Mondal


University Roll No. – 11500221053
Stream – CSE-B Group – 2
Subject – Object Oriented Programming
Subject Code – PCC-CS593

Problem Statement:

1. Define an abstract class figure. Define the area and volume method in the
child classes. Use dynamic method dispatch.

Code:
public class assignment7{
public static void main(String[] args){
Figure rectangle = new TwoDFigure(5, 10);
Figure box = new ThreeDFigure(10, 30, 20);

System.out.println("The area of the rectangle is:


" + rectangle.area());
System.out.println("The area of the box is : " +
box.volume());
}
}

abstract class Figure{


abstract double area();
abstract double volume();
}

class TwoDFigure extends Figure{


double length;
double breadth;

TwoDFigure(double length, double breadth){


this.length = length;
this.breadth = breadth;
}

@Override
double area(){
return length * breadth;
}

@Override
double volume(){
return 0; //not applicable for two dimension
figure
}
}
class ThreeDFigure extends Figure{
double length;
double breadth;
double height;

ThreeDFigure(double length, double breadth, double


height){
this.length = length;
this.breadth = breadth;
this.height = height;
}
@Override
double area(){
return 2*(length*breadth + breadth*height +
length*height);
}

@Override
double volume(){
return length * breadth * height;
}
}

Output:
2. Implement the following design with suitable example classes.

Code:

interface A {
void doSomething();
}
interface A1 {
void doSomething1();
}
abstract class C {
abstract void performAction();
}
class D extends C implements A {
@Override
void performAction() {
System.out.println("Class D performing an action.");
}
@Override
public void doSomething() {
System.out.println("Class D is doing something.");
}
public void doSomething1() {
System.out.println("Class D is doing something again.");
}
}

public class assignment7ii {


public static void main(String[] args) {
D d = new D();
d.performAction();
d.doSomething();
d.doSomething1();
}
}

Output:

You might also like