-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram44.java
48 lines (42 loc) · 1.38 KB
/
Program44.java
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
/* Program 44
Write a program to overload the function area(). Accept values from the user and print out the area of a square, rectangle and triangle.
04/06/24 */
import java.util.Scanner;
public class Program44 {
static double area(int s) { // square
return s * s;
}
static double area(int l, int b) { // rectangle
return l * b;
}
static double area(double b, double h) { // triangle
return 0.5 * b * h;
}
static double input(String p) {
Scanner sc = new Scanner(System.in);
System.out.print(p);
return sc.nextDouble();
}
public static void main(String args[]) {
int choice = (int)input("Enter choice\n1. Square\n2. Rectangle\n3. Triangle\n");
double res = -1;
switch (choice) {
case 1:
res = area((int)input("Enter side length: "));
break;
case 2:
int l = (int)input("Enter length: ");
int b = (int)input("Enter breadth: ");
res = area(l, b);
break;
case 3:
double base = input("Enter base: ");
double h = input("Enter height: ");
res = area(base, h);
break;
default:
System.out.println("Invalid input");
}
System.out.println("The area is " + res);
}
}