第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元。请你通过上面定义的接口和类,实现她通过中介买房的过程,显示需交纳的中介费和契税。
1.业务接口
业务接口1 package home; 2 3 public interface Business { 4 double RATIO =0.022;//房屋中介收取的中介费用占房屋标价的比例 5 double TAX=0.03;//购房需要交纳的契税费用占房屋标价的比例 6 static void buying (double price) { 7 // TODO Auto-generated method stub 8 9 } 10 }
2.购房者类
1 package home; 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) { 9 System.out.println(name+"购买一个"+price+"的住宅 "); 10 } 11 }
3.中介类
1 package home; 2 3 public class Intermediary implements Business { 4 Buyer buyer; 5 public Intermediary (Buyer buyer) { 6 this.buyer=buyer; 7 } 8 public void buying (double price) { 9 buyer.buying(price); 10 this.charing(price); 11 } 12 private void charing(double price) { 13 System.out.println("房屋中介需要收取的中介费"+price*RATIO); 14 System.out.println("需要交纳的契税"+price*TAX); 15 } 16 }
4.Test类
1 package home; 2 import java.util.*; 3 public class test { 4 public static void main(String[] args) { 5 Buyer buyer = new Buyer("Lisa"); 6 Scanner b=new Scanner(System.in); 7 System.out.println("请输入价格"); 8 Intermediary intermediary = new Intermediary(buyer); 9 double buyer1 =b.nextInt(); 10 intermediary.buying(buyer1); 11 } 12 }
题目2:
输入5个数,代表学生成绩,计算其平均成绩。当输入值为负数或大于100时,通过自定义异常处理进行提示。
1 import java.util.*; 2 class MyException extends Exception{ 3 private int exceptnumber; 4 MyException (int a){ 5 exceptnumber=a; 6 } 7 public String toString() { 8 return "数据异常"; 9 } 10 } 11 public class aaa { 12 static void makeExcept(int a) throws MyException { 13 if(a<0||a>100) 14 throw new MyException(a); 15 } 16 public static void main(String[] args) { 17 Scanner c = new Scanner(System.in); 18 int score[] = new int[5]; 19 int sum = 0; 20 System.out.print("请输入5个数"); 21 try { 22 for (int i = 0; i < score.length; i++) { 23 score[i] = c.nextInt(); 24 makeExcept(score[i]); 25 } 26 for (int i = 0; i < score.length; i++) { 27 sum += score[i]; 28 } 29 int b = 0; 30 b = sum / 5; 31 System.out.println("平均分为" + b); 32 } 33 catch (MyException e) { 34 System.out.println(e); 35 } 36 } 37 }
正常状态
异常