Question
Write a program that will read in a file containing shape data and calculate the area for each shape. The shape data and the calculated
Write a program that will read in a file containing shape data and calculate the area for each shape. The shape data and the calculated area will be written to a file. Each line of the input file will contain the shape name (a single String with no spaces) and the corresponding dimensions (all double). 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 dimension(s) and the area 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. 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.
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 input value is entered.
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started