java编程题(写出代码)

编程创建一个Box类(长方体),在Box类中定义三个变量,分别表示长方体的长(length)、宽(width)和高(heigth),再定义一个方法void setBox(int l, int w, int h) 对这三个变量进行初始化,然后定义一个方法int volume ()来计算长方体的体积。最后,在main()方法中创建一个Box类的对象b,首先通过调用对象b的setBox()方法来设置长方体的长、宽和高,再通过调用对象b的volume() 方法来计算这个给定尺寸的长方体的体积,并输出这个结果。

代码如下:

class Box {

private int length;

private int width;

private int height;

public void setBox(int l, int w, int h) {
this.length = l;
this.width = w;
this.height = h;
}

public int volume() {
return length * width * height;
}
}

public class App {

public static void main(String[] argv) {

Box b = new Box();

b.setBox(3, 4, 5);

System.out.println("体积:" + b.volume());
}
}

温馨提示:答案为网友推荐,仅供参考
相似回答