非法三角异常

时间:2013-11-15 23:49:03

标签: java exception io

我的作业有些问题,并希望你们可以帮助我。作业是这样的:

  

IllegalTriangleException )编程练习11.1定义了具有三个边的三角形类。在三角形中,任何两个边的总和都大于另一边。 三角类必须遵守此规则。创建 IllegalTriangleException 类,并修改 Triangle 类的构造函数,以便在创建具有违反规则的边的三角形时抛出 IllegalTriangleException 对象,如下:

   /** Construct a triangle with the specified sides */
    public Triangle(double side1, double side2, double side3)
       throws IllegalTriangleException {
        //Implement it
    }

好。这是三角类。

import java.lang.Math;

public class Triangle extends GeometricObject {
 private double side1 = 1.0;
 private double side2 = 1.0;
 private double side3 = 1.0;

 public Triangle() {

}

 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 void setSide1(double side1) {
    this.side1 = side1;
}

 public void setSide2(double side2) {
    this.side2 = side2;
}

 public void setSide3(double side3) {
    this.side3 = side3;
}

 public double getArea() {
     double p = getPerimeter() / 2;
     return Math.sqrt(p * ((p - side1) * (p - side2) * (p - side3)));
}

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

 public String toString() {
     return " Triangle: Side 1 = " + side1 + " Side 2 = " + side2
             + " Side 3 = " + side3;
}

}

IllegalTriangleException 类:

 public class IllegalTriangleException extends Exception {
private double perimeter;

 /** Construct an exception*/
  public IllegalTriangleException(double perimeter) {
    super( "The sum of any two sides is greater than the other side" +  perimeter);
    this.perimeter = perimeter;
}
 /**return the side*/
  public double getPerimeter() {
    return perimeter;
}
}

这是 TriangleWithException 类:

  public class TriangleWithException {
    //The perimeter of the triangle
    private double perimeter;
    private double side1 = 1.0;
    private double side2 = 1.0;
    private double side3 = -1.0;

    //Number of objects created
    private static int numberOfObjects = 0;

        //construct a triangle with perimeter 
    public TriangleWithException(double side1, double side2, double side3)  {

    }
    //Construct a triangle with a specified perimeter.
    public TriangleWithException(double newPerimeter) {
        setPerimeter(newPerimeter);
        numberOfObjects++;

    }
    //Return perimeter
    public double getPerimeter() {
        return perimeter;
    }

    //set new perimeter
    public void setPerimeter(double newPerimeter) throws IllegalArgumentException {
        if (newPerimeter >= 0)
            perimeter = newPerimeter;
        else
            throw new IllegalArgumentException("Any two sides needs to be grater than the other side");

    }
    // Return numberOFObjects
    public static int getNumberOfObjects() {
        return numberOfObjects;
    }

    //return area of triangle
    public double findArea() {
        double p = getPerimeter() / 2;
        return Math.sqrt(p * ((p - side1) * (p - side2) * (p - side3)));
    }
    }

最后,测试程序

public class TestTriangleWithException {
public static void main(String[] args) {
    try {
      new TestTriangleWithException(/** What goes here?*/);
      new TestTriangleWithException(/** What goes here?*/);
      new TestTriangleWithException(/** What goes here?*/);
}
    catch (IllegalTriangleException ex) {
        System.out.println(ex);
}
    System.out.println("Number of objects created: " + 
            TriangleWithException.getNumberOfObjects());
}
}
  class TriangleWithException {
//Perimeter
private double perimeter;

//number of objects created
private static int numberOfObjects = 0;

//Construct a triangle with a specified perimeter
public TriangleWithException() throws IllegalTriangleException {
    double side1; 
    double side2; 
    double side3;
}

//construcj a triangle 
public TriangleWithException(double newPerimeter)
throws IllegalTriangleException {
    if (newPerimeter >= 0)
        perimeter = newPerimeter;
        else
            throw new IllegalTriangleException(newPerimeter);

}

//Return numberofobjects
public static int getNumberOfObjects() {
    return numberOfObjects;

}

//Return the area of the triangle 
/** How do define getPerimeter for the type IllegalTriangleException?
 * How do i resolve side1, side2 and side3 as variables?*/
public double findArea() {
     double p = getPerimeter() / 2;
    return Math.sqrt(p * ((p - side1) * (p - side2) * (p - side3)));
}
}

