Write a menu driven Java program to calculate the Area of Square, Rectangle and Circle according to user’s choice. Hint: Area of Square – Side 2 Area of Rectangle – Length x Breadth Area of circle – (22/7) x r2
Answers
Answer:
class OverloadDemo
{
void area(float x)
{
System.out.println("the area of the square is "+Math.pow(x, 2)+" sq units");
}
void area(float x, float y)
{
System.out.println("the area of the rectangle is "+x*y+" sq units");
}
void area(double x)
{
double z = 3.14 * x * x;
System.out.println("the area of the circle is "+z+" sq units");
}
}
class Overload
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
ob.area(5);
ob.area(11,12);
ob.area(2.5);
}
}
Output:
the area of the square is 25.0 sq units
the area of the rectangle is 132.0 sq units
the area of the circle is 19.625 sq units
Code:
import java.util.Scanner;
public class Areas {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter - \n" +
" 1. Square \n" +
" 2. Rectangle \n" +
" 3. Circle");
switch (sc.nextInt()) {
case 1:
System.out.print("Enter the Side of the Square - ");
float side = sc.nextFloat();
System.out.println("Area - " + (side * side) + " unit square");
break;
case 2:
System.out.print("Enter Length of the Rectangle - ");
float length = sc.nextFloat();
System.out.print("Enter Breadth of the Rectangle - ");
float breadth = sc.nextFloat();
System.out.println("Area - " + (length * breadth) + " unit square");
break;
case 3:
System.out.print("Enter the Radius of the Circle - ");
float radius = sc.nextFloat();
System.out.println("Area - " + (Math.PI * radius * radius) + " unit square");
break;
default:
System.out.println("Invalid Choice!");
}
}
}
Sample I/O:
Enter -
1. Square
2. Rectangle
3. Circle
Enter Choice - 2
Enter Length of the Rectangle - 12
Enter Breadth of the Rectangle - 23
Area - 276.0 unit square