Question

In: Computer Science

you are to write a program in Java, that reads in a set of descriptions of...

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.

Solutions

Expert Solution

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.


Related Solutions

Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write a Java program that prompts for and reads the number N of cities or locations...
Write a Java program that prompts for and reads the number N of cities or locations to be processed. It then loops N times to prompt for and read, for each location, the decimal latitude, decimal longitude, and decimal magnetic declination. It then computes and displays, for each location, the Qibla direction (or bearing) from Magnetic North. The true bearing from a point A to a point B is the angle measured in degrees, in a clockwise direction, from the...
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Write a program in python that reads the elements of a set from the keyboard, stores...
Write a program in python that reads the elements of a set from the keyboard, stores them in a set, and then determines its powerset. Specifically, the program should repeatedly ask the user: Enter one more element ? [Y/N] If the user answers Y then an new element is read from the keyboard: Enter the new element in the set: This cycle continues until the user answers N to the first question. At that point the program shall compute the...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for...
Write a program that performs the following two tasks in java Reads an arithmetic expression in...
Write a program that performs the following two tasks in java Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). Evaluates the postfix expression. Use linked lists to implement the Queue and Stack ADTs. DO NOT USE BUILT IN JAVA CLASSES
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
JAVA Write a program that reads the integers between -100 and 100 and counts the occurrences...
JAVA Write a program that reads the integers between -100 and 100 and counts the occurrences of each with ascending order. input: line1:number of figures line2:number Sample Input 5 -3 100 -1 -2 -1 Sample Output -3 1 -2 1 -1 2 100 1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT