Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the class ReadShapeFile, there is a file called shapes.txt. Ive opened it on line 2 This has a list of shapes. Youre going to

  1. In the class ReadShapeFile, there is a file called shapes.txt. Ive opened it on line 2 This has a list of shapes. Youre going to read each line for each shape and call createShape, which will create one of the available shapes, Circle, Rectangle, or Square and returns a GeometricObject, which you will add to the ArrayList called shapeList.

The file has some unavailable shapes. If the shape is unavailable, createShape should throw a ShapeException. You should catch a ShapeException and continue reading the file.

By the end, the side of shapeList should be 20.

Summary:

  • Create a ShapeException class
  • Implement createShape to return the appropriate shape depending on the string shapeName
  • createShape should throw a ShapeException if it is not a Circle, Square, or Rectangle
  • A loop should read in the shapes.txt file lineby-line
  • If the file cannot be read, you should break out of the loop
  • If you get a ShapeException, you should continue reading the file

import java.io.*; import java.util.ArrayList;

import shapes.*;

/* your tasks: * create a class called ShapeException * createShape should throw a ShapeException * in main(), you should catch the ShapeException * */ public class ReadShapeFile {

public static GeometricObject createShape(String shapeName) { /* if shapeName is "Circle" return Circle(); * if shapeName is "Square" return Square(); * if shapeName is "Rectangle" return Rectangle(); * if it is not any one of these, it should throw * a ShapeException */ return null; } public static void main(String[] args) { ArrayList shapeList = new ArrayList(); File f = new File("shapes.txt"); String inString = null; /* create a loop to read the file line-by-line */ try { GeometricObject shape = createShape(inString); } catch (/* your exception */ ) { System.err.println("Cannot create shape: " + inString); }

System.out.println(shapeList); } }

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions