Ten jobs

Topic 1:

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

Service Interface

package zhong;

public interface business {
       double ra = 0.022;
       double T = 0.03;
    
       void buying(double p);
    }

Class home buyers

Package Zhong; 

public class Buyer the implements Business { 
        String name; 
     
     public Buyer (String name) { 
           this.name = name;  } 
     public void Buying (Double P) { 
     System.out.println ( "price for a later:" + p );}}

Intermediary class

package zhong;

public class intermediary implements business {
    
      buyer buyer;
    
         public intermediary(buyer buyer) {
super(); this.buyer = buyer; } public void buying(double p) { buyer.buying(p); this.charing(p); } public void charing(double p) { System.out.println("中介费为:" + p * ra); System.out.println("契税:" + p * T); } }

Test category

package zhong;
public class Test {
    public static void main(String[] args) {
        buyer buyer = new buyer("Lisa");
         intermediary intermediary = new intermediary(buyer);
        intermediary.buying(650000); } }

Test Results

Topic two

Enter the number 5, on behalf of student achievement, we calculate the average. When the input value is negative or greater than 100, custom exception handling prompt.

Custom exception class

package yichang;

public class MyException extends Exception {
     
        double n;
    
      public MyException(double a) {
            n = a;
       }
    
       public String toString() { return "自定义["+n+"不在0到100中]"; } }

Test category

package yichang; 
import java.util.Scanner;

public class tys {
     static void avg()throws MyException {
        double a;
        double sum = 0; Scanner s = new Scanner(System.in); System.out.println("5名成绩"); try { for (int i = 0; i < 5; i++) { a = s.nextDouble(); if (a> 100 || a < 0) throw new MyException(a); sum += a; } System.out.println( sum / 5); } catch (MyException e) { System.out.println("捕获"+e); } } }

result

Guess you like

Origin www.cnblogs.com/shuang123/p/11852034.html