-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
71 lines (54 loc) · 2.38 KB
/
Copy pathMain.java
File metadata and controls
71 lines (54 loc) · 2.38 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
import java.util.Scanner;
public class Main {
public static double area(double length, double breadth) {
return length * breadth;
}
public static double area(double side) {
return side * side;
}
public static double areaCircle(double radius) {
return 3.14 * radius * radius;
}
public static double areaTriangle(double base, double height) {
return 0.5 * base * height;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Choose the shape to calculate the area:");
System.out.println("1. Rectangle");
System.out.println("2. Square");
System.out.println("3. Circle");
System.out.println("4. Triangle");
System.out.print("Enter your choice (1-4): ");
int choice = scanner.nextInt();
switch (choice) {
case 1: // Rectangle
System.out.print("Enter the length of the rectangle: ");
double length = scanner.nextDouble();
System.out.print("Enter the breadth of the rectangle: ");
double breadth = scanner.nextDouble();
System.out.println("Area of Rectangle: " + area(length, breadth));
break;
case 2: // Square
System.out.print("Enter the side of the square: ");
double side = scanner.nextDouble();
System.out.println("Area of Square: " + area(side));
break;
case 3: // Circle
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
System.out.println("Area of Circle: " + areaCircle(radius));
break;
case 4: // Triangle
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();
System.out.println("Area of Triangle: " + areaTriangle(base, height));
break;
default: // Invalid choice
System.out.println("Invalid choice! Please enter a number between 1 and 4.");
}
scanner.close();
}
}