m0_62438861 2021-09-30 11:52 采纳率: 77.8%
浏览 107
已结题

有会java的程序员吗?我是初学者希望可以教一下这个java编程怎么做

设计一个box类,这个类有长,宽,高,容积,使用容积,共五个属性,一个构造方法,传入三个参数初始化长宽高属性,一个计算容积方法,并将结果附值给容积属性,一个装入盒子的方法,可以改变使用容积属性,一个清空盒方法,将盒子使用容积置为零,一个判断盒子是否装满方法

  • 写回答

4条回答 默认 最新

  • limit、T 2021-09-30 11:58
    关注

    好兄弟,我可以教你吗?
    这个题就很简单,我们把需求拆分一下:
    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;
    }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • zlebhs 2021-09-30 12:54
    关注

    建议多自己动手敲一下,这是基础中的基础了。

    评论
  • mizuhokaga 2021-09-30 13:18
    关注
    
    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;
        }
    
    
    }
    
    评论
  • 轻描_看花开 2021-09-30 13:19
    关注

    这个都是基础了,面向对象语言,重在操作对象,你可以到B站上看看免费教学视频,

    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 10月13日
  • 已采纳回答 10月5日
  • 创建了问题 9月30日

悬赏问题

  • ¥50 74LS系列 74LS00 74LS04设计一个RS485电路(关键词-差分)
  • ¥30 各位help写一下代码
  • ¥15 在运行SDEdit模型下载不了
  • ¥15 求51控制l298n驱动的小车中超声波避障怎么写
  • ¥15 电脑连上WIFI却用不了
  • ¥30 MATLAB在RLC电路的固有响应和阶跃响应GUI仿真报告
  • ¥15 hyper-v出现的问题
  • ¥15 有能用的可加酬金,求可以批量下载懒人听书的软件,能登录自己帐号的。
  • ¥100 高博一起做RGB-D SLAM(5)VO无法出visualisation问题
  • ¥15 使用matlab进行手眼标定的仿真验证,得到齐次矩阵与opencv相差较大