0% found this document useful (0 votes)
2 views4 pages

Java Shape Area Calculation Code

The document contains a Java program that defines an abstract class 'Shape' with derived classes 'Triangle' and 'Rectangle' that implement the method to compute their areas. The main class 'Area1' allows users to choose between calculating the area of a triangle or a rectangle by inputting the necessary dimensions. The program uses dynamic binding to instantiate the appropriate shape and calculate the area based on user input.

Uploaded by

Vaibhav Jagtap
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)
2 views4 pages

Java Shape Area Calculation Code

The document contains a Java program that defines an abstract class 'Shape' with derived classes 'Triangle' and 'Rectangle' that implement the method to compute their areas. The main class 'Area1' allows users to choose between calculating the area of a triangle or a rectangle by inputting the necessary dimensions. The program uses dynamic binding to instantiate the appropriate shape and calculate the area based on user input.

Uploaded by

Vaibhav Jagtap
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

Program code :

// Abstract base class Shape

abstract class Shape

// Two double-type values for dimensions

double dim1,dim2;

// Constructor

Shape(double a,double b)

dim1=a;

dim2=b;

// Abstract method to be implemented in derived classes

abstract void compute_area();

// Derived class for Triangle

class Trangle extends Shape

// Constructor

Trangle(double base,double height)

super(base,height);

// Overriding compute_area() for Triangle

@Override

void compute_area()

{
double area = 0.5*dim1*dim2;

[Link](" Area Of trangl is= "+area);

// Derived class for Rectangle

class Rectangle extends Shape

// Constructor

Rectangle(double length,double breadth)

super(length,breadth);

// Overriding compute_area() for Rectangle

@Override

void compute_area()

double area=dim1*dim2;

[Link](" Area Of Rectangle is= "+area);

// Main class

public class Area1

public static void main(String[] args)

[Link] sc = new [Link]([Link]);

[Link]("============================================");
[Link](" Choose Shape: "+"\n"+ " 1. Triangle "+"\n"+" 2.
Rectangle : ");

int choice = [Link]();

Shape shape;

switch (choice)

case 1:

[Link](" Enter base of triangle: ");

double base = [Link]();

[Link](" Enter height of triangle: ");

double height = [Link]();

shape = new Trangle(base, height); // dynamic binding

shape.compute_area();

break;

case 2:

[Link](" Enter Lenght of Rectangle: ");

double length = [Link]();

[Link](" Enter Bridth of Rectangle: ");

double breadth = [Link]();

shape= new Rectangle(length,breadth); // dynamic binding

shape.compute_area();

break;

default:

[Link](" invalid choice Please Retry");

[Link]();

}
Output :

You might also like