CS230 Lab: on Exceptions and IO: Shapes (continued)

Lab: on Exceptions and IO: Shapes (continued)

Download the folder download/lab5/ShapesProject_lab_starter from your cs account. Take a look at all the classes included. In this task, you will continue working on the ShapesDriver.java class you started working on in class.

Task 2: Reading Shape data from a file

Task 2A: Add a few constructors

Once we start working with files, many of our objects need certain methods that are particularly well-suited to this interaction between our objects and the files (for example, we saw in class the method serialize() which gave us a very concise representation of our Shape objects). In this task, you will work on adding constructors to our classes that take in as input String representations of objects.

First, take a look at the Circle class. Note the existence of a second constructor. Now add a second constructor that works in a similar fashion for the Rectangle class, and one for the Triangle class.

Task 1B: Helping us read from a file...

Many of our upcoming tasks will deal with reading from a File. It might be easier to deal with arrays of Strings instead of File objects, so, thinking ahead, we'll define a helper method which will read our Files into String[], where each element of the array is a line from the file.

Please catch the relevant FileNotFoundException right here, in this method. Remember that in general it is a good idea to deal with any exception your code may produce as close to its source as possible!

Don't forget to close the scanner when you are done with reading the text file.

Test this method so that you have some assurance that the file was read correctly into the array.

Task2: Collect triangles

Write a method called collectTrianglesFromFile() that reads in from a file (formatted like shapeData.txt) and returns all the triangles found in the file.

First, write down a detailed list of all the steps you'll have to tackle for this problem. Write this list as in-line comments for the method. This will be the starting point for your code.

Are there any methods already defined that can help you? What should be the return type for this method?

Make sure to test this method on the file provided.

Task 3: Collect all large shapes

Write a method called collectLargeShapes() that reads in from a file (formatted like shapeData.txt) and returns all the shapes whose perimeter is larger than some threshold value.

Once again, write down a detailed list of all the steps you'll have to tackle for this problem. Write this list as in-line comments for the method. This will be the starting point for your code.

Don't forget to test this method!

Task 4: Malformed input data: Exceptions and Recovery

Some times input data may not be formed as expected, and this may have consequences in the way the program runs. Consider your ShapeDriver program:

  • What may happen if the first word on a line in your file is the word "rectangle", but you're only given 1 number on that line (so you're basically missing the width)?
  • What if the number provided is not really a number (maybe you find the letter "A")?
  • How do you test your code against cases like the above? Feel free to temporarily edit the input data file so that it contains some malformed data to test your hypothesis.
  • How can you change your code so that when such a malformed piece of input is encountered the program behaves in a graceful way?