-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeAreaCalculator.java
More file actions
112 lines (95 loc) · 5.46 KB
/
ShapeAreaCalculator.java
File metadata and controls
112 lines (95 loc) · 5.46 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
108
109
110
111
112
import java.util.Scanner;
// Abstract base class Shape
abstract class Shape {
abstract double calculateArea();
}
// Derived class Circle
class Circle extends Shape {
private double radius;
// Constructor
public Circle(double radius) {
this.radius = radius;
}
@Override
double calculateArea() {
return Math.PI * radius * radius;
}
}
// Derived class Rectangle
class Rectangle extends Shape {
private double length;
private double width;
// Constructor
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
double calculateArea() {
return length * width;
}
}
// Derived class Triangle
class Triangle extends Shape {
private double base;
private double height;
// Constructor
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
@Override
double calculateArea() {
return 0.5 * base * height;
}
}
// Main class
public class ShapeAreaCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(" ███████╗██╗ ██╗ █████╗ ██████╗ ███████╗ █████╗ ██████╗ ███████╗ █████╗ ");
System.out.println(" ██╔════╝██║ ██║██╔══██╗██╔══██╗██╔════╝ ██╔══██╗██╔══██╗██╔════╝██╔══██╗");
System.out.println(" ███████╗███████║███████║██████╔╝█████╗█████╗███████║██████╔╝█████╗ ███████║");
System.out.println(" ╚════██║██╔══██║██╔══██║██╔═══╝ ██╔══╝╚════╝██╔══██║██╔══██╗██╔══╝ ██╔══██║");
System.out.println(" ███████║██║ ██║██║ ██║██║ ███████╗ ██║ ██║██║ ██║███████╗██║ ██║");
System.out.println(" ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚══════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝");
System.out.println(" ██████╗ █████╗ ██╗ ██████╗██╗ ██╗██╗ █████╗ ████████╗ ██████╗ ██████╗ ");
System.out.println("██╔════╝██╔══██╗██║ ██╔════╝██║ ██║██║ ██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗");
System.out.println("██║ ███████║██║ ██║ ██║ ██║██║ ███████║ ██║ ██║ ██║██████╔╝");
System.out.println("██║ ██╔══██║██║ ██║ ██║ ██║██║ ██╔══██║ ██║ ██║ ██║██╔══██╗");
System.out.println("╚██████╗██║ ██║███████╗╚██████╗╚██████╔╝███████╗██║ ██║ ██║ ╚██████╔╝██║ ██║");
System.out.println(" ╚═════╝╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝");
System.out.println("\nChoose a shape to calculate the area");
System.out.println("1. Circle");
System.out.println("2. Rectangle");
System.out.println("3. Triangle");
int choice = scanner.nextInt();
Shape shape = null;
switch (choice) {
case 1:
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
shape = new Circle(radius);
break;
case 2:
System.out.print("Enter the length of the rectangle: ");
double length = scanner.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double width = scanner.nextDouble();
shape = new Rectangle(length, width);
break;
case 3:
System.out.print("Enter the base of the triangle: ");
double base = scanner.nextDouble();
System.out.print("Enter the height of the triangle: ");
double height = scanner.nextDouble();
shape = new Triangle(base, height);
break;
default:
System.out.println("Invalid choice!");
System.exit(0);
}
System.out.println("The area is: " + shape.calculateArea());
scanner.close();
}
}