Write a program using a function called area() to compute the area of a:
i. circle(it* r) where t = 3.14
ii. square(side*side)
iii. rectangle (length*breadth)
Display the menu to output the area as per User's choice...plse do it fast..it is argent(without using scanner)
Answers
Explanation:
import java.util.Scanner;
public class AreaOfChoice {
// Function to compute the area of circle
private double areaOfCircle(int radius) {
double pi = 3.14;
double area = pi * Math.pow(radius, 2);
return area;
}
// Function to compute the area of square
private double areaOfSquare(int side) {
double area = side * side;
return area;
}
// Function to compute the area of rectangle
private double areaOfRectangle(int length, int breadth) {
double area = length * breadth;
return area;
}
public double area() {
Scanner input = new Scanner(System.in);
System.out.println(" Enter the Choice");
System.out.println("1. Area Of Circle 2. Area of Square 3. Area of Rectangle");
int choice = input.nextInt();
double area;
switch (choice) {
case 1:
System.out.println("Enter the radius");
int radius = input.nextInt();
area = areaOfCircle(radius);
break;
case 2:
System.out.println("Enter the side");
int side = input.nextInt();
area = areaOfSquare(side);
break;
case 3:
System.out.println("Enter the Length");
int length = input.nextInt();
System.out.println("Enter the Breadth");
int breadth = input.nextInt();
area = areaOfRectangle(length, breadth);
break;
default:
area = -1;
}
return area;
}
public static void main(String args[]) {
AreaOfChoice obj = new AreaOfChoice();
double area = obj.area();
if (area == -1) {
System.out.println(" Invalid choice");
} else {
System.out.println(" The area is " + area);
}
}
}
Explanation:
The question is little ambiguous if we are expected to use overloading , but there are two parameters of type int for rectangle and square that restricts us from going with overloading .
Tried to fulfill all the asks from examiner
1. Menu Driven Program
2. Print the area based on choice
3. The function area() should be responsible for calculating
4. More private functions according to convenience