一、题目①
编写一个应用程序,模拟中介和购房者完成房屋购买过程。
共有一个接口和三个类:
- 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、Business.java
1 /**
2 * 接口Business,对RATIO和TAX进行赋值
3 * @author 赵鹏
4 */
5 public interface Business {
6
7 double RATIO = 0.022;
8 double TAX = 0.03;
9 public void buying(double price);
10
11 }
2、Buyer.java
1 /**
2 * Buyer接接口Business
3 * @author 赵鹏
4 */
5 public class Buyer implements Business {
6
7 String name ;
8 public void buying(double price){
9
10 }
11 }
3、intermediary.java
1 /**
2 * intermidary接接口Business
3 * 方法buying和charing分别求出中介费和契税并输出
4 * @author 赵鹏
5 */
6 public class intermediary implements Business {
7
8 void Intermediary(Buyer buyer){
9
10 }
11
12 public void buying(double price){
13 System.out.println("中介费为:" + price * RATIO);
14 }
15
16 public void charing(double price){
17 System.out.println("契税为:" + price * TAX);
18 }
19 }
4、Test.java
1 /**
2 * 主方法定义购买者的名字为Lisa
3 * 手动输入房屋标价
4 * 对中介费和契税两个方法进行调用
5 */
6 import java.util.Scanner;
7
8 public class Test {
9 /**
10 * @param args
11 */
12 public static void main(String[] args) {
13 // TODO Auto-generated method stub
14 Buyer buyer = new Buyer();
15 buyer.name = "Lisa";
16 intermediary a = new intermediary();
17
18 Scanner reader = new Scanner(System.in);
19
20 System.out.println("姓名:" + buyer.name);
21 System.out.println("输入房屋标价:");
22 double price = reader.nextDouble();
23 a.buying(price);
24 a.charing(price);
25 }
26
27 }
三、结果截图

四、题目②
输入5个数,代表学生成绩,计算其平均成绩。当输入值为负数或大于100时,通过自定义异常处理进行提示。
五、代码实现
1 /**
2 * MyException继承Exception,toString()进行方法重写
3 * makeExcept类方法中抛出异常对象
4 * 主方法中进行成绩的输入输出,若有异常,捕获异常MyException
5 */
6 import java.util.Scanner;
7
8 class MyException extends Exception{
9 private double exceptnumber;
10 MyException(double grade){
11 exceptnumber = grade;
12 }
13 public String toString(){
14 return "自定义异常["+ exceptnumber +"]";
15 }
16 }
17
18 public class average {
19 static void makeExcept(double grade) throws MyException{
20 if(grade > 100 || grade < 0)
21 throw new MyException(grade);
22 }
23 /**
24 * @param args
25 */
26 public static void main(String[] args) {
27 // TODO Auto-generated method stub
28 System.out.println("输入五个学生的成绩:");
29 Scanner reader = new Scanner(System.in);
30
31 double grade[] =new double[5];
32 double sum = 0;
33
34 for(int i = 0; i < 5; i++){
35 System.out.println("第" + (i+1)+ "个学生的成绩是:");
36 grade[i] = reader.nextDouble();
37 sum = sum + grade[i];
38
39 try{
40 makeExcept(grade[i]);
41 }catch(MyException e){
42 System.out.println("捕获" + e);
43 return;
44 }
45 }
46 System.out.println("这五个学生的平均成绩为:" + sum / 5);
47 }
48
49 }
六、结果截图
