第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元。请你通过上面定义的接口和类,实现她通过中介买房的过程,显示需交纳的中介费和契税。
接口

package ccut.java;

public interface Business {
    double ratio = 0.022;
    double tax = 0.03;
  void buying(double price);
}

购房者类

package ccut.java;

public class Buyer implements Business {
    String name;

    public void buying(double price) {
        System.out.println(name + "购买的住宅标价为:" + price);
    }
}

中介类

package ccut.java;
public class Intermediary implements Business{
 Buyer buyer;
 Intermediary(Buyer buyer){
  this.buyer = buyer;
 }
 public void buying(double price) {
 }
 public void charing(double price) {
  System.out.println(buyer.name+"住宅需要的中介费用"+price*ratio);
  System.out.println(buyer.name+"住宅需要交纳的契税"+price*tax);
 }
}

主类

package ccut.java;
import ccut.java.Intermediary;

import java.util.*;

public class Text {
    public static void main(String[] args) {

        Buyer buyer = new Buyer();
        buyer.name ="Lisa";
        Scanner in1=new Scanner(System.in);
        System.out.println("请输入房屋的价格:");
        int price =in1.nextInt();
        buyer.buying(price);
        Intermediary intermediary = new Intermediary(buyer);
        intermediary.charing(price);
    }
}


题目2:

输入5个数,代表学生成绩,计算其平均成绩。当输入值为负数或大于100时,通过自定义异常处理进行提示。

package com.ccut;

/**
 * @author Ruayo
 * @project arithmetic-generator
 * @package com.ccut
 * @date 2019/11/13 16:57
 */
public class MyException extends Exception {
    private double excpt;
    MyException(double score){
    excpt = score;
    }

    public String toString() {
        return "异常"+excpt;
    }
}
package com.ccut;

import java.util.Scanner;

/**
 * @author Ruayo
 * @project arithmetic-generator
 * @package com.ccut
 * @date 2019/11/13 16:53
 */
public class Text {
    static void makeExcep(double score) throws MyException {
        if (score < 0 || score > 99) throw new MyException(score);
    }
    public static void main(String[] args) {
        double score;
        double sum=0;
        Scanner in1 = new Scanner(System.in);
        try {
            for (int i = 1; i <= 5; i++) {
                score = in1.nextDouble();
                makeExcep(score);
                sum += score;
            }
            System.out.println(sum / 5);
        } catch (MyException e) {
            System.out.println("捕获" + e);
        }
    }
}

原文地址:https://www.cnblogs.com/papapa613/p/11885996.html