Create a program that will String input. Based on the number of inputs, the program will decide what shape it is. (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:Rectagle
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
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 Square implements IShape {
	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 implements IShape {
	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 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;
	}
}
public class App {
	/** Main Method */
	public static void main(String[] args) {
		int ch = args.length;
		IShape IShape;
		switch (ch) {
		case 1:
			double radius = Double.parseDouble(args[0]);
			IShape = new Circle(radius);
			System.out.println("Type: Circle");
			System.out.println("Radius: " + radius);
			System.out.println("Perimeter: " + IShape.computesPerimeter());
			System.out.println("Area: " + IShape.computesArea());
			break;
		case 2:
			double length = Double.parseDouble(args[0]);
			double width = Double.parseDouble(args[1]);
			if (length == width) {
				IShape = new Square(length);
				System.out.println("Type: Square");
				System.out.println("Side: " + length);
				System.out.println("Perimeter: " + IShape.computesPerimeter());
				System.out.println("Area: " + IShape.computesArea());
			} else {
				IShape = new Rectangle(length, width);
				System.out.println("Type: Rectagle");
				System.out.println("Length: " + length);
				System.out.println("Width: " + width);
				System.out.println("Perimeter: " + IShape.computesPerimeter());
				System.out.println("Area: " + IShape.computesArea());
			}
			break;
		case 3:
			double a = Double.parseDouble(args[0]);
			double b = Double.parseDouble(args[1]);
			double c = Double.parseDouble(args[2]);
			IShape = new Triangle(a, b, c);
			System.out.println("Type: Triangle");
			System.out.println("Sides: " + a + ", " + b + ", " + c);
			System.out.println("Perimeter: " + IShape.computesPerimeter());
			System.out.println("Area: " + IShape.computesArea());
			break;
		default:
			System.out.println("Invalid input");
			break;
		}
	}
}
Comments