Answer to Question #314410 in Java | JSP | JSF for dick

Question #314410

Create a program that will String input. Based on the number of inputs,

the USER will decide what shape it is circle,rectangle,square and triangle.

(1 input – circle, 2 inputs – square or rectangle, 3 inputs - triangle).

The program will then display the shape type, details, perimeter and area.


Justines-MacBook-Pro:classes Justine$ java-jar Shape.jar15

Type:Circle

Radius:15

Perimeter:94.2

Area:706.5


Justines-MacBook-Pro:classes Justine$ java-jar Shape.jar15

Type:Rectangle

Length:24

Width:12

Perimeter:72.0

Area:288.0


Justines-MacBook-Pro:classes Justine$ java-jar Shape.jar15

Type:Square

Side:12

Perimeter:48.0

Area:144.0


Justines-MacBook-Pro:classes Justine$ java-jar Shape.jar 9 15 12

Type:Triangle

Sides:9, 15, 12

Perimeter:36.0

Area:54.0


Justines-MacBook-Pro:classes Justine$ java-jar Shape.jar 9 15 12 8

Invalid input


class:

-TestShape

-Shape

-Circle

-Rectangle

-Square

T-riangle


1
Expert's answer
2022-03-19T12:25:07-0400




import java.util.Scanner;


abstract class Shape {
	abstract double computesArea();


	abstract double computesPerimeter();
}


class Rectangle extends Shape {
	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 Square extends Shape {
	private double side;


	public Square() {
	}


	public Square(double side) {
		this.side = side;
	}


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


	@Override
	public double computesPerimeter() {
		return 4 * side;
	}
}


class Triangle extends Shape {
	private double a;
	private double b;
	private double c;


	public Triangle() {
	}


	public Triangle(double a, double b, double c) {
		this.a = a;
		this.b = b;
		this.c = c;
	}


	@Override
	public double computesArea() {
		double s = (a + b + c) / 2.0;
		return Math.sqrt(s * (s - a) * (s - b) * (s - c));
	}


	@Override
	public double computesPerimeter() {
		return a + b + c;
	}


}


class Circle extends Shape {
	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;
	}
}


public class TestShape{


	/** Main Method */
	public static void main(String[] args) {


		Shape newShape;
		switch (args.length) {
		case 1:
			double radius = Double.parseDouble(args[0]);
			newShape = new Circle(radius);
			System.out.println("Type: Circle");
			System.out.println("Radius: " + radius);
			System.out.println("Perimeter: " + newShape.computesPerimeter());
			System.out.println("Area: " + newShape.computesArea());
			break;
		case 2:
			double length = Double.parseDouble(args[0]);
			double width = Double.parseDouble(args[1]);
			if (length == width) {
				newShape = new Square(length);
				System.out.println("Type: Square");
				System.out.println("Side: " + length);
				System.out.println("Perimeter: " + newShape.computesPerimeter());
				System.out.println("Area: " + newShape.computesArea());
			} else {
				newShape = new Rectangle(length, width);
				System.out.println("Type: Rectagle");
				System.out.println("Length: " + length);
				System.out.println("Width: " + width);
				System.out.println("Perimeter: " + newShape.computesPerimeter());
				System.out.println("Area: " + newShape.computesArea());
			}
			break;
		case 3:
			double a = Double.parseDouble(args[0]);
			double b = Double.parseDouble(args[1]);
			double c = Double.parseDouble(args[2]);
			newShape = new Triangle(a, b, c);
			System.out.println("Type: Triangle");
			System.out.println("Sides: " + a + ", " + b + ", " + c);
			System.out.println("Perimeter: " + newShape.computesPerimeter());
			System.out.println("Area: " + newShape.computesArea());
			break;
		default:
			System.out.println("Invalid input");
			break;
		}
	}
}

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