Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help to fix the Java code below STEP 10: Our next goal is to be able to read descriptions of dots from a

I need help to fix the Java code below

STEP 10: Our next goal is to be able to read descriptions of dots from a file, and convert the descriptions to instances. Each line of our file will have 4 properties separated by commas: the color name, an X position, a Y position, and a radius. In DotReader.java, write the method readDot() that returns type Dot, takes no arguments, and throws IOException. Within the readDot() method, use the BufferedReader instance variable provided to read a single line and break it at the commas into an array of Strings. For example, the input line BLUE,100,200,300 should result in the array { BLUE, 100, 200, 300 }. Note there are no blank spaces in the input. If you have trouble with parsing the line, try looking up split() in the String API page; use , as the regex arg. If the line read from the BufferedReader is null, indicating end-of-file, your readDot() method should also return null to indicate end-of-file. Remember that the Dot ctor needs 3 int arguments, but if you use Strings split() method, you get an array of Strings. Youll have to convert (for example) the String 100 to the int 100. Look up the parseInt() method on the API page for Integer.

STEP 11: Using the values in the String array, readDot() should return a new Dot using the four values.

STEP 12: But wait, what if we have an irresponsible user that tries to add more properties, like flavor, onto one line? (Maybe that user likes Dots candies.) We have to check that our array has only 4 properties in it, no more, no less. If the array is a different size than what we expected, we'll throw our own exception: a DotException. Create a new class called DotException that's a subclass of Exception, has a constructor that accepts a single String argument, and calls the superclass constructor using that String argument.

STEP 13: Now that we have our own specific Exception we can use when making Dots, let's use it in our DotReader class. In DotReader.java, make sure that the array from the parsed line is of exactly size 4. If it's not, throw a new DotException that gives a brief description and the line that's causing a problem. Youll have to change the readDot() declaration to declare that the method throws DotException. Its ok to have multiple exception types after throws just separate the types with commas. To throw the exception, do something like this:

DotException de = new DotException(a good message); throw de;

package dotlab;

import java.io.*;

public class DotReader {

private BufferedReader br;

public DotReader(BufferedReader br) {

this.br = br;

}

//returns type Dot, takes no arguments, and throws IOException

public Dot readDot() throws IOException, DotException {

String dm = br.readLine();

if (dm == null) {

return null;

}

String[] words = dm.split(",");

String name = words[0];

int x = Integer.parseInt(words[1]);

int y = Integer.parseInt(words[2]);

int radius = Integer.parseInt(words[3]);

Dot d1 = new Dot(name, x, y, radius);

if (words.length > 4 || words.length < 4) {

DotException err = new DotException("Dot size is not what is expected");

throw err;

}

return d1;

}

}

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

Handbook Of Database Security Applications And Trends

Authors: Michael Gertz, Sushil Jajodia

1st Edition

1441943056, 978-1441943057

More Books

Students also viewed these Databases questions

Question

how would you have done things differently?

Answered: 1 week ago