第十次作业

作业1

1.业务接口

1 package lianxi4;
2 
3 public interface Business {
4 double RATIO=0.022;
5 double TAX=0.03;
6  void buying (double price);
7 }

2.购房者类

 1 package lianxi4;
 2 
 3 public class Buyer implements Business{
 4  String name;//表示购房者姓名
 5  public Buyer(String name) {
 6     this.name=name;
 7 }
 8  public void buying (double price){                         //显示输出购买一套标价为price元的住宅
 9      System.out.println(name+"购买了一套总价为"+price+"元的住宅");
10  } 
11 }

3.中介类

 1 package lianxi4;
 2 
 3 public class Intermediary implements Business{
 4     Buyer buyer;
 5     Intermediary(Buyer buyer)     //构造方法
 6     {
 7         this.buyer=buyer;
 8         
 9     }
10     public void buying (double price){
11         buyer.buying(price);
12         this.charing(price);
13     }
14     public void charing(double price){
15         System.out.println("中介费为"+price*RATIO);
16         System.out.println("契税为"+price*TAX);
17     }
18 
19 }

4.Test类

package lianxi4;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
     Buyer buyer =new Buyer("Lisa");
    
     Intermediary intermediary = new Intermediary(buyer);
    Scanner reader = new Scanner(System.in);
    System.out.println("请输入要购买房屋的标价:");
    double price = reader.nextDouble();
    intermediary.buying(price);

    }

}

运行结果

 

 

作业2

题目简述

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

1.异常类

 1 package lianxi3;
 2 
 3 public class MyException extends Exception{
 4 
 5     public String myMessm(){
 6 
 7 
 8         return "成绩不能小于零或大于100,请重新输入!";
 9   }
10 }

2.计算类

 1 package lianxi3;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Jisuan {
 6 
 7     public int i;
 8 
 9     public void jisuan(){
10         Scanner reader = new Scanner(System.in);
11         System.out.println("请输入5个成绩");
12         int sum=0;
13         for(i=0;i<=4;i++){
14             int a=reader.nextInt();
15             try {
16                 if(a<0||a>100)
17                   throw new MyException();
18                 sum=sum+a;
19                 
20             } catch (Exception e) {
21                 System.out.println(((MyException) e).myMessm());
22             }
23             if(a<0||a>100)
24                 i--;
25         }
26         
27         int ave=sum/5;
28         System.out.println("平均成绩是:"+ave);
29           
30         }
31 }

3.主类

 1 package lianxi3;
 2 
 3 public class Example {
 4 
 5     public static void main(String[] args) {
 6 Jisuan s1 =new Jisuan();
 7   s1.jisuan();
 8         
 9     }
10 }

运行结果

 

 

posted @ 2019-11-16 17:34  常恒*&&  阅读(126)  评论(0编辑  收藏  举报