设计一个box类,这个类有长,宽,高,容积,使用容积,共五个属性,一个构造方法,传入三个参数初始化长宽高属性,一个计算容积方法,并将结果附值给容积属性,一个装入盒子的方法,可以改变使用容积属性,一个清空盒方法,将盒子使用容积置为零,一个判断盒子是否装满方法
好兄弟,我可以教你吗?
这个题就很简单,我们把需求拆分一下:
1、设计一个box类:创建一个文件为box.java,并在里面写上:
public class box{}
2、这个类中有五个属性:在刚刚写的类( {}这个大括号的中间加入 )中加入:
public class box{
private double chang; // 长
private double kuan; // 宽
private double gao; // 高
private double rong; // 容积
private double useRong; // 使用容积
}
3、一个三个参数的构造方法:
public class box{
private double chang; // 长
private double kuan; // 宽
private double gao; // 高
private double rong; // 容积
private double useRong; // 使用容积
// 初始化长宽高的构造函数
public box(double chang, double kuan, double gao){
this.chang = chang;
this.kuan = kuan;
this.gao = gao;
}
}
4、一个计算容积的方法,一个使用容积的方法、一个清空使用容积的方法、一个判断是否装满的方法:
public class box{
private double chang; // 长
private double kuan; // 宽
private double gao; // 高
private double rong; // 容积
private double useRong; // 使用容积
// 初始化长宽高的构造函数
public box(double chang, double kuan, double gao){
this.chang = chang;
this.kuan = kuan;
this.gao = gao;
}
// 计算容积的方法
public String setRong(){
this.rong = this.changthis.kuanthis.gao;
return "容积计算成功,容积为:"+this.rong;
}
// 使用容积的方法
public String addUseRong(double a){
if((this.useRong+a)>this.rong){
return "太多了装不下";
}
if(this.isOver()){
return: "已经装满了";
}
this.useRong = this.useRong + a;
return "装好了~已使用:"+this.useRong;
}
// 清空使用容积的方法
public String clearUseRong(){
this.useRong = 0;
return "清空了~";
}
// 判断是否装满的方法
public boolean isOver(){
if(this.useRong == this.rong) {
return true;
}
return false;
}
}
建议多自己动手敲一下,这是基础中的基础了。
public class box {
private int length;
private int width;
private int height;
private int volume;
//已使用了的容积
private int usedVolume;
public static void main(String[] args) {
box mybox=new box(1,2,3);
box t=new box(1,2,3);
mybox.adjustUsedVolume(mybox,t.getVolume());
System.out.println(mybox.isFill(mybox));
mybox.clear(mybox);
System.out.println("UsedVolume:"+mybox.getUsedVolume());
}
box(int l, int w, int h) {
this.length = l;
this.width = w;
this.height = h;
this.usedVolume=0;
this.volume=this.computeVolume(this);
}
public int computeVolume(box b) {
int res = 0;
try {
res = b.getLength() * b.getHeight() * b.getWidth();
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
public void adjustUsedVolume(box b, int anotherVolume) {
int t = b.getUsedVolume() + anotherVolume;
System.out.println(t);
if (t <=b.getVolume()) {
b.setUsedVolume(t);
} else {
System.out.println("error");
}
}
public void clear(box b) {
if (b.getVolume() != 0) {
if (b.getUsedVolume() != 0) {
b.setUsedVolume(0);
}
}
}
public boolean isFill(box b) {
return b.getVolume() == b.getUsedVolume() ? true : false;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
public int getUsedVolume() {
return usedVolume;
}
public void setUsedVolume(int useVolume) {
this.usedVolume = useVolume;
}
}
这个都是基础了,面向对象语言,重在操作对象,你可以到B站上看看免费教学视频,