java - Box 类,getFull() 方法不会显示准确的 boolean 结果

标签 java class methods driver box

设计并实现一个名为 Box 的类,其中包含表示盒子的高度、宽度和深度的实例数据。还包括一个名为 full 的 boolean 变量作为实例数据,表示盒子是否已满。定义 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);

}

}

最佳答案

此方法中有一个拼写错误:

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

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

关于java - Box 类,getFull() 方法不会显示准确的 boolean 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52582711/

相关文章:

java - 使用我的第一个 HelloWorldLogin android 代码获取 "Force Close"

javascript - 在 Javascript 中调用函数时使用 "()"的优缺点?

java - 首先将 Eclipse 中的现有本地存储库提交到空的 Github 存储库

java - Java 类的驱动程序 “Rectangle"

Java数据文件在类之间共享

java - 子类/接口(interface)和父类(super class)之间的转换

iphone - 通过单击 UIWebView 内的文本来调用 Objective-C 方法

c# - 这些默认参数的人造模拟之间是否存在显着的机械差异?

java - 将 SDL 2.0 游戏移植到 Android

java - HttpPost 响应不返回 json 对象