IllegalTriangleException

IllegalTriangleException

我正在编写一个程序,该程序应该创建一个三角形并在三角形不合法的情况下引发异常,这意味着任何两个边的总和必须大于剩余边的长度。该程序由4个类组成(超类GeometricObject,Triangle类,Program6类和IllegalTriangleException类)。该程序应该从文件中读取数据,该文件包括各边的长度,颜色以及是否填充,执行计算以确定周长和面积,并将信息写入名为prog6.out的文件中。 。自从实现IllegalTriangleException类以来,由于在使程序使用常规打印语句正确显示结果时遇到一些问题,因此我尚未实现对文件的写入。

这是我到目前为止所拥有的:GeometricObject类:

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;
    }
}


三角类:

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);
        }
    }
}


Program6类:

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();
    }
}


和IllegalTriangleException类:

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;
    }
}


如果有帮助,这里是用于输入的文件:

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


在添加Exception类之前,程序运行正常,正在正确读取文件并将正确的值分配给变量,并且正确地计算了面积和周长,但是,现在,我已经在Exception类中添加了,这是我的输出:

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


不知道我做错了什么,如果有人可以帮助我,将不胜感激。这是我第一次使用这么多的类,也是我第一次使用异常处理,所以请加心:-)

最佳答案

有三个基本问题...

第一...

Triangle构造函数中,您要将getSide1()getSide2()getSide3()传递给setSides方法...

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


这意味着,您只需传递side1side2side3字段的值(尚未设置为默认值0),这意味着您实际上是在说...

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


这需要更改以传递构造函数的参数,例如...

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


第二...

您的setSides方法正在测试实例字段state1state2state3的状态,这些实例字段尚未设置(甚至更正了先前的错误)仍默认为0。 .so 0 + 0 > 0是... false

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


应该修改它以使用传递给方法的值代替...

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


第三...

前面的if语句是错误的。这基本上等于说,如果一切都很好,请设置side1 = newSide1,并且不管其他任何情况,分别将side2side3设置为newSide2newSide3 ...

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;


这就是为什么使用{...}非常重要的原因,例如...

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);
    }
}


现在的问题是,只有在IllegalTriangleException时才会引发newSide1 + newSide2 > newSide3,这可以通过在单个if语句中测试每个条件来纠正,例如...

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);
    }
}


没有冒犯,但一个好的IDE应该使用if语句来解决问题,而调试器将有助于发现其他问题...

额外...

Triangle构造函数令人窒息IllegalTriangleException,这意味着它永远不会抛出给调用者...

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);
    }
}


删除try-catch并让呼叫者对其进行处理...

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

09-30 23:40