Oop 4
Oop 4
04
Name:
Roll.No.:
/* 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 java.util.*;
abstract class shape
{
double val1,val2;
void input()
{
Scanner s =new Scanner(System.in);
System.out.println("Enter the first value");
val1= s.nextDouble();
System.out.println("Enter the second value");
val2= s.nextDouble();
}
abstract void compute_area();
}
}
}
class Dynamic
{
public static void main(String []args)
{
shape s;
Triangle t= new Triangle();
Rectangle r = new Rectangle();
s=t;
s.input();
s.compute_area();
s=r;
s.input();
s.compute_area();
}
}
Output :
Enter the first value
10
Enter the second value
20
--Calculate Traingle Area--
Traingle area:100.0