0% found this document useful (0 votes)
8 views2 pages

j10 - Lab Task One

The document contains Java code defining a class hierarchy for geometric shapes, including Circle and Rectangle classes that extend an abstract Shape class. Each shape class has a method to calculate its area and a method to display its name. The main program creates instances of Circle and Rectangle, displays their names, and prints their areas.

Uploaded by

Sarathi M
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)
8 views2 pages

j10 - Lab Task One

The document contains Java code defining a class hierarchy for geometric shapes, including Circle and Rectangle classes that extend an abstract Shape class. Each shape class has a method to calculate its area and a method to display its name. The main program creates instances of Circle and Rectangle, displays their names, and prints their areas.

Uploaded by

Sarathi M
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
You are on page 1/ 2

1)

Code:

public class Circle extends Shape{


double radius;
Circle(double radius){
this.radius=radius;
}
double calculateArea(){
return (22/7)*radius*radius;
}
}

public class Rectangle extends Shape{


private double width;
private double length;

Rectangle(double length, double width) {


this.length = length;
this.width = width;
}

double calculateArea() {
return length * width;
}
}

public abstract class Shape{


abstract double calculateArea();
void sisplayShapeName(String name){
System.out.println("Shape:"+name);
}
}

public class Program {


public static void main(String[] args) {
Circle circle = new Circle(10);
circle.sisplayShapeName("Circle");
System.out.println("Area: " + circle.calculateArea());

Rectangle rectangle = new Rectangle(4, 6);


rectangle.sisplayShapeName("Rectangle");
System.out.println("Area: " + rectangle.calculateArea());
}
}
Output:

You might also like