Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this problem you modify the Rectangle class to throw a custom exception if a

ID: 3767565 • Letter: F

Question

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.

Rectangle and GeometricObject classes are at the bottom.

Create a package named prob2

Copy GeometricObject and Rectangle from the prob1 package to the prob2package.

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

Create a class named IllegalRectangleException – This class should be aRuntimeException. It’s constructor should accept a message, the width, height, and area of an illegal rectangle. It should also make available these values.

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

Create a text file named rectangles.txt in your prob2 folder with the sample values show 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

5 8

4 7

0.1 0.2

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 triangles 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

this is the GeometricObject class

package prob2;
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;

/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}

/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}

/** Return color */
public String getColor() {
return color;
}

/** Set a new color */
public void setColor(String color) {
this.color = color;
}

/** Return filled. Since filled is boolean,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}

/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}

/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}

@Override
public String toString() {
return "created on " + dateCreated + " color: " + color +
" and filled: " + filled;
}

/** Abstract method getArea */
public abstract double getArea();

/** Abstract method getPerimeter */
public abstract double getPerimeter();
}

this is the Rectangle class

ublic class Rectangle extends GeometricObject {
private double width;
private double height;

public Rectangle() {
}

public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}

/** Return width */
public double getWidth() {
return width;
}

/** Set a new width */
public void setWidth(double width) {
this.width = width;
}

/** Return height */
public double getHeight() {
return height;
}

/** Set a new height */
public void setHeight(double height) {
this.height = height;
}

@Override /** Return area */
public double getArea() {
return width * height;
}

@Override /** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}

@Override
public String toString() {
// Implement it to return the three sides
return "Rectangle: width = " + width + ", height = " + height;
}
}

Explanation / Answer

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 triangles 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:
class RectangleTester
{
// Reading the Rectangle.txt file

public void ReadFile()
{
FileWriter fileWriter =
                new FileWriter("illegalRectangles.txt");
BufferedWriter bufferedWriter =
                new BufferedWriter(fileWriter);

try (BufferedReader br = new BufferedReader(new FileReader("Rectangle.txt")))
{
    String line;
    while ((line = br.readLine()) != null) {
String tes[]t=line.split(" ");
if(test.length>0)
{
  double width=Double.parseDouble(test[0]);
  double height=Double.parseDouble(test[1]);
  double area=width*height
  if(area>1.0)
  {
  String text="IllegalRectangle: "+"Width:"+width +"Height: "+height+ "Area: "+area;
   System.out.println("It is a valid Rectangle");
   bufferedWriter.write(text);  //write it into another file it the rectangle is valid
  }
}
    }
}
catch(Exception e){}
}
}

Create a class named IllegalRectangleException –
This class should be aRuntimeException.
It’s constructor should accept a message, the width, height, and area of an illegal rectangle. It should also make available these values.

class IllegalRectangleException extends Exception
{
public IllegalRectangleException(){super();}
double width;
double height;
double area;
String message;
public IllegalRectangleException(String message, double width, double height, double area)
{
System.out.println(width);
System.out.println(height);
System.out.println(area);
System.out.println(message);
}

}

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


public class Rectangle extends GeometricObject throws IllegalRectangleException {
   private double width;
   private double height;

   try{
   // try to create an IllegalRectangle here
   }
catch(IllegalRectangleException e)
{
throw new IllegalRectangleException(width,height,area,"Illegal Rectangle created");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote