Java Program to Calculate Areas of Different Shapes using Method Overloading


import java.util.Scanner;

public class AreaOfShapes {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
do{
System.out.println("1. Square\n2. Circle\n3. Rectangle");
choice = sc.nextInt();

switch (choice){
case 1:
System.out.print("Enter Side : ");
int side = sc.nextInt();
System.out.println("Area : "+area(side)+" units");
break;
case 2:
System.out.print("Enter Radius : ");
Float radius = sc.nextFloat();
System.out.println("Area : "+area(radius)+" units");
break;

case 3:
System.out.print("Enter Breadth : ");
int breath = sc.nextInt();
System.out.println("Area : "+area(1,breath)+" units");
break;
default:
System.out.println("Enter valid option!");
}
}while (choice<4);

}

public static float area(float radius){
return (float)Math.PI * (radius * radius);
}
public static int area(int side){
return side*side;
}
public static int area(int length, int breath){
return length * breath;
}

} 

Output:

1. Square

2. Circle

3. Rectangle

1

Enter Side : 12

Area : 144 units

1. Square

2. Circle

3. Rectangle

1

Enter Side : 2

Area : 4 units

1. Square

2. Circle

3. Rectangle


Comments

Popular posts from this blog

Hackerrank - Quicksort 2 - Sorting

Hackerrank - Day of the Programmer

Hackerrank - UNIQUE ARMSTRONG NUMBER