简体   繁体   English

最后预期的计算与测试仪不匹配。 我的 setDimensions 方法有什么问题吗?

[英]Last expected calculation doesn't match tester. Could my setDimensions method be whats wrong?

for my coding class we are creating a class that calculates the cost of a window's glaze for our window company.对于我的编码课程,我们正在创建一个课程来计算我们窗户公司的窗户玻璃的成本。 When grading my code with the tester it seems that my method to change the dimensions of the window doesnt work.使用测试仪对我的代码进行评分时,我更改窗口尺寸的方法似乎不起作用。 Here is the constructor of the 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;
}

And here is my setDimensions method that isn't working这是我的 setDimensions 方法不起作用

public void setDimensions(double theWidth, double theHeight) {
    width= theWidth;
    height= theHeight;
}

If you need more context, I would be more than happy to provide some more when asked.如果您需要更多背景信息,我很乐意在被问到时提供更多信息。 Thank you for reading, I hope you can help me.感谢您的阅读,希望您能帮到我。

Besides updating the width and height, you also need to recalculate the area like you do in the constructor.除了更新宽度和高度之外,您还需要像在构造函数中一样重新计算面积。

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

You Need to recalculate your properties after updating width and height .您需要在更新widthheight后重新计算您的属性。 You can do so by calling a method which basically just executes the code inside your constructor again.您可以通过调用一个方法来实现,该方法基本上只是再次执行构造函数中的代码。

public void setDimensions(double theWidth, double theHeight)
{
    width= theWidth;
    height= theHeight;
    recalculate();
}

And the recalculate() method could look like this:recalculate()方法可能如下所示:

private void recalculate()
{
    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;
}

This would update your properties after changing width and height .这将在更改widthheight后更新您的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM