forked from chetannihith/Java-hacktoberfest25
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArea.java
More file actions
68 lines (56 loc) · 1.88 KB
/
Area.java
File metadata and controls
68 lines (56 loc) · 1.88 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
import java.util.Scanner;
public class Area{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Choose the shape (1 for square, 2 for rectangle):");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the side length of the square:");
double length = sc.nextInt();
AreaCalculate square = new AreaCalculate(length);
square.calculateSquareArea();
break;
case 2:
System.out.println("Enter the length of the rectangle:");
double r_length = sc.nextDouble();
System.out.println("Enter the width of the rectangle:");
double r_width = sc.nextDouble();
AreaCalculate rectangle = new AreaCalculate(r_length,r_width);
rectangle.calculateRectangleArea();
break;
default:
System.out.println("Invalid choice");
}
}
}
class AreaCalculate{
double length;
double width;
// Constructor Overloaded for square or Rectangle.
AreaCalculate(double length) {
this.length = length;
}
AreaCalculate(double length, double width) {
this.length = length;
this.width = width;
}
void calculateSquareArea() {
if(length < 0){
System.out.println("Invalid Length Entered");
}
else{
double area = length * length;
System.out.println("Area of the square : "+area);
}
}
void calculateRectangleArea() {
if(length < 0 || width < 0){
System.out.println("Invalid Dimensions Entered");
}
else{
double area = length * width;
System.out.println("Area of the Rectangle : "+area);
}
}
}