Assignment No.
04
Name:
[Link].:
/* 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 abstract function and redefine this function in the derived class to suit their
requirements. Write a program that accepts dimensions of triangle/rectangle and display calculated area.
Implement dynamic binding for given case study. */
Source Code :
import [Link].*;
abstract class shape
{
double val1,val2;
void input()
{
Scanner s =new Scanner([Link]);
[Link]("Enter the first value");
val1= [Link]();
[Link]("Enter the second value");
val2= [Link]();
}
abstract void compute_area();
}
class Triangle extends shape
{
void compute_area()
{
double area;
area = 0.5 * val1 * val2;
[Link]("--Calculate Traingle Area--");
[Link]("Traingle area:" + area);
}
class Rectangle extends shape
{
void compute_area()
{
double area;
area = val1*val2;
[Link]("--Calculate Rectangle Area--");
[Link](" Rectangle area"+ area);
}
}
class Dynamic
{
public static void main(String []args)
{
shape s;
Triangle t= new Triangle();
Rectangle r = new Rectangle();
s=t;
[Link]();
s.compute_area();
s=r;
[Link]();
s.compute_area();
}
}
Output :
Enter the first value
10
Enter the second value
20
--Calculate Traingle Area--
Traingle area:100.0
Enter the first value
10
Enter the second value
20
--Calculate Rectangle Area--
Rectangle area200.0
Process finished with exit code 0