对于我的编码类,我们正在创建一个类来计算我们 window 公司的 window 玻璃的成本。当使用测试器对我的代码进行评分时,我更改窗口尺寸的方法似乎不起作用。这是窗口的构造函数:
public Window(double width, double height) {
double rectangleArea = width*height;
double radius = width/2;
double circleArea= Math.PI*Math.pow(radius, 2);
double semiCircleArea= circleArea/2;
double areaFeetSquared= rectangleArea + semiCircleArea;
area= areaFeetSquared*SQUARE_INCHES_PER_SQUARE_FOOT;
double amountOfGlaze= area/SQUARE_INCHES_PER_OUNCE_OF_GLAZE;
costOfGlaze= amountOfGlaze*COST_PER_OUNCE;
}
这是我的 setDimensions 方法不起作用
public void setDimensions(double theWidth, double theHeight) {
width= theWidth;
height= theHeight;
}
如果您需要更多背景信息,我很乐意在询问时提供更多信息。感谢您的阅读,希望您能帮助我。
最佳答案
除了更新宽度和高度之外,您还需要像在构造函数中一样重新计算面积。
public Window(double width, double height) {
this.width = width;
this.height = height;
calculateArea();
}
private void calculateArea() {
double rectangleArea = width * height;
double radius = width / 2;
double circleArea = Math.PI * Math.pow(radius, 2);
double semiCircleArea = circleArea / 2;
double areaFeetSquared = rectangleArea + semiCircleArea;
area = areaFeetSquared * SQUARE_INCHES_PER_SQUARE_FOOT;
double amountOfGlaze = area / SQUARE_INCHES_PER_OUNCE_OF_GLAZE;
costOfGlaze = amountOfGlaze * COST_PER_OUNCE;
}
public void setDimensions(double width, double height) {
this.width = width;
this.height = height;
calculateArea();
}
关于java - 最后的预期计算与测试仪不匹配。我的 setDimensions 方法有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46400032/