教材代码完成情况测试P171(课上测试)

一、任务详情

  • 0 在Ubuntu中用自己的有位学号建一个文件,教材p171 Example7_7
    1. 增加一个类DangerException2, 显示“超轻”异常,超轻的阈值minContent设为你的学号后三位。
  • 2.修改p171 Example7_7中代码和相关代码,增加一行对DangerException2进行测试
    1. 提交添加的代码和运行结果截图

二、源代码

1.Example7_7类

public class Example7_7 {
   public static void main(String args[]) {
      CargoBoat ship = new CargoBoat();
      ship.setMaxContent(1000);
      ship.setMinContent(215);
      int m =0;
	      try{
           ship.loading2(m);
		m=0;

	}
      catch(DangerException2 e){
           System.out.println(e.warnMess2());
	}
      try{  
           ship.loading(m);
           m = 216;
           ship.loading(m);
           m = 367;
           ship.loading(m);
           m = 555;
           ship.loading(m);
      }
      catch(DangerException e) {
           System.out.println(e.warnMess()); 
           System.out.println("无法再装载重量是"+m+"吨的集装箱");       
      }

      finally {
          System.out.printf("货船将正点启航");
      }
  }
}

2.CargoBoat类

public class CargoBoat {
     int realContent;  //装载的重量
     int minContent;//最小装载量
     int maxContent;   //最大装载量
     public void setMaxContent(int c) {
         maxContent = c;
     }
     public void setMinContent(int c){
        minContent = c;
    }
    public void loading2(int m) throws DangerException2{
        realContent += m;
        if(realContent<minContent){
            throw new DangerException2();
        }
        System.out.println("目前装载了"+realContent+"吨货物");
    }
     public void loading(int m) throws DangerException {
       realContent += m;
       if(realContent>maxContent) {
          throw new DangerException(); 
       }
       System.out.println("目前装载了"+realContent+"吨货物");
     }
}

3.DangerException类

public class DangerException extends Exception {
   final String message = "超重";
   public String warnMess() {
       return message;
   }
} 

4.DangerException2类

public class DangerException2 extends Exception {
   final String message = "超轻";
   public String warnMess2() {
       return message;
   }
} 

三、运行结果截图

我的学号是20175215,后三位是215,我测试的重量是0216

四、程序修改与完善

将Example7_7类修改后如下

public class Example7_7 {
    public static void main(String args[]) {
        CargoBoat ship = new CargoBoat();
        ship.setMaxContent(1000);
        int n=215;
        ship.setMinContent(n);
        int m =0;
        try{
            ship.loading2(m);
            m=100;
            ship.loading2(m);
            m=200;

        }
        catch(DangerException2 e){
            System.out.println(e.warnMess2());
            System.out.println("需要再装载"+(n-m)+"吨货物");
        }
        try{
            ship.loading(m);
            m = 400;
            ship.loading(m);
            m = 367;
            ship.loading(m);
            m = 555;
            ship.loading(m);
        }
        catch(DangerException e) {
            System.out.println(e.warnMess());
            System.out.println("无法再装载重量是"+m+"吨的集装箱");
        }

        finally {
            System.out.printf("货船将正点启航");
        }
    }
}

运行结果为:

posted @ 2019-04-15 14:23  20175215丁文韬  阅读(354)  评论(1编辑  收藏  举报