题目1:
编写一个应用程序,模拟中介和购房者完成房屋购买过程。
1.Business
(1)两个数据域(成员变量)
RATIO: double型,代表房屋中介收取的中介费用占房屋标价的比例,初值为0.022
TAX:double型,代表购房需要交纳的契税费用占房屋标价的比例,初值为0.03
(2)一个方法
void buying (double price):price表示房屋总价
package house; public interface Business { double RATIO=0.022; double TAX=0.03; void buying (double price); }
2.Buyer
(1)一个成员变量
name:String型,表示购房者姓名
(2)一个方法:
public void buying (double price):显示输出购买一套标价为price元的住宅
package house; public class Buyer implements Business{ String name; Buyer (String name){ this.name=name; } @Override public void buying(double price) { // TODO Auto-generated method stub System.out.println("购买一套标价为"+price+"元的住宅"); } }
3.Intermediary
中介类Intermediary是业务接口Business的非抽象使用类,包括:
- 一个成员变量
buyer:Buyer型,代表房屋中介接待的购房对象
- 三个方法
Intermediary(Buyer buyer):构造方法
public void buying (double price):购房者buyer购买一套标价为price元的住宅,之后计算需要支付的中介费和交纳的契税
public void charing(double price):表示计算购买标价为price元的住宅时,房屋中介需要收取的中介费和需要交纳的契税(中介费计算公式:房屋标价* RATIO,契税计算公式:房屋标价*TAX)
package house; public class Intermediary implements Business { Buyer buyer; Intermediary(Buyer buyer){ this.buyer=buyer; } @Override public void buying(double price) { // TODO Auto-generated method stub System.out.println(" "+buyer.name+"购买一套标价为"+price+"元的住宅,之后计算需要支付的中介费"+(price*Business.RATIO)+"和交纳的契税"+price*Business.TAX); } public void charing(double price) { // TODO Auto-generated method stub System.out.println("购买一套标价为"+price+"元的住宅,房屋中介需要收取的中介费"+(price*Business.RATIO)+"和交纳的契税"+price*Business.TAX); } }
4.Text
package house; import java.util.Scanner; public class Text { public static void main(String[] args) { Scanner reader=new Scanner(System.in); Buyer buyer=new Buyer("lisa"); Intermediary in=new Intermediary(buyer); double price=reader.nextDouble(); in.buying(price); } }
运行界面
题2:输入5个数,代表学生成绩,计算其平均成绩。当输入值为负数或大于100是通过自定义异常处理进行提示
1myException
//自定义异常 一个变量 一个构造方法 重写toString package Error; public class myException extends Exception{ private double a; myException(double a2){ this.a=a2; } public String toString(){ return "输入值为负数或大于100"; } }
2.error
package Error; import java.util.Scanner; public class error { static void makenum (double a) throws myException{ if(a<0||a>101) throw new myException(a); } public static void main(String[] args) { double num; double sum=0; Scanner reader=new Scanner(System.in); for(int i=0;i<5;i++){ try{ makenum(num=reader.nextDouble()); sum=sum+num; }catch (myException e) { System.out.println(" "+e); } } System.out.println(sum/5.0); } }
运行界面