第十次作业
-
一、题目
编写一个应用程序,模拟中介和购房者完成房屋购买过程。
共有一个接口和三个类:
- Business—— 业务接口
- Buyer —— 购房者类
- Intermediary—— 中介类
- Test —— 主类
二、代码
Test1.java
package cn; import java.util.*; public class Test1 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("请输入房屋的价格"); Scanner reader = new Scanner(System.in); double price=reader.nextDouble(); Buyer buyer=new Buyer("lisa"); intermediary A=new intermediary(buyer); //创建对象 buyer.buying(price); A.buying(price); A.charing(price); } }
Buyer.java
package cn; public class Buyer implements Business{ String name; Buyer(String name){ this.name=name; } public void buying(double price){ System.out.print("住宅的标价"+price); } }
Business.java
package cn; public interface Business { double RATIO = 0.022; double TAX= 0.03; void buying(double price); }
intermediary .java
package cn; public class intermediary { Buyer buyer; intermediary(Buyer buyer){ this.buyer=buyer; } public void buying (double price){ System.out.println(buyer.name+"所需要支付的中介费"+price*Business.RATIO+"和交纳的契税"+price*Business.TAX); } public void charing(double price){ System.out.println("房屋中介所需要收取的中介费"+price*Business.RATIO+"和交纳的契税"+price*Business.TAX); } }
三、结果
题目二:
输入5个数,代表学生成绩,计算其平均成绩。当输入值为负数或大于100时,通过自定义异常处理进行提示。
二、代码
package vb; public class Myexception extends Exception{ private double exceptnumber; Myexception(double a){ exceptnumber = a; } public String toString(){ return "自定义异常"; } }
package vb; import java.util.Scanner; public class Test { static void makeExcept(int a)throws Myexception{ //声明异常 if(a<0||a>100) throw new Myexception(a); } public static void main(String[] args) { // TODO Auto-generated method stub int sum=0; int average; try { for(int i=0;i<=5;i++) { Scanner reader = new Scanner(System.in); int a=reader.nextInt(); makeExcept(a); sum+=a; } average = sum / 5; System.out.println("平均成绩为"+average); } catch(Myexception e) { System.out.println(e); } } }