Answer to Question #312462 in Java | JSP | JSF for Jay

Question #312462

Write a java program that computes the area & perimeter 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 & perimeter he/she wants to compute. Depending on the users choice, the program should prompt for the dimensions and perform the computations. The output should be: - The type of figure, the dimensions, the area and the perimeter. (NB:The calculation should be for only one figure at any one time.)

using object oriented


1
Expert's answer
2022-03-16T07:08:34-0400


import java.util.Scanner;


interface IShape {
	double computesArea();


	double computesPerimeter();
}


class Rectangle implements IShape {
	private double w;
	private double h;


	public Rectangle() {
	}


	public Rectangle(double w, double h) {
		this.w = w;
		this.h = h;
	}


	@Override
	public double computesArea() {
		return w * h;
	}


	@Override
	public double computesPerimeter() {
		return 2 * (w + h);
	}
}


class Circle implements IShape {
	private double radius;


	public Circle() {
	}


	public Circle(double radius) {
		this.radius = radius;
	}


	@Override
	public double computesArea() {
		return Math.PI * radius * radius;
	}


	@Override
	public double computesPerimeter() {
		return 2 * Math.PI * radius;
	}
}


class RightAngledTriangle implements IShape {
	private double w;
	private double h;


	public RightAngledTriangle() {
	}


	public RightAngledTriangle(double w, double h) {
		this.w = w;
		this.h = h;
	}


	@Override
	public double computesArea() {
		return 0.5 * w * h;
	}


	@Override
	public double computesPerimeter() {
		return w + h + Math.sqrt((w * w) + (h * h));
	}


}


public class App {


	/** Main Method */
	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in); // Create a Scanner
		int ch = -1;
		IShape IShape;
		while (ch != 4) {
			System.out.println("1. Computes the area & perimeter a rectangle");
			System.out.println("2. Computes the area & perimeter a circle");
			System.out.println("3. Computes the area & perimeter a right-angled triangle");
			System.out.println("4. Exit");
			System.out.print("Your choice: ");
			ch = keyBoard.nextInt();
			switch (ch) {
			case 1:
				System.out.print("Enter the width of a rectangle: ");
				double w = keyBoard.nextDouble();
				System.out.print("Enter the height of a rectangle: ");
				double h = keyBoard.nextDouble();
				IShape = new Rectangle(w, h);
				System.out.println("The area of a rectangle: " + IShape.computesArea());
				System.out.println("The perimeter of a rectangle: " + IShape.computesPerimeter());
				break;
			case 2:
				System.out.print("Enter the radius of a circle: ");
				double r = keyBoard.nextDouble();
				IShape = new Circle(r);
				System.out.println("The area of a circle: " + IShape.computesArea());
				System.out.println("The perimeter of a circle: " + IShape.computesPerimeter());
				break;
			case 3:
				System.out.print("Enter the width of a right-angled triangle: ");
				w = keyBoard.nextDouble();
				System.out.print("Enter the height of a right-angled triangle: ");
				h = keyBoard.nextDouble();
				IShape = new RightAngledTriangle(w, h);
				System.out.println("The area of a right-angled triangle: " + IShape.computesArea());
				System.out.println("The perimeter of a right-angled triangle: " + IShape.computesPerimeter());


				break;
			case 4:
				break;
			default:
				break;
			}
		}
		keyBoard.close();
	}


}

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