gpt4 book ai didi

java - 异常处理

转载 作者:行者123 更新时间:2023-11-30 03:48:20 25 4
gpt4 key购买 nike

我正在编写一个程序,该程序应该创建一个三角形,如果三角形不合法,则抛出异常,这意味着任何两条边的总和必须大于剩余边的长度。该程序由 4 个类组成(父类(super class) 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 类之前,程序运行正常,文件被正确读取并为变量分配正确的值,并且面积和周长被正确计算,但是,现在我已经添加了异常类,这是我的输出:

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...所以 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);
}
}

现在的问题是,只有当 newSide1 + newSide2 > newSide3 时才会引发 IllegalTriangleException,这可以通过在单个 中测试每个条件来纠正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);
}

关于java - 异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25027249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com