user2932587
user2932587

Reputation:

Exception Handling

I'm writing a program that is supposed to create a triangle and throw an exception if the triangle is not legal, meaning that the sum of any two sides must be greater than the length of the remaining side. The program consists of 4 classes (the super class GeometricObject, a Triangle class, a Program6 class, and an IllegalTriangleException class). The program is supposed to read in data from a file that consists of the lengths for each side, the color, and whether it's filled or not, perform calculations to determine the perimeter and area, and write the information to a file entitled prog6.out. I haven't implemented the writing to a file yet though since I'm having some problems getting the program to display the results correctly using a regular print statement since implementing the IllegalTriangleException class.

Here is what I have so far: The GeometricObject Class:

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

    public GeometricObject() {
        dateCreated = new java.util.Date();
    }

    public GeometricObject(String color, boolean filled) {
        dateCreated = new java.util.Date();
        this.color = color;
        this.filled = filled;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

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

    public String toString() {
        return "Created on: " + dateCreated + "\nColor: " + color + "\nFilled: " + filled;
    }
}

The Triangle class:

public class Triangle extends GeometricObject {
    private double side1;
    private double side2;
    private double side3;
    private double s = (side1 + side2 + side3)/2;
    private double area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
    private double perimeter;

    public Triangle() {
        side1 = 1.0;
        side2 = 1.0;
        side3 = 1.0;
    }

    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }

    public double getSide1() {
        return side1;
    }

    public double getSide2() {
        return side2;
    }

    public double getSide3() {
        return side3;
    }



    public double getPerimeter() {
        return (side1 + side2 + side3);
    }

    public double getArea() {
        this.s = (side1 + side2 + side3)/2;
        this.area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
        return area;
    }

    public String toString() {
        return "Triangle: " + "\nSide 1: " + side1 + "\nSide2: " + side2 + "\nSide3: " + side3 + "\nArea: " + getArea() + "\nPerimeter: " + getPerimeter() + "\nColor: " + getColor() + "\nFilled: " + isFilled();
    }

    public void setSides(double newSide1, double newSide2, double newSide3) throws IllegalTriangleException {
        if (side1 + side2 > side3){
            if (side1 + side3 > side2)
                if (side2 + side3 > side1)
        side1 = newSide1;
        side2 = newSide2;
        side3 = newSide3;
        }
        else
            throw new IllegalTriangleException(newSide1, newSide2, newSide3);
    }

    public Triangle(double side1, double side2, double side3, String color, boolean filled) throws IllegalTriangleException {
        try {
            setSides(getSide1(), getSide2(), getSide3());
        }
        catch(IllegalTriangleException ex) {
            System.out.println("Illegal Triangle: The summ of any two sides must be greater than the other side" + side1 + "," + side2 + "," + side3);
        }
    }
}

The Program6 class:

import java.util.Scanner; 

public class Program6 {
    public static void main(String[] args) throws Exception {
        java.io.File file = new java.io.File("prog6.dat");
        Scanner fin = new Scanner(file);

        while (fin.hasNext()) {
            double side1 = fin.nextDouble();
            double side2 = fin.nextDouble();
            double side3 = fin.nextDouble();
            String color = fin.next();
            String bool = fin.next();
            boolean filled;
             if(bool.equals("T"))
                  filled = true;
             else
                  filled = false;
            Triangle triangle = new Triangle(side1, side2, side3, color, filled);
            System.out.println(triangle);
        }
        fin.close();
    }
}

And the IllegalTriangleException class:

public class IllegalTriangleException extends Exception {

    private double side1;
    private double side2;
    private double side3;

    public IllegalTriangleException() {
    }

    public IllegalTriangleException(double side1, double side2, double side3) {
        super("Illegal Triangle: The summ of any two sides must be greater than the other side" + side1 + "," + side2 + "," + side3);
        //this.side1 = side1;
        //this.side2 = side2;
        //this.side3 = side3;
    }

    public double getSide1() {
        return side1;
    }

    public double getSide2() {
        return side2;
    }

    public double getSide3() {
        return side3;
    }
}

And in case it helps, here is the file that is being used for the input:

1.0 1.5 1.0 yellow  T
3.0 4.0 5.0 blue F
2.4 1.1 1.2 red T
3.1 6.2 8.5 green T
1.2 9.9 4.5 cyan F

Before adding in the Exception class, the program was working properly, the file was being read correctly and assigning the correct values to the variables, and the area and perimeters were being calculated correctly, however, now that I've added in the exception class, here is my output:

