program that computes the area of either a rectangle, a circle or a right-angled triangle. The program should display a menu that enables the user to select the type of figure whose area he/she wants to compute. Depending on the user’s choice, the program should prompt for the dimensions and perform the computations. The output should be: - The type of figure, the dimensions and the area. Define three functions: - one to compute the area of a rectangle, one the area of a circle and one the area of a triangle. NB: 1. The calculation should be for only one figure at any one time.
2. Computations should be done in the user-defined functions.
import java.util.Scanner;
public class Main
{
static Scanner in =new Scanner(System.in);
public static void RectangleArea(){
double length,width;
System.out.print("Enter length of the rectangle: ");
length=in.nextDouble();
System.out.print("Enter width of the rectangle: ");
width=in.nextDouble();
double area=length*width;
System.out.println("\nArea of the rectangle = "+area);
}
public static void CircleArea(){
double radius;
System.out.print("Enter radius of the circle: ");
radius=in.nextDouble();
double pi=3.142;
double area=pi*radius*radius;
System.out.println("\nArea of the circle = "+area);
}
public static void TriangleArea(){
double h,b;
System.out.print("Enter base of the triangle: ");
b=in.nextDouble();
System.out.print("Enter height of the triangle: ");
h=in.nextDouble();
double area=0.5*b*h;
System.out.println("\nArea of the triangle = "+area);
}
public static void main(String[] args) {
int c;
System.out.print("\n1. Rectangle\n2. Circle\n3. Triangle\nSelect a figure:");
c=in.nextInt();
if (c==1){
RectangleArea();
}
else if (c==2){
CircleArea();
}
else if (c==3){
TriangleArea();
}
else{
System.out.println("Invalid input");
}
}
}
Comments
Leave a comment