Answer to Question #267543 in Java | JSP | JSF for saviour

Question #267543

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.

1
Expert's answer
2021-11-17T17:37:06-0500
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");
		}
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS