Java back feeling exercises

I, entitled

When the purchase of housing loans, down payment is a functional operation, defined in the Payment interface. Residential and Commercial Property Property Property Type Property Type has a function to pay the first payment. In accordance with the above, create an interface and two classes:

(1) Interface Payment, comprising:

  • double downPay (double price, double area): a method to calculate the first payment of housing, the price per square meter of housing on behalf of parameter price, area represents the total number of square houses, that area.

(2) Payment of non-use of abstract class interface --Residence class (class residential house property), includes:

  • double downPay (double price, double area): method of calculating the first payment of housing rewrite, and returns the result (the ratio of residential housing property down payment of 20% of the total housing fund).

(3) Payment of non-use of abstract class interface --Commercial class (Commercial Property Property Type), includes:

  • double downPay (double price, double area): method of calculating the first payment of housing rewrite, and returns the results (commercial property houses down payment is 45% of the total housing fund).

      Test the main class, create objects residential house property, complete the calculation of the first payment of residential property housing. Then the definition of commercial housing property object to the Payment interface object, then use a callback interface to complete the calculation of the first payment of commercial property housing. Test data input, the output of the completion.

Second, the source code

1.Payment.java

/ * 
 * Create an interface that declares a method downpay 
 * / 
Package COM;
 public  interface Payment {
     Double downPay ( Double . Price, Double Area); 
}

2.Residence.java

/ * 
 * Class to create a residential house property, a method override downpay 
 * / 
Package COM;
 public  class Residence the implements Payment {
     public  Double downPay ( Double . Price, Double Area) {
         return . Price * 0.2 * Area ; 
    } 
}

3.Commercial.java

/ * 
 * Create a commercial property class housing, downpay override methods 
 * / 
Package COM;
 public  class Commercial's the implements Payment {
     public  Double downPay ( Double . Price, Double Area) {
         return . Price * Area * 0.45 ; 
    } 
}

4.Test.java

/ * 
 * Create a test class, output, correction using the interface, the result output Commercial class 
 * / 
Package COM;
 Import java.util.Scanner;
 public  class the Test {
     / ** 
     * @param args
      * / 
    public  static  void main (String [ ] args) {
         // TODO Auto-Generated Method, Stub 
        Scanner sc = new new Scanner (System.in); 
        System.out.println ( "Please enter the price per square meter of housing:" );
         Double . price = sc.nextDouble (); 
        System.out.println ( "Please enter the total area of housing:" );
         Double area =sc.nextDouble (); 
        Residence r = new new Residence (); 
        System.out.println ( "down payment of residential housing property is:" + r.downPay (. price, Area)); 
        Payment the p- = new new Commercial's (); 
        System .out.println ( "the first payment of commercial housing property is:" + p.downPay (. price, Area)); 
    } 
}

Third, the operating results

 

Guess you like

Origin www.cnblogs.com/jingxueyan/p/11809154.html