第10次作业
-
题目
编写一个应用程序,模拟中介和购房者完成房屋购买过程。
共有一个接口和三个类:
-
Business—— 业务接口
-
Buyer —— 购房者类
-
Intermediary—— 中介类
-
Test —— 主类
1.业务接口
业务接口包括:
(1)两个数据域(成员变量)
RATIO: double型,代表房屋中介收取的中介费用占房屋标价的比例,初值为0.022
(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元。请你通过上面定义的接口和类,实现她通过中介买房的过程,显示需交纳的中介费和契税。
-
-
源程序
Test.java
1 /** 2 * 1.文件功能:计算需要交纳的中介费和契税 3 * 2.成员变量:无 4 * 3.方法:main 5 */ 6 import java.util.*; 7 public class Test { 8 9 public static void main(String[] args) { 10 // TODO Auto-generated method stub 11 Scanner in = new Scanner(System.in); 12 System.out.println("请输入房屋价格:"); 13 int price = in.nextInt(); 14 15 Buyer buyer = new Buyer(); 16 buyer.name = "Lisa"; 17 buyer.buying(price); 18 19 Intermediary intermediary = new Intermediary(buyer); 20 21 intermediary.buying(price); 22 intermediary.charing(price); 23 } 24 25 }
Business.java
1 /** 2 * 1.文件功能:接口 3 * 2.成员变量:RATIO:代表房屋中介收取的中介费用占房屋标价的比例 4 * TAX:代表购房需要交纳的契税费用占房屋标价的比例 5 * 3.方法:抽象方法 buying 6 */ 7 public interface Business { 8 double RATIO = 0.022; 9 double TAX = 0.03; 10 void buying (int price); 11 }
Buyer.java
1 /** 2 * 1.文件功能:购买者类 3 * 2.成员变量:name 购买者姓名 4 * 3.方法:输出要购买房屋的价格 5 */ 6 public class Buyer implements Business{ 7 String name; 8 9 public void buying (int price) { 10 System.out.println(name+"购买一套标价为"+price+"元的住宅"); 11 } 12 }
Intermediary.java
1 /** 2 * 1.文件功能:中介类 3 * 2.成员变量:buyer类 4 * 3.方法:buying计算并输出需要交纳的中介费和契税 5 */ 6 public class Intermediary implements Business{ 7 Buyer buyer; 8 9 Intermediary(Buyer buyer){ 10 this.buyer = buyer; 11 } 12 13 public void buying (int price) { 14 System.out.println(buyer.name+"需要交纳的契税为:"+price*TAX+"元"); 15 System.out.println(buyer.name+"需要支付的中介费为:"+price*RATIO+"元"); 16 } 17 18 public void charing(int price) { 19 System.out.println("中介需要交纳的契税为:"+price*TAX+"元"); 20 System.out.println("中介需要收取的中介费为:"+price*RATIO+"元"); 21 } 22 }
-
运行结果

题目二
-
题目
输入5个数,代表学生成绩,计算其平均成绩。当输入值为负数或大于100时,通过自定义异常处理进行提示。
-
源程序
Test.java
1 /** 2 * 文件功能:输入五个数求平均值 3 */ 4 import java.util.Scanner; 5 6 public class Test { 7 8 public static void main(String[] args) { 9 10 Average ave = new Average(); 11 12 System.out.println("平均值是" + ave.getAverage()); 13 System.out.println("==END=="); 14 } 15 16 }
Average.java
1 /** 2 * 文件功能:输入五个数大于零小于100的数并返回平均值 3 */ 4 import java.util.Scanner; 5 6 public class Average { 7 8 public final static int i = 5; 9 10 double getAverage() { 11 12 Scanner in = new Scanner(System.in); 13 double a = 0; 14 double sum = 0; 15 int count = 0; 16 17 System.out.println("请输入五个数"); 18 19 while (count < i) { 20 a = in.nextDouble(); 21 try { 22 if (a > 100 || a < 0) { 23 throw new MyException(); 24 } 25 26 } catch (MyException e1) { 27 System.out.println(e1.toString()); 28 continue; 29 } catch (Exception e2) { 30 continue; 31 } 32 sum += a; 33 count++; 34 } 35 36 return sum / i; 37 } 38 }
MyException.java
1 /** 2 * 文件功能:自定义异常类 重写toString方法 3 */ 4 public class MyException extends Exception { 5 6 public String toString() { 7 return "数字大于100或小于零,请重新输入"; 8 } 9 }
-
运行结果

-

浙公网安备 33010602011771号