In: Computer Science
you are to write a program in Java, that reads in a set of descriptions of various geometric shapes, calculates the areas and circumferences of the shapes, and then prints out the list of shapes and their areas in sorted order from smallest to largest area. There are four possible shapes: Circle, Square, Rectangle, and Triangle. The last is always an equilateral triangle. The program should read from standard input and write to standard output. The program should read until the end of input is reached, i.e., there is no sentinel value to mark the end of input. There are at most 100 shapes in the input. Each line of the input contains a description of one shape and contains three or four fields separated by a single space. The first field is the name of the particular object (some String). The second field is the type of the shape (one of "Circle", "Square", "Rectangle", or "Triangle" - also a string.) The third field is the size: the radius of the circle, the size of a side of the square, the length of the rectangle, or the size of a side of the triangle. Only the rectangle has a fourth field - the height of the rectangle. The program should read in the input, compute both the area and circumference (or perimeter) of the shape, then sort the shapes by their areas, and print out the shapes in order from smallest to largest area.
Your program must: • Read from standard input and write to standard output. • Work on any size lists up to and including 100, not just the sizes in the example below. • Be efficient. • Have a base class, Shape, for a generic shape. This class must a method, getShape(String desc) which takes the description of a shape (including the name of the object) as described above and returns an appropriate Shape object, a getArea method, a getCircumference method, and a toString method. • Have four subclasses of Shape: Circle, Square, Rectangle, and Triangle.
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; abstract class Shape { public abstract double getArea(); public abstract double getCircumference(); } class Square extends Shape { private int side; public Square(int side) { this.side = side; } @Override public double getArea() { return side * side; } @Override public double getCircumference() { return 4 * side; } @Override public String toString() { return "Square (" + side + ")"; } } class Triangle extends Shape { private int side; public Triangle(int side) { this.side = side; } @Override public double getArea() { return Math.sqrt(3) * side * side / 4.0; } @Override public double getCircumference() { return 3 * side; } @Override public String toString() { return "Triangle (" + side + ")"; } } class Circle extends Shape { private int radius; public Circle(int radius) { this.radius = radius; } @Override public double getArea() { return Math.PI * radius * radius; } @Override public double getCircumference() { return 2 * Math.PI * radius; } @Override public String toString() { return "Circle (" + radius + ")"; } } class Rectangle extends Shape { private int a, b; public Rectangle(int a, int b) { this.a = a; this.b = b; } @Override public double getArea() { return a * b; } @Override public double getCircumference() { return 2 * (a + b); } @Override public String toString() { return "Rectangle (" + a + ", " + b + ")"; } } public class ShapeTester { public static void main(String[] args) { ArrayList<Shape> shapes = new ArrayList<>(); try { Scanner reader = new Scanner(new File("input.txt")); while(reader.hasNextLine()) { String line = reader.nextLine(); String tokens[] = line.split(" "); if(tokens[0].equalsIgnoreCase("circle")) { shapes.add(new Circle(Integer.parseInt(tokens[1]))); } if(tokens[0].equalsIgnoreCase("square")) { shapes.add(new Square(Integer.parseInt(tokens[1]))); } if(tokens[0].equalsIgnoreCase("triangle")) { shapes.add(new Triangle(Integer.parseInt(tokens[1]))); } if(tokens[0].equalsIgnoreCase("rectangle")) { shapes.add(new Rectangle(Integer.parseInt(tokens[1]), Integer.parseInt(tokens[2]))); } } Collections.sort(shapes, new Comparator<Shape>() { @Override public int compare(Shape o1, Shape o2) { return (int) (o1.getArea() * 100 - o2.getArea() * 100); } }); // print. for(Shape s: shapes) { System.out.println(s); } } catch (FileNotFoundException e) { System.out.println("Input file not found."); } } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.