【发布时间】:2019-10-31 09:34:45
【问题描述】:
public static void main(String[] args) {
double w;
double h;
double d;
Scanner input = new Scanner(System.in);
System.out.print ("Enter the width of your box:");
w= input.nextDouble();
System.out.print ("Enter the height of your box:");
h= input.nextDouble();
System.out.print ("Enter the depth of your box:");
d= input.nextDouble();
Box boxOne = new Box(w, h, d);
System.out.println(boxOne.toString());
boxOne.setDim(w, h, d);
}
}
class Box
{
private double width = 1.0;
private double height = 1.0;
private double depth = 1.0;
public Box (double w, double h, double d)
{
setWidth(w);
setHeight(h);
setDepth(d);
}
public void setWidth(double w)
{
if(w > 0)
{
width = w;
}
else
{
width = 1.0;
}
}
public void setHeight(double h)
{
if(h > 0)
{
height = h;
}
else
{
height = 1.0;
}
}
public void setDepth(double d)
{
if(d > 0)
{
depth = d;
}
else
{
depth = 1.0;
}
}
public void setDim(double width, double height, double depth)
{
double volume=width*height*depth;
System.out.println("The volume of the box is "+volume);
}
public double volume ()
{
}
public String getWidth()
{
return String.format("%f",width);
}
public String getHeight()
{
return String.format("%f",height);
}
public String getDepth()
{
return String.format("%f",depth);
}
public String toString()
{
return String.format("Width is %s.\nHeight is %s.\nDepth is %s.", getWidth(), getHeight(),getDepth());
}
public boolean equalTo (Box o)
{
}
}
我不明白如何在这段代码中使用 public boolean equalTo (Box o) 和 public double volume() 方法。我应该在这两种方法的主体中写什么?以及如何在主要方法中使用它们?我不理解这两种方法。如何创建boolean equalTo() 方法和double volume() 方法,以及如何测试它们?
【问题讨论】:
-
Java 和 JavaScript 是两种完全不同的编程语言。因此,当您对
Java有疑问时,不应标记JavaScript。 -
调用equalTo >
equals,以后Java可以自动使用,这是继承自Object类的方法