Illegal Triangle: The summ of any two sides must be greater than the other side1.0,1.5,1.0
Triangle: 
Side 1: 0.0
Side2: 0.0
Side3: 0.0
Area: 0.0
Perimeter: 0.0
Color: white
Filled: false
Illegal Triangle: The summ of any two sides must be greater than the other side3.0,4.0,5.0
Triangle: 
Side 1: 0.0
Side2: 0.0
Side3: 0.0
Area: 0.0
Perimeter: 0.0
Color: white
Filled: false
Illegal Triangle: The summ of any two sides must be greater than the other side2.4,1.1,1.2
Triangle: 
Side 1: 0.0
Side2: 0.0
Side3: 0.0
Area: 0.0
Perimeter: 0.0
Color: white
Filled: false
Illegal Triangle: The summ of any two sides must be greater than the other side3.1,6.2,8.5
Triangle: 
Side 1: 0.0
Side2: 0.0
Side3: 0.0
Area: 0.0
Perimeter: 0.0
Color: white
Filled: false
Illegal Triangle: The summ of any two sides must be greater than the other side1.2,9.9,4.5
Triangle: 
Side 1: 0.0
Side2: 0.0
Side3: 0.0
Area: 0.0
Perimeter: 0.0
Color: white
Filled: false

Not sure what I've done wrong, if anyone can help me out it'd be much appreciated. This is the first time I've worked with this many classes and also the first time I've worked with exception handling, so please be nice :-)

Upvotes: 1

Views: 1541

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

There are three basic problems...

First...

In your Triangle constructor, you are passing getSide1(), getSide2() and getSide3() to the setSides method...

public Triangle(double side1, double side2, double side3, String color, boolean filled) throws IllegalTriangleException {
    try {
        setSides(getSide1(), getSide2(), getSide3());

This means, you are simply passing the values of the fields side1, side2 and side3, which haven't been set yet are defaulted to 0, which means you are effectively saying...

setSides(0d, 0d, 0d);

This needs to be changed to pass the parameters of the constructor, for example...

public Triangle(double side1, double side2, double side3, String color, boolean filled) throws IllegalTriangleException {
    try {
        setSides(side1, side2, side3);

Second...

Your setSides method is testing the state of the instance fields state1, state2 and state3, which haven't been set yet (and even correcting for the previous mistake) are still defaulted to 0...so 0 + 0 > 0 is ... false

public void setSides(double newSide1, double newSide2, double newSide3) throws IllegalTriangleException {
    if (side1 + side2 > side3){
        if (side1 + side3 > side2)
            if (side2 + side3 > side1)

This should be modified to use the values passed to the method instead...

public void setSides(double newSide1, double newSide2, double newSide3) throws IllegalTriangleException {
    if (newSide1 + newSide2 > newSide3)
        if (newSide1 + newSide3 > newSide2)
            if (newSide2 + newSide3 > newSide1)

Third...

The previous if statement is wrong. What this basically equates to saying is, if everything is fine, set side1 = newSide1 and regardless of anything else, set side2 and side3 to newSide2 and newSide3 respectivly...

public void setSides(double newSide1, double newSide2, double newSide3) throws IllegalTriangleException {
    if (newSide1 + newSide2 > newSide3) 
        if (newSide1 + newSide3 > newSide2)
            if (newSide2 + newSide3 > newSide1)
                side1 = newSide1;

    side2 = newSide2;
    side3 = newSide3;

This is why it's VERY important to use {...}, for example...

public void setSides(double newSide1, double newSide2, double newSide3) throws IllegalTriangleException {
    if (newSide1 + newSide2 > newSide3) {
        if (newSide1 + newSide3 > newSide2) {
            if (newSide2 + newSide3 > newSide1) {
                side1 = newSide1;
                side2 = newSide2;
                side3 = newSide3;
            }
        }
    } else {
        throw new IllegalTriangleException(newSide1, newSide2, newSide3);
    }
}

Now, the problem is, the IllegalTriangleException will only be raised when newSide1 + newSide2 > newSide3, this can be rectified by testing each condition in a single if statement, for example...

public void setSides(double newSide1, double newSide2, double newSide3) throws IllegalTriangleException {
    if (newSide1 + newSide2 > newSide3 && 
            newSide1 + newSide3 > newSide2 &&
            newSide2 + newSide3 > newSide1) {
        side1 = newSide1;
        side2 = newSide2;
        side3 = newSide3;
    } else {
        throw new IllegalTriangleException(newSide1, newSide2, newSide3);
    }
}

No offense, but a good IDE should have picked up the issue with the if statements and a debugger would have helped find the other problems...

Additional...

The Triangle constructor is smothering the IllegalTriangleException, this means that it is never thrown to the caller...

public Triangle(double side1, double side2, double side3, String color, boolean filled) throws IllegalTriangleException {
    try {
        setSides(side1, side2, side3);(IllegalTriangleException ex) {
        System.out.println("Illegal Triangle: The summ of any two sides must be greater than the other side" + side1 + "," + side2 + "," + side3);
    }
}

Remove the try-catch and let the caller deal with it...

public Triangle(double side1, double side2, double side3, String color, boolean filled) throws IllegalTriangleException {
    setSides(side1, side2, side3);
}

Upvotes: 1

Related Questions