十次作业

题目1:

编写一个应用程序,模拟中介和购房者完成房屋购买过程。

业务接口

package zhong;

public interface business {
       double ra = 0.022;
       double T = 0.03;
    
       void buying(double p);
    }

购房者类

package zhong;

public class buyer implements business {
        String name;
     
     public buyer(String name) {
           this.name = name;
       }
    
       public void buying(double p) {
    
           System.out.println( "购买一套标价为:" + p  );
       }
 }

中介类

package zhong;

public class intermediary implements business {
    
      buyer buyer;
    
         public intermediary(buyer buyer) {
super(); this.buyer = buyer; } public void buying(double p) { buyer.buying(p); this.charing(p); } public void charing(double p) { System.out.println("中介费为:" + p * ra); System.out.println("契税:" + p * T); } }

测试类

package zhong;
public class Test {
    public static void main(String[] args) {
        buyer buyer = new buyer("Lisa");
         intermediary intermediary = new intermediary(buyer);
        intermediary.buying(650000);
    }
}

测试结果

题目二

输入5个数,代表学生成绩,计算其平均成绩。当输入值为负数或大于100时,通过自定义异常处理进行提示。

自定义异常类

package yichang;

public class MyException extends Exception {
     
        double n;
    
      public MyException(double a) {
            n = a;
       }
    
       public String toString() {
    
            return "自定义["+n+"不在0到100中]";
       }
     
    }

测试类

package yichang; 
import java.util.Scanner;

public class tys {
     static void avg()throws MyException {
        double a;
        double sum = 0;
       Scanner s = new Scanner(System.in);
      System.out.println("5名成绩");
       try {
           for (int i = 0; i < 5; i++) {
                a = s.nextDouble();
              if (a> 100 || a < 0)
                   throw new MyException(a);
               sum += a;
           }
              System.out.println( sum / 5);
       } catch (MyException e) {
          System.out.println("捕获"+e);
       }

   }
}

结果

原文地址:https://www.cnblogs.com/shuang123/p/11852034.html