-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodOverloading.java
More file actions
66 lines (49 loc) · 1.77 KB
/
MethodOverloading.java
File metadata and controls
66 lines (49 loc) · 1.77 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
import java.util.Scanner;
public class MethodOverloading{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Calculate Perimeter of different shapes");
System.out.println("Enter 1 to Calculate Perimeter of A Square");
System.out.println("Enter 2 to Calculate Perimeter of a Rectangle");
System.out.println("Enter 3 to Calculate Perimeter of a Circle");
System.out.println();
System.out.println("Enter your choice");
int choice = input.nextInt();
switch(choice){
case 1:{
System.out.println(" Enter the length of the square: ");
int lengthofSquare = input.nextInt();
MethodOverloading.shape(lengthofSquare);
}
break;
case 2:{
System.out.println(" Enter the length of the rectangle: ");
int lengthofRectangle = input.nextInt();
System.out.println(" Enter the breadth of the rectangle: ");
int breadthofRectangle = input.nextInt();
MethodOverloading.shape(lengthofRectangle, breadthofRectangle);
}
break;
case 3:{
System.out.println(" Enter the radius of the circle: ");
double radiusofCircle = input.nextDouble();
MethodOverloading.shape(radiusofCircle);
}
break;
default:
System.out.println("Invalid input");
}
}
public static void shape(int length){
int perimeterOfSquare = 4 * length;
System.out.printf("The perimeter of a Square is %d%n", perimeterOfSquare);
}
public static void shape(int length, int breadth){
int perimeterOfRectangle = 2*(length + breadth);
System.out.printf("The perimeter of a Rectangle is %d%n", perimeterOfRectangle);
}
public static void shape(double radius){
double perimeterOfCircle = 2* Math.PI * radius;
System.out.printf("The perimeter of a Circle is %d%n", perimeterOfCircle);
}
}