【问题标题】:Box class, getFull() method will not display accurate boolean resultBox 类,getFull() 方法不会显示准确的布尔结果
【发布时间】:2019-03-06 01:37:14
【问题描述】:

设计并实现一个名为 Box 的类,其中包含表示盒子高度、宽度和深度的实例数据。还包括一个名为 full 的布尔变量作为实例数据,表示盒子是否已满。定义 Box 构造函数以接受并初始化盒子的高度、宽度和深度。

每个新创建的 Box 都是空的(构造函数应该将 full 初始化为 false)。包括所有实例数据的 getter 和 setter 方法。包括一个 toString 方法,该方法返回该框的单行描述。提供了一个驱动程序,用于实例化和更新多个 Box 对象的主要方法用于测试目的。

但是,我试图让我的 getFull() 方法来判断维度是否相乘到 125,而事实并非如此。 getFull() 方法仍然显示框已满是真的,这是错误的。

盒子类:

package BoxClass;

public class Box {

double height, width, depth;
boolean full;
private double fullbox = 125.0;

public Box(double height_double, double width_double, double depth_double) //Constructor
{
    height = height_double;
    width = width_double;
    depth = depth_double;
    full = false;
}
public boolean Full()
{
    return(true);
}
public double getHeight() //Getters
{
    return(height);
}
public double getWidth()
{
    return(width);
}
public double getDepth()
{
    return(depth);
}
public boolean getFull()
{
    if(((height)*(width)*(depth)) == (fullbox))
    {
        return(true);
    }
}
public void setHeight(double height2) //Setters
{
    height = height2;
}
public void setWidth(double width2)
{
    width = width2;
}
public void setDepth(double depth2)
{
    depth = depth2;
}
public void setFull(boolean full2)
{
    full = full2;
}
public String toString()
{
    return("Height: " + height + "| Width: " + width + "| Depth: " + depth + " | Full? " + Full());
}

}

驱动类:

package BoxClass;

public class BoxTest {

public static void main(String[] args) {

    Box obj1, obj2, obj3;

    obj1 = new Box(2.05,2.05,0.05);
    obj2 = new Box(3.06,0.08,1.54);
    obj3 = new Box(0.05,2.06,2.09);

    System.out.println(obj1);
    System.out.println(obj2);
    System.out.println(obj3);

}

}

【问题讨论】:

    标签: java class methods driver box


    【解决方案1】:

    你在这个方法中有一个错字:

    public String toString()
    {
        return("Height: " + height + "| Width: " + width + "| Depth: " + depth + " | Full? " + Full());
    }
    

    您正在调用 Full() 方法,该方法始终返回 true。您要调用的方法是 getFull()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 2017-02-08
      • 1970-01-01
      • 2013-11-24
      • 2012-04-28
      • 1970-01-01
      相关资源
      最近更新 更多