Java 10th job

Topic 1:

One,:

Write an application to simulate intermediaries and buyers to complete the home buying process.

There are three classes and one interface:

  • Business-- service interface
  • Buyer - class buyers
  • Intermediary-- intermediary class
  • Test - the main class

    1. Service Interface

    Service interfaces include:

    (1) two data fields (member variables)

    RATIO: double type, intermediary fees charged by estate agents on behalf of the proportion of the price of houses, the initial value of 0.022

    TAX: double type, buyers need to pay the deed on behalf of the proportion of the cost price of the house, the initial value of 0.03

    (2) a method

    void buying (double price): price represents housing price

    2. Class home buyers

    Buyers Buyer class is non-abstract service interfaces Business use categories, including:

    (1) a member variable

    name: String type, represents buyers' names

    (2) a method:

    public void buying (double price): display output price to buy a house for the price of $

    3. Intermediary class

    Intermediary intermediary class is non-abstract service interfaces Business use categories, including:

    • A member variable

    buyer: Buyer type, housing agency received on behalf of buyers objects

    • Three methods

    Intermediary (Buyer buyer): Constructor

    public void buying (double price): buyers buyer to purchase a house priced at price yuan, after calculating the intermediary fees to be paid and to pay the deed tax

    public void charing (double price): represents the calculation of the purchase price for the price of $ residential, intermediary fees estate agents need to charge and required to pay the deed tax (agency fee calculation formula: Housing price * RATIO, deed tax calculation formula: Housing price * TAX )

    4. Test class

    Purchase objects defined in the Test class - the names Lisa, from the console input price she plans to buy a house, such as 650,000 yuan. You through the interface and class definitions above, the realization of her through an intermediary to buy a house, show the need to pay intermediary fees and deed.

Second, the source code:

1、Business.java

package Haha;

public interface Business {//创建接口Business
    double     RATIO=0.022;
    double TAX=0.03;
    void buying (double price);
 
}

2、Buyer.java

package Haha;

public class Buyer implements Business{//创建使用接口的Buyer类
    String name;
    public void buying(double price){
    System.out.println("房屋的价格:"+price);//输出房屋价格
    }
    
}

3、Intermediary.java

package Haha;

public class Intermediary implements Business {//创建使用接口的Intermediary类
    Buyer buyer;
    Intermediary(Buyer buyer){
        this.buyer=buyer;
    }
    public void buying (double price){
        System.out.println(buyer.name+"购买的房屋价格是:"+price);//输出购买者名字和房屋的价格
    }
    public void charing(double price){
        System.out.println("房屋中介所需中介费"+price*RATIO);//输出房屋中介费
        System.out.println("房屋中介所需缴税"+price*TAX);//输出房屋中介所需缴税
    }
}

4、Test.java

package Haha;

import java.util.Scanner;
public class Test{//Test主类
    public static void main(String[] args){//主方法
        Scanner reader=new Scanner(System.in);//创建Buyer对象
        Buyer buyer=new Buyer();
        Intermediary i=new Intermediary(buyer);
        double price=reader.nextDouble();
        buyer.buying(price);
        buyer.name = "lisa";
        System.out.println("姓名:" + buyer.name);
        i.buying(price);
        i.charing(price);
    }
}

三、运行结果:

 

 

 题目2:

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

二、源程序:

Test.java

package Yichang;
import java.util.Scanner;
 
  class MyException extends Exception{
       private double exceptnumber;
       MyException(double a){
           exceptnumber=a;
       }
       public String toString(){
           return "自定义异常["+exceptnumber+"]";//自定义异常的描述信息
       }
   }
  class Test {
       static void makeExcept(double a)throws MyException{
               if(a<0||a>100){
                   throw new MyException(a);//抛出自定义异常MyException
               }
       }
       public static void main(String[] args) {
           try{
               double  sum=0;
               double  a;
               Scanner reader=new Scanner(System.in);
               System.out.println("请输入5个数:");
               for(int i=0;i<5;i++){
                   a=reader.nextDouble();
                   sum+=a;
               makeExcept(a);
               }
              System.out.println("平均数:"+sum/5.0);
           }catch(MyException e){       //捕获自定义异常MyException
               System.out.println("捕获"+e);
           }
       }
 
   }

三、运行结果:

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/szy799/p/11852899.html