0% found this document useful (0 votes)
19 views

Calculate: Length Breadth Area Length Breadth

The document defines an abstract Shape class with a Calculate method, and Rectangle and Triangle classes that extend Shape and override Calculate to calculate their respective areas. The Main class takes user input for a rectangle's length and breadth, and a triangle's base and height, creates instances of Rectangle and Triangle, and calls Calculate on each to output their areas.

Uploaded by

Supratim Sircar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Calculate: Length Breadth Area Length Breadth

The document defines an abstract Shape class with a Calculate method, and Rectangle and Triangle classes that extend Shape and override Calculate to calculate their respective areas. The Main class takes user input for a rectangle's length and breadth, and a triangle's base and height, creates instances of Rectangle and Triangle, and calls Calculate on each to output their areas.

Uploaded by

Supratim Sircar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

package Q5;

abstract public class Shape {


abstract public void Calculate();
}

package Q5;
public class Rectangle extends Shape {
float length, breadth;
float area;
public Rectangle(float l, float b) {
length = l;
breadth = b;
}
public void Calculate() {
area = length * breadth;
System.out.println("Area of Rectangle =
"+area+"\n");
}
}

package Q5;
public class Triangle extends Shape {
float base, height;
float area;
public Triangle(float b, float h)
{
base = b;
height = h;
}
public void Calculate() {
area = 0.5f * base * height;
System.out.println("Area of Triangle =
"+area+"\n");
}
}
import Q5.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter length and breadth for
Rectangle : ");
float lenRect=sc.nextFloat();
float brRec=sc.nextFloat();
Rectangle R;
R = new Rectangle(lenRect, brRec);
R.Calculate();
System.out.println("Enter base and height for
Triangle : ");
float baseTri=sc.nextFloat();
float hgtTri=sc.nextFloat();
Triangle T;
T = new Triangle(baseTri, hgtTri);
T.Calculate();
sc.close();
}
}

INPUT/OUTPUT:
Enter length and breadth for Rectangle :
34.5
23.2
Area of Rectangle = 800.4

Enter base and height for Triangle :


23.4
34.9
Area of Triangle = 408.33002

You might also like