第1关:计算并输出盒子的体积

  • 任务要求
  • 参考答案
  • 评论5

任务描述

假设Box是一个长方体盒子,我们需要为Box类添加一个方法volume,来计算和输出盒子的容积

编程要求

根据提示,在右侧编辑器补充代码,计算并输出盒子的体积。

测试说明

我会对你编写的代码进行测试: 可用关键字new来创建Box类的对象,并为Box类的对象赋值,如下例box即为 Box类的对象:


  1. Box box = new Box(); //创建对象 box
  2. box.width = 10; //box对象的属性赋值
  3. box.height = 20;
  4. box.depth = 10;
  5. box.volume();

预期输出: the volume is:2000.0

package task1;

public class Box {

    double width;
    double height;
    double depth;
    public void volume() {
        //TODO write your code here
        double v=width*height*depth;
        System.out.println("the volume is:"+v);


    }

}

第2关:计算并返回盒子的体积

  • 任务要求
  • 参考答案
  • 评论5

任务描述

假设Box是一个长方体,我们需要为Box类添加一个方法volume,计算并返回盒子的容积。

编程要求

根据提示,在右侧编辑器补充代码,计算并返回盒子的体积。

测试说明

我会对你编写的代码进行测试:

创建出Box对象,然后为Box对象赋值,并调用volume()方法,将盒子的体积作为函数的返回值返回:


  1. Box box = new Box();
  2. box.width = 10;
  3. box.height = 20;
  4. box.depth = 10;
  5. double volume = box.volume();

预期输出:

the volumn is:2000.0

package task2;

public class Box {

    double width;
    double height;
    double depth;
/************Begin********/
 public double volume(){
     return width*height*depth;
 }

/************End********/       

}

第3关:计算并返回给定几何参数的盒子容积

  • 任务要求
  • 参考答案
  • 评论5

任务描述

我们需要为Box类新添一个方法volume,计算并返回盒子的容积。 方法volume(double width,double height,double depth)的参数是盒子的长,宽,高。

编程要求

根据提示,在右侧编辑器补充代码,计算并输出盒子的体积。

测试说明

我会对你编写的代码进行测试:

newBox对象,然后为Box对象赋值,并调用volume方法,传入圆柱体几何类型,最后打印出盒子的体积:


  1. Box box = new Box();
  2. double volume = box.volume(10,20,15 );
  3. System.out.println( "the volume is:" + volume );

预期输出: the volume is:3000.0

提示:


  1. public class Box {
  2. double width;
  3. double height;
  4. double depth;
  5. public double volume(double width,double height,double depth) {...}
package task3;

public class Box {

    double width;
    double height;
    double depth;

    //TODO write your code here
    public double volume(double width,double height,double depth){
        return width*height*depth;
    }

}

 

第4关:让Box保护其管理的数据

  • 任务要求
  • 参考答案
  • 评论5

任务描述

基于任务3,我们在使用Box对象的过程中发现,方法volume可以任意变更内部的widthheight等参数,这是非常危险的,因此我们希望在使用的过程中,能够保护其内部数据,避免随意被修改。 #####编程提示 通过在数据成员前面加关键字private,可以做到对数据的保护,避免当前类外的数据直接调用和修改,做到数据隐藏。 被private修饰的数据成员,只能通过该类提供的对外接口对其操作。

编程要求

根据提示,在右侧编辑器补充代码,实现widthheight的隐藏,并通过设计成员变量的getset方法来实现对其读取操作,完成输出盒子的数据信息。

测试说明

我会对你编写的代码进行测试:

newBox对象,然后调用set方法为Box对象赋值,最后打印出盒子的widthheight信息:


  1. Box box=new Box();
  2. box.setWidth( 10 );
  3. box.setHeight( 20 );
  4. System.out.println( "the width is:" + box.getWidth() );
  5. System.out.println( "the height is:" + box.getHeight() );

预期输出:


  1. the width is:10.0
  2. the height is:20.0

提示:


  1. public class Box {
  2. private double width;
  3. private double height;
  4. private double depth;
  5. public void setWidth(double width){
  6. //TO DO YOU JOB
  7. }
  8. public double getWidth(){ return width;}
  9. //TO DO YOU JOB
  10. }
  11. ......
  12. }
package task4;

public class Box {

    private double width;
    private double height;
    private double depth;

  /********* Begin *********/        
  public void setWidth(double width){
    this.width=width;
  }
  
