第十次作业

题目一:

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

共有一个接口和三个类:

  • Business—— 业务接口
  • Buyer —— 购房者类
  • Intermediary—— 中介类
  • Test —— 主类

1.业务接口

业务接口包括:

(1)两个数据域(成员变量)

RATIO: double型,代表房屋中介收取的中介费用占房屋标价的比例,初值为0.022

TAX:double型,代表购房需要交纳的契税费用占房屋标价的比例,初值为0.03

(2)一个方法

void buying (double price):price表示房屋总价

2.购房者类

购房者类Buyer是业务接口Business的非抽象使用类,包括:

(1)一个成员变量

name:String型,表示购房者姓名

(2)一个方法:

public void buying (double price):显示输出购买一套标价为price元的住宅

3.中介类

中介类Intermediary是业务接口Business的非抽象使用类,包括:

  • 一个成员变量

buyer:Buyer型,代表房屋中介接待的购房对象

  • 三个方法

Intermediary(Buyer buyer):构造方法

public void buying (double price):购房者buyer购买一套标价为price元的住宅,之后计算需要支付的中介费和交纳的契税

public void charing(double price):表示计算购买标价为price元的住宅时,房屋中介需要收取的中介费和需要交纳的契税(中介费计算公式:房屋标价* RATIO,契税计算公式:房屋标价*TAX)

4.Test类

在Test类中定义购房对象——姓名Lisa,从控制台输入她计划买的房屋标价,如650000元。请你通过上面定义的接口和类,实现她通过中介买房的过程,显示需交纳的中介费和契税。


 

代码:

 1 package goufang;
 2 import java.util.*;
 3 public class Testt10 {
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         Scanner input = new Scanner(System.in);
 7         Business i = new Intermediary(new Buyer("Lisa"));
 8         System.out.println("所购买房屋的标价为:");
 9         double price = input.nextDouble();
10         i.buying(price);
11     }
12     }
 1 package goufang;
 2 
 3 public class Buyer implements Business{
 4         private String name = null;
 5         public Buyer(){}
 6         public Buyer(String name){
 7             this.name = name;
 8         }
 9         public void buying(double price) {
10             System.out.println(this.name + "购买一套标价为" + price + "元的住宅");
11         }
12         private String getName() {
13             return name;
14         }
15 }
1 package goufang;
2 
3 public interface Business {
4       double ratio = 0.022;
5         double tax = 0.03;
6         void buying(double price);
7 }
 1 package goufang;
 2 
 3 public class Intermediary implements Business{
 4     Buyer buyer = null;
 5     
 6     public Intermediary(){}
 7     public Intermediary(Buyer buyer) {
 8         this.buyer = buyer;
 9     }
10     public void buying(double price) {
11         buyer.buying(price);
12         charing(price);
13     }
14     public void charing(double price) {
15         System.out.println("房屋中介所需的费用为:" + (price *Business.ratio) + "元");
16         System.out.println("他/她需要交纳的契税为:" + (price * Business.tax) + "元");
17     }
18 }

运行结果:



题目二:

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

代码:

 1 package jisuanpingjun;
 2 import java.util.*;
 3 public class Test10 {
 4     static void overbound(double num) throws MyException{
 5         if(num>100||num<0)
 6             throw new MyException();
 7     }
 8         public static void main(String[] args) {
 9             System.out.println("请输入五个学生成绩:");
10             double sum=0;
11             double average=0;
12             Scanner reader = new Scanner(System.in);
13             try{
14               for(int i=0;i<5;i++){
15                 double num = reader.nextDouble();
16                 overbound(num);
17                 sum += num;
18             }
19               System.out.println("五位学生成绩平均值为:"+sum/5);
20             }catch (MyException e){
21                 System.out.println("捕获"+e);
22             }
23 
24     }
25     }
package jisuanpingjun;

public class testt10 extends Exception {
     double num;
     testt10(double a){
            num = a;
        }
        public String toString(){
            return "数字"+num+"超出范围";
        }
}

 

运行结果:

posted @ 2019-11-18 12:47  wangyd  阅读(108)  评论(0编辑  收藏  举报