所以,我现在面临的问题是这些

如果违反了规则,我需要修改 Triangle 类的构造函数以抛出 IllegalTriangleException

我需要弄清楚Perimeter是否是我需要的变量,而不是异常类中的边。

我是初学者,所以虽然解决方案看起来很明显,但它们并不适合我。

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:2)

我认为应该帮助你。我将选择语句添加到构造函数中,并帮助您使用Exception类。如果您的GeometricObject课程很好,则此代码应该适合您。

import java.util.Scanner;

public class TriangleWithException {

public static void main(String[] args) throws IllegalTriangleException {

    Scanner input = new Scanner(System.in);

    // create triangle
    Triangle triangle = new Triangle();
    boolean done = false;
    double side1 = 1.0;
    double side2 = 1.0;
    double side3 = 1.0;

    System.out.println("Enter three sides: ");

    while (!done) {
        try {
            side1 = input.nextDouble();
            side2 = input.nextDouble();
            side3 = input.nextDouble();
            triangle = new Triangle(side1, side2, side3);
            done = true;
        }
        catch (IllegalTriangleException ex) {
            System.out.println(ex.getMessage());
            System.out.print("Enter three sides:  ");
            input.nextLine();
        }
    }




    System.out.println("Enter a color:  ");
    String color = input.next();
    triangle.setColor(color);

    System.out.println("Enter true or false for triangle filled: ");
    boolean filled = input.nextBoolean();
    triangle.setFilled(filled);

    System.out.println();

    System.out.println(triangle.toString() + 
            "\narea: " + triangle.getArea() + 
            "\nperimeter " + triangle.getPerimeter() +
            "\ncolor: " + triangle.getColor() + 
            "\nfilled: " + triangle.isFilled());
}

}

原创三角课

class Triangle extends GeometricObject {

protected double side1 = 1.0;
protected double side2 = 1.0;
protected double side3 = 1.0;

public Triangle() {
}

public Triangle(double side1, double side2, double side3) throws   IllegalTriangleException {
    if (side1 >= side2 + side3)
        throw new IllegalTriangleException(side1);
    else if (side2 >= side1 + side3)
        throw new IllegalTriangleException(side2);
    else if (side3 >= side2 + side1)
        throw new IllegalTriangleException(side3);
    else {
        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() {
    double p = (side1 + side2 + side3) / 2;
    double area = Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
    return area;
}

@Override
public String toString() {
    return "Triangle side 1 = " + side1 + ", side 2 = " + side2 + ", side 3 = " + side3;
}
}

IllegaTriangleException类

class IllegalTriangleException extends Exception {

private double side = 1.0;

public IllegalTriangleException(double side) {
    super(side + " is larger than other two combined");

} 

public double getSide1() {
    return side;
}

}

答案 1 :(得分:0)

从Triangle类开始:

public class Triangle extends GeometricObject {
    private double side1;
    private double side2;
    private double side3;

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

    ...
}

和例外:

public class IllegalTriangleException extends Exception {

    public IllegalTriangleException() {
    }

    public IllegalTriangleException(String message) {
        super(message);
    }

    ...

}

现在你必须添加用于检查三角形的代码并最终抛出异常(只有结构,我不会完成整个工作):

public class Triangle extends GeometricObject {
    private double side1;
    private double side2;
    private double side3;

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

        if (E*R*R*O*R)
            thrown new IllegalTriangleException("Any two sides needs to be grater than the other side");
    }

    ...
}

答案 2 :(得分:0)

在你的构造函数中,你只是在侧面,这是你应该检查Exception的时候。

所以,我们有:

  

side_1< side_2 + side_3
side_2< side_1 + side_3
side_3<   side_1 + side_2

这可以重新安排为:

  

side_1< side_2 + side_3
side_1> side_2 - side_3
side_1>   side_3 - side_2

这减少到:

  

side_1< side_2 + side_3
side_1> absolute_value(side_3 -   side_2)

所以,我们有:

public TriangleWithException(double side1, double side2, double side3) 
                            throws IllegalTriangleException {
    if (side1 >= side2 + side3 || side1 <= Math.abs(side3-side2))
        throw new IllegalTriangleException(yourMessage);

    //otherStuffInConstructor
}