  public double getWidth(){
    return width;
  }

  public void setHeight(double height){
    this.height=height;
  }
  
  public double getHeight(){
    return height;
  }

  public void setDepth(double depth){
    this.depth=depth;
  }
  
  public double getDepth(){
    return depth;
  }


  /********* End *********/        

}

第5关:通过构造函数指定Box的几何参数,并添加方法计算容积。

100

  • 任务要求
  • 参考答案
  • 评论5

任务描述

我们需要为Box类添加一个带参数的构造函数,来初始化盒子。另外,还需要定义一个无参的函数volume,计算并返回盒子的容积。

编程要求

根据提示,在右侧编辑器补充代码,计算并输出盒子的体积。

测试说明

我会对你编写的代码进行测试:


  1. Box box = new Box(10,25,30 );
  2. double vol = box.volume();
  3. System.out.println( "the volume is:" + vol );

预期输出: the volume is:7500.0

提示:


  1. public class Box {
  2. double width;
  3. double height;
  4. double depth;
  5. public Box(double width,double height,double depth) {
  6. // your code
  7. }
  8. public double volume() {
  9. // your code
  10. }
  11. }
package task5;

public class Box {

    double width;
    double height;
    double depth;

    //TODO your code
public  Box(double width,double height,double depth){
    this.width=width;
    this.height=height;
    this.depth=depth;
}

public Box(){
}

public double volume(){
    return width*height*depth;
}

}

第6关:构造一个Box的子类ScaleBox,能设置盒子的几何尺寸,根据盒子类型计算盒子容积

100

  • 任务要求
  • 参考答案
  • 评论5

任务描述

在前面的任务中,我们并没有给Box赋予盒子类型,现在我们为其添加类型属性,但是我们不希望改变Box原有代码,又希望能够拥有Box的全部数据,这个时候,继承就派上用场了。

编程要求

根据提示,在右侧编辑器补充代码,完成子类ScaleBox的定义。要求能根据输入的盒子类型及大小,输出盒子的容积,并且在输出时带上盒子的类型信息。 ####编程提示

编写一个类ScaleBox,继承自Box,然后为ScaleBox增加一个私有属性type,代表盒子形状类型,为type添加相应的get/set方法,最后我们编写一个printVolumn方法,返回盒子的容积信息。并且增加构造函数public ScaleBox(int type)public ScaleBox(double width,double height,double depth,int type)参数中有代表盒子类型的参数typeBox类中已经有无参构造public Box()和含参构造参数 public Box(double width,double height,double depth)可供使用。

测试说明

我会对你编写的代码进行测试: type=1时,代表长方体,type=2时,代表圆柱体。圆柱体计算时,π=3.14


  1. ScaleBox box1 = new ScaleBox(2);
  2. box1.setWidth( 15 );
  3. box1.setHeight(24);
  4. box1.setDepth(12);
  5. box1.printVolumn();
  6. box1.setType(1);
  7. box1.printVolumn();
  8. ScaleBox box2 = new ScaleBox(10,12,15,1);
  9. box2.setHeight(22);
  10. System.out.printf("the volumn is:%6.2f",box2.printVolumn());
package task6;

public class ScaleBox extends Box{         
    int Type;//1 长方体 2 圆柱体
    public ScaleBox(){}
    public ScaleBox(int boxType) {
        //TODO write your code here
        this.Type=boxType;
    }
    public ScaleBox(double width,double height,double depth,int boxType) {
        //TODO write your code here
        super(width,height,depth);
        this.Type=boxType;
    }

 /**********************Begin*******************/
    //TODO write your code here
    public void setType(int Type){
        this.Type=Type;
    }
    public int getType(){
        return Type;
    }

    public double getWidthFromBox(){
        return getWidth();
    }
    public double getHeightFromBox(){
        return getHeight();
    }
    public double getDepthFromBox(){
        return getDepth();
    }

    public double printVolumn(){
        if(Type==1){
            return getWidthFromBox()*getHeightFromBox()*getDepthFromBox();
        }
        else{
            return 3.14*getWidthFromBox()*getWidthFromBox()*getHeightFromBox();
        }
    }
 /**********************End*******************/
}

 因为width,hright,depth均为父类的私有属性,在子类中不可直接调用,所以要间接调用父类的get函数来获取用户输入的数值。

此处用到的3.14不可以用Math.PI来代替,会导致精度不对而通过不了测试


 

 

 

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