Question: 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,

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 fileget 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 cant 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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!