第10次作业


题目1:

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

共有一个接口和三个类:

  • 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元。请你通过上面定义的接口和类,实现她通过中介买房的过程,显示需交纳的中介费和契税。

题目2:

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

 

 

 1 //第一题
 2 
 3 import java.util.Scanner;
 4 
 5 interface Business
 6 {
 7     double RATIO=0.022;
 8     double TAX=0.03;
 9     void buying(double price);
10 }
11 public class text0 {
12     public static void main(String[] args){
13         Buyer b=new Buyer();
14         Intermediary intermediary=new Intermediary(b);
15         Scanner scanner=new Scanner(System.in);
16         System.out.print("请输入房屋的标价:");
17         double price=scanner.nextDouble();
18         b.buying(price);
19         intermediary.buying(price);
20         intermediary.charing(price);
21     }
22 }
23 class Buyer implements Business
24 {
25     String name="Lisa";
26     public void buying(double price){
27         System.out.print(name+"购买一套住宅的价格是:"+price);
28     }
29 }
30 
31 class Intermediary implements Business
32 {
33     Buyer b;
34     Intermediary(Buyer buyer){
35         this.b=buyer;
36     }
37     public void buying(double price){
38         System.out.print(",需要支付的中介费:"+price*RATIO);
39         System.out.println(",需要交纳的契税:"+price*TAX);
40     }
41     public void charing(double price){
42         System.out.println("房屋中介需要收取的中介费:"+price*RATIO);
43     }
44 }

 

 1 // 第二题
 2 import java.util.Scanner;
 3 public class text{
 4     static void ex()throws textry{
 5         double m,n=0;
 6         Scanner sc=new Scanner(System.in);
 7         for(int i=0;i<5;i++){
 8             System.out.println("请输入所求的数字");
 9             m=sc.nextDouble();
10             if(m<0||m>100){
11                 throw new textry(m);
12             }
13             else {
14                 n+=m;
15             }
16        }
17         System.out.println(n/5);
18     }
19 public static void main(String[] args) {
20     try{ //异常
21         ex();
22     }catch(textry e ){
23         System.out.println(e);
24         }
25     }
26 }
27 class textry extends Exception{
28     private double number;
29     textry(double m){
30         number=m;
31     }
32     public String toString(){ //自定义异常返回值
33         return number+"不在预定范围";
34     }
35 }

第10次作业_第1张图片

 

 

你可能感兴趣的:(第10次作业)