java 如何使用 boxTest 从盒子类中调用盒子的长度、宽度、高度、面积、体积

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27325803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:37:53  来源:igfitidea点击:

how to call length, width, height, area, volume of a box from box class using boxTest

java

提问by NumbNuts

I am writing a code to measure the surface area of a box and volume of the box. Finally, I got it to works. Now the challenges are I have to make 4 object and and store it into array and then use enhanced for loop to go through each box in the array. I mean when you loop through the array, it will get to first box, and ask you to enter the length, width, and height. Then it will show you the first box's length, width, height, surface area, and volume. I try to find an example for it but I cant find anything. I am still trying to make it work. Thank you for your help. here is my Box code.

我正在编写一个代码来测量盒子的表面积和盒子的体积。最后,我让它工作了。现在的挑战是我必须制作 4 个对象并将其存储到数组中,然后使用增强的 for 循环遍历数组中的每个框。我的意思是当你遍历数组时,它会到达第一个框,并要求你输入长度、宽度和高度。然后它会显示第一个盒子的长度、宽度、高度、表面积和体积。我试图为它找到一个例子,但我找不到任何东西。我仍在努力让它发挥作用。谢谢您的帮助。这是我的 Box 代码。

public class Box
{
    private double length = 1.0;
    private double width = 1.0;
    private double height = 1.0;

    //constructor
    public Box (double l, double w, double h)
    {
        setLength(l);
        setWidth(w);
        setHeight(h);
    }

    //set length method
    public void setLength(double l)
    {
        if(l > 0)
        {
            length = l;
        }
        else
        {
            length = 1.0;
        }
    }

    //set width method
    public void setWidth(double w)
    {
        if(w > 0)
        {
            width = w;
        }
        else
        {
            width = 1.0;
        }
    }

    //set height method
    public void setHeight(double h)
    {
        if(h > 0)
        {
            height = h;
        }
        else
        {
            height = 1.0;
        }
    }

    //calculate area method
    public double calculateArea(double length, double width)
    {
        return (length*width);
    }

    //calculate volume method
    public double calculateVolume(double length, double width, double height)
    {
        return (length*width*height);
    }

    //get length method
    public String getLength()
    {
        return String.format("%f", length);
    }

    //get width method
    public String getWidth()
    {
        return String.format("%f",width);
    }

    //get height
    public String getHeight()
    {
        return String.format("%f",height);
    }   

    public String toString()
    {
        return String.format("Length is %s.\nWidth is %s.\nHeight is %s.\n", getLength(), getWidth(), getHeight());
    }

}

}

and here is my main code

这是我的主要代码

import java.util.Scanner;

public class BoxTest
{
    public static void main(String[] args)
    {
        //Box boxOne, boxTwo, boxThree, boxFour;          
        double l;
        double w;
        double h;

        Scanner input = new Scanner(System.in);
        int[] boxes = new int[4];
        System.out.print ("Enter the length of your box:");
        l= input.nextDouble();
        System.out.print ("Enter the width of your box:");
        w= input.nextDouble();
        System.out.print ("Enter the height of your box:");
        h= input.nextDouble();

        Box boxOne = new Box(l, w, h);
        System.out.println(boxOne.toString());
        System.out.printf("The surface area of the box is %f.\nThe volume of the box is %f.\n", 
                          boxOne.calculateArea(l, w), boxOne.calculateVolume(l, w, h));


    }
}

回答by Luis Lavieri

You haven't declared those doublesin your constructor.

您尚未doubles在构造函数中声明这些。

it should be like this:

它应该是这样的:

double l = 50.55;
double w = 40.99;
double h = 12.33;
Box boxOne = new Box(l, w, h);
...

EDIT:

编辑

I just saw that you are trying to get the values from the Scanner.

我刚刚看到您正试图从Scanner.

You should first read them, store them in the variables, and then create a newinstance of Box. Not the other way around.

你应该先阅读它们,将它们存储在变量,然后创建一个new实例Box。不是反过来。

So,

所以,

Scanner input = new Scanner(System.in);
System.out.print ("Enter the dimension of your box:");
l= input.nextDouble();
w= input.nextDouble();
h= input.nextDouble();

Box boxOne = new Box(l, w, h);
...

回答by Konato_K

Because you do not use semicolons to separate parameters, use commands instead.

因为不使用分号分隔参数,请改用命令。

Also if you're passing a parameter you don't need to specific the type.

此外,如果您要传递参数,则不需要指定类型。

Try using Box boxOne = new Box(length, width, height);

尝试使用 Box boxOne = new Box(length, width, height);

回答by Chris

See changes and comments in the code:

查看代码中的更改和注释:

public class Box
{
    private double length;  // No need to set default values here
    private double width;   // No need to set default values here
    private double height;  // No need to set default values here

    //alternative constructor
    public Box()
    {
        //Just instantiate the box, but do not set anything.
    }

    //constructor
    public Box (double l, double w, double h)
    {
        this.length = l;  // Don't use method calls that can be overridden in
        this.width = w;   // your constructor.
        this.height = h;
    }

    //set length method
    public void setLength(double l)
    {
        if(l > 0)
        {
            this.length = l; // You have to use this.length to set
                             // the value stored in the object
        }
        else
        {
            this.length = 1.0;
        }
    }

    //set width method
    public void setWidth(double w)
    {
        if(w > 0)
        {
            width = w; // same as above
        }
        else
        {
            width = 1.0; // same as above
        }
    }

    //set height method
    public void setHeight(double h)
    {
        if(h > 0)
        {
            height = h; // same as above
        }
        else
        {
            height = 1.0; // same as above
        }
    }

    //calculate area method
    public double calculateArea() // don't use new parameters. Instead use the values
                                  // stored in the object (this.length and this.width)
    {
        return (this.length * this.width);
        // the formula is not correct, because this only calculates the area
        // of one side of the box, but I will let you figure that out yourself ;-)
    }

    //calculate volume method
    public double calculateVolume() // same as above
    {
        return (this.length * this.width * this.height);
    }

    //get length method
    public String getLength()
    {
        // It's not advisable to convert the type of the variable
        // in a standard getter, you should rather use something like
        // getLengthAsString and use getLength to return the length as
        // it is.

        return String.format("%f", this.length); // You have to use this.length to return
                                                 // the value stored in the object
    }

    //get width method
    public String getWidth()
    {
        return String.format("%f",width); //same as above
    }

    //get height
    public String getHeight()
    {
        return String.format("%f",height); //same as above
    }   

    public String toString()
    {
        return String.format("Length is %s.\nWidth is %s.\nHeight is %s.\n", getLength(), getWidth(), getHeight());
    }

For the main function:

对于主函数:

To declare an array of 4 boxes you would do:

要声明一个包含 4 个框的数组,您可以执行以下操作:

Box[] boxes = new Box[4];

And to put a box at the first position in the array:

并在数组的第一个位置放置一个框:

boxes[0] = new Box();

then read the user input and set the box-properties accordingly:

然后读取用户输入并相应地设置框属性:

    newBox.setHeight(scanner.nextDouble());
    newBox.setLength(scanner.nextDouble());
    newBox.setWidth(scanner.nextDouble());

This should give you all you need to fix the problem.

这应该为您提供解决问题所需的一切。