Solved–HW 5 –Solution

$30.00 $19.00

This homework has 2 problems which deal with text files and exceptions (Ch 12). Eclipse Reminder You will create a Java Project in Eclipse with the name: hw5_FLastname You will have two packages: prob1, prob2 Problem 1 For this problem you modify the Rectangle class to throw a custom exception if an attempt is made…

You’ll get a: . zip file solution

 

 
Categorys:
Tags:

Description

5/5 – (2 votes)

This homework has 2 problems which deal with text files and exceptions (Ch 12).

Eclipse Reminder

  • You will create a Java Project in Eclipse with the name: hw5_FLastname

  • You will have two packages: prob1, prob2

Problem 1

For this problem you modify the Rectangle class to throw a custom exception if an attempt is made to create an illegal rectangle. The program will read a file of potential rectangles and write the illegal ones to a file.

  1. Create a package named prob1

  2. Download the classes GeometricObject and Rectangle from BlazeView and copy them to the prob1 package.

  3. Read (no action required) – An illegal rectangle is one whose area is less than 1.0.

  1. Create a class named IllegalRectangleException This class should be a subtype (subclass) of RuntimeException. Its constructor should accept a message, the width, height, and area of an illegal rectangle. It should also make available these values.

  1. Modify the Rectangle class to throw an IllegalRectangleException when an attempt is made to create an illegal rectangle.

  1. Create a text file named rectangles.txt in your prob1 folder with the sample values shown below. Each line represents a rectangle: width and height. Note: there are 6 possible rectangles in this sample file, but in general there could be any number.

3.5 4.5

0.5 0.25

0.4 0.9

    1. 8

    1. 7

    1. 1 0.2

  1. Create a class named RectangleTester which should read from rectangles.txt. It should read all the rectangles in the file, one at a time. Any rectangles that are illegal should be written to an output file named illegalRectangles.txt. If the input is as above, then illegalRectangles.txt should produce this output:

Illegal Rectangle: Width=0.50, Height=0.25, Area=0.13

Illegal Rectangle: Width=0.40, Height=0.90, Area=0.36

Illegal Rectangle: Width=0.10, Height=0.20, Area=0.02

Problem 2

For this problem you will read a file of numbers adding them as you go, but skipping some of the number as indicated by a code in the file.

  1. Create a package named prob2

  1. Create a text file named numbers.txt in your prob2 folder with the sample values shown below. Note: the third line is the letter “s” followed by the number “2”. Similarly, the ninth line is the letter “s” followed by the number “1”. This will be explained next.

4

5

s2

99

2

17

1

8

s1

7

10

10

5

  1. Create a class named NumberAdder that has a main that reads numbers.txt and adds all numbers except the ones skipped as indicated by a line in the file that starts with the letter “s” and is followed by the number of lines to skip. Finally, the sum should be printed to the console. For example, the input below:

would produce the output: Sum=60

Here is another test case which should produce the output: Sum=90

s5

2

43

3

6

2

9

12

4

56

s2

10

4

8

s2

1

5

s2

4

2

10

Submission

Make sure you zip the ENTIRE folder hw5_FLastname!! Submit your zip file to Blazeview by the due date. The name of your file should be: hw5_FLastname.zip.

Grading

I will RANDOMLY select one problem for grading. For example, if problem 1 is selected, I will only go to the package/folder prob1 for grading. So make sure all your Java codes are in the correct packages/folders.

Additional Requirements:

  1. No late submission will be accepted.

  2. Please exactly follow the naming rules described above. You will be deducted 10 points for incorrect naming.

  1. Write comments at the beginning of your Java source file(s) to indicate your name, student ID, “CS 1302-A Homework 5”, and the due date.

  1. Make sure that your programs are well-documented, readable, and user friendly. Make sure you document your programs by adding comments to your statements/blocks so that I can understand your code. You will get 0 to 10 points based on the helpfulness of your comments. You will be deducted 10 points for no comments.

  1. It is your responsibility to make sure your programs can compile and run correctly. Please be aware that programs that do not compile may receive ZERO credit.

  1. When grades are returned to you via BlazeView, you have 7 days to meet with the instructor to discuss on grade changes. After 7 days, the grades are written in stone and can’t be changed after that point.

3