第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 interface Business{ 2 double RATIO = 0.022; 3 double TAX = 0.03; 4 void buying(double price); 5 } 6 class Buyer implements Business{ 7 String name; 8 public void buying(double price) { 9 System.out.println(name+"购买一套标价为"+price+"元的住宅"); 10 } 11 } 12 class Intermediary implements Business{ 13 Buyer buyer; 14 Intermediary(Buyer buyer){ 15 this.buyer=buyer; 16 } 17 public void buying(double price) { 18 charing(price); 19 } 20 public void charing(double price) { 21 System.out.println("房屋中介费:"+price*RATIO+"交纳的契税"+price*TAX); 22 } 23 } 24 public class zuoyeb { 25 26 public static void main(String[] args) { 27 // TODO Auto-generated method stub 28 Scanner reader=new Scanner(System.in); 29 Buyer b=new Buyer(); 30 b.name="Lisa"; 31 double price=reader.nextDouble(); 32 b.buying(price); 33 Intermediary i=new Intermediary(b); 34 i.buying(price); 35 } 36 37 }
题目2:
输入5个数,代表学生成绩,计算其平均成绩。当输入值为负数或大于100时,通过自定义异常处理进行提示。
1 package com.ccut; 2 3 /** 4 * @author Ruayo 5 * @project arithmetic-generator 6 * @package com.ccut 7 * @date 2019/11/13 16:57 8 */ 9 public class MyException extends Exception { 10 private double excpt; 11 MyException(double score){ 12 excpt = score; 13 } 14 15 public String toString() { 16 return "异常"+excpt; 17 } 18 }
1 package com.ccut; 2 3 import java.util.Scanner; 4 5 /** 6 * @author Ruayo 7 * @project arithmetic-generator 8 * @package com.ccut 9 * @date 2019/11/13 16:53 10 */ 11 public class Text { 12 static void makeExcep(double score) throws MyException { 13 if (score < 0 || score > 99) throw new MyException(score); 14 } 15 public static void main(String[] args) { 16 double score; 17 double sum=0; 18 Scanner in1 = new Scanner(System.in); 19 try { 20 for (int i = 1; i <= 5; i++) { 21 score = in1.nextDouble(); 22 makeExcep(score); 23 sum += score; 24 } 25 System.out.println(sum / 5); 26 } catch (MyException e) { 27 System.out.println("捕获" + e); 28 } 29 } 30 }