Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Java application that will read in a text file containing shape dimensions and calculate the area for each shape. The shape name, dimensions,

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Write a Java application that will read in a text file containing shape dimensions and calculate the area for each shape. The shape name, dimensions, and the calculated area will be written to a text file. Each line of the input file will contain the shape name (a single String with no spaces) and the corresponding dimensions (all double) separated by space(s). A square will have a side length, circle will have a radius, a rectangle will have a length and a width, and a triangle will have a base and a height. The following shows part of a typical data file: Circle 2.5 Circle 12.7 Square 6.0 Triangle 12.5 6.2 Rectangle 12.1 5.6 Triangle 1.5 3.2 Square 7.3 The program should compute the area for each shape instance then write the area and dimension(s) to the corresponding output file. You will create an output file for each shape type. All circle data should be written to a file called "circles.txt", square data to "squares.txt, rectangle data to "rectangles.txt", and triangle data to "triangles.txt". Use the given Shape, Square, Circle, Rectangle, and Triangle classes. Areas.java contains a skeleton of the program. Do the following: 1. Set up a Scanner object scan from the input file and PrintWriter objects circles, squares, rectangles, and triangles to the corresponding output files inside the try clause (see the comments in the program. 2. Write the name of the shape as the first line in each of the corresponding shape file. The second line should be a header that labels the corresponding values written in the file: circles.txt: Circle Area Radius 19.63. 2.5 506.69 12.7 squares.txt: Square Area. Side Length 36.0. 6.0 53.29. 7.3 rectangles.txt: Rectangle Area. Length Width triangles.txt: Triangle Area Base Height 2. Inside the while loop add code to read the input file-get the shape and based on the name, get the corresponding number of dimension values. Instantiate the corresponding shapeObj then compute the area. Write the dimension(s) and the area to the correct output file. Make sure the output files contents are properly formatted and lined-up. 3. After the loop close the Scanner and PrintWriter objects in a finally block. 4. Think about the exceptions that could be thrown by this program: A FileNotFoundException if the input file does not exist . An InputMismatchException if it can't read an int or double when it tries to - this indicates an error in the input file format - A NegativeNumberException if a negative dimension value is read. Add a catch for each of these situations, and in each case give as specific a message as you can. The program will terminate if any of these exceptions is thrown, but at least you can supply the user with useful information. 5. Test the program. Test data is in the file shapes.txt. Be sure to test each of the exceptions as well. 6. Modify your program so that it prompts the user for the input filename. Test your program again. // ******* Areas.java // Reads shape data from a text file and writes data to corresponding text files. import java.util.*; import java.io.*; public class Areas { // Reads shape data from text file, computes the area, then writes data to another file based on the shape type // public static void main (String[] args) { String shape Type; double area; double radius; double side; double length, width; double base, height; // type of shape // area of the shape Il circle dimension // square dimension // rectangle dimensions // triangle dimensions String name, inputName = "shapes.txt"; String circleName = "circles.txt"; String squareName = "squares.txt"; String rectangleName = "rectangles.txt"; String triangleName = "triangles.txt"; Shape shapeObj = null; try ( // Set up Scanner to input file // Set up the output file streams ) { // Print a header to each of the output files squares.println(); squares.println ("Squares"); squares.println (; circles.println (); circles.println ("Circles"); circles.println (); rectangles.println(); rectangles.println ("Rectangles"); rectangles.println (); triangles.println (; triangles.println ("Triangles"); triangles.println(); // Process the input file, one token at a time while (scan.hasNext() { II Get the shape name and dimension(s) // Instantiate the correct object for shapeObj // Calculate the corresponding area using the correct area() method // Write the shape dimension(s) and area in the correct file } } //Add a catch for each of the specified exceptions, and in each case //give as specific a message as you can public class Circle extends Shape private double radius; public Circle (String name, double r) { super (name); radius = r; } public double area() { return Math.PI * radius } radius; public String toString() { return super.toString() + "Radius = " } + radius; public boolean equals(Object otherObj) { if (otherObj == null) { return false; } else if (otherObj.getClass() != this.getClass()) return false; else Circle otherCirc = (Circle) otherObj; if (otherCirc.getName().equals(this.getName() && Math.abs(otherCirc.radius - this.radius)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago