forked from IamBisrutPyne/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeMain.java
More file actions
107 lines (92 loc) · 2.65 KB
/
ShapeMain.java
File metadata and controls
107 lines (92 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.util.*;
/**
* Program Title: OOP Inheritance & Polymorphism
* Author: @ashwil-colaco
* Date: 2025-10-07
*
* Description: Demonstrates core OOP concepts (inheritance, polymorphism, abstract classes)
* by creating a Shape abstract class with Circle and Rectangle subclasses.
* The program calculates and prints the area of shapes using polymorphism.
* Time Complexity: O(1) for each area calculation.
* Space Complexity: O(n) for storing Shape objects in an array.
*/
/**
* Abstract class representing a generic shape.
*/
abstract class Shape {
/**
* Calculates and returns the area of the shape.
*
* @return area of the shape
*/
public abstract double getArea();
}
/**
* Class representing a circle shape.
*/
class Circle extends Shape {
private double radius;
/**
* Constructor to initialize the radius of the circle.
*
* @param radius radius of the circle
*/
public Circle(double radius) {
this.radius = radius;
}
/**
* Calculates and returns the area of the circle.
*
* @return area of the circle
*/
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
/**
* Class representing a rectangle shape.
*/
class Rectangle extends Shape {
private double length;
private double width;
/**
* Constructor to initialize the length and width of the rectangle.
*
* @param length length of the rectangle
* @param width width of the rectangle
*/
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
/**
* Calculates and returns the area of the rectangle.
*
* @return area of the rectangle
*/
@Override
public double getArea() {
return length * width;
}
}
/**
* Main class to demonstrate polymorphism with Shape, Circle, and Rectangle.
*/
public class ShapeMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Shape[] shapes = new Shape[2];
System.out.print("Enter the radius of the Circle: ");
double radius = sc.nextDouble();
System.out.print("Enter the length and breadth of the Rectangle: ");
double length = sc.nextDouble();
double breadth = sc.nextDouble();
shapes[0] = new Circle(radius);
shapes[1] = new Rectangle(length, breadth);
for (Shape shape : shapes) {
System.out.println("Area: " + shape.getArea());
}
sc.close();
}
}