0% found this document useful (0 votes)
3 views3 pages

Dynamic Binding in Shape Area Calculation

Uploaded by

lomtenavnath28
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)
3 views3 pages

Dynamic Binding in Shape Area Calculation

Uploaded by

lomtenavnath28
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

Experiment 04:

/*
[Link] Binding: Design a base class shape with two double type values and member
functions to input the data and compute_area() for calculating area of shape.
Derive two classes: triangle and rectangle. Make compute_area() as an abstract
function and redefine this function in the derived class to suit their
requirements. Write a program that accepts dimensions of triangle/rectangle and
displays calculated area. Implement dynamic binding for given case study
*/

import [Link].*;

abstract class Shape{


double length;
double breath;

Shape(double length, double breath){


[Link] = length;
[Link] = breath;
}

abstract void calc_area();


}

class Rectangle extends Shape{


Rectangle(double length, double breath){
super(length, breath);
}

void calc_area() {
[Link]("The area of rectangle is: " + length * breath);
}
}

class Triangle extends Shape{


Triangle(double length, double breath){
super(length, breath);
}

void calc_area() {
[Link]("The area of triangle is: " + (0.5 * length * breath));
}
}
public class Assignment_04 {

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("Enter R: Rectagle, T: Triangle, E: Exit : ");
String res = [Link]();

do {
switch(res) {
case "R" -> {
[Link]("Enter lenght: ");
double length = [Link]();
[Link]("Enter breath: ");
double breath = [Link]();
Rectangle R1 = new Rectangle(length, breath);
R1.calc_area();
}
case "T" -> {
[Link]("Enter lenght: ");
double length = [Link]();
[Link]("Enter breath: ");
double breath = [Link]();
Triangle T1 = new Triangle(length, breath);
T1.calc_area();
}
default -> {
[Link]("Invalid input!");
}
}
[Link]("Do you want continue: Y/N?: ");
String input = [Link]();
if("N".equals(input)) {
break;
}else {
[Link]("Enter R: Rectagle, T: Triangle, E: Exit : ");
res = [Link]();
}
}while(!"E".equals(res));
}
}

Output:
Enter R: Rectangle, T: Triangle, E: Exit : T
Enter length: 44
Enter breath: 17
The area of triangle is: 374.0
Do you want continue: Y/N?: y
Enter R: Rectangle, T: Triangle, E: Exit : R
Enter length: 19
Enter breath: 11
The area of rectangle is: 209.0
Do you want continue: Y/N?: N

You might also like