Java 练习

1.题目:计算住宅产权房屋和商业产权房屋首付款。

 

2.代码

/**

接口Payment中写入一个计算房屋首付款的方法  downPay()方法;

定义Residence类实现接口Payment,Residence类中重写了计算房屋首付款的方法;

定义Commercial类(商业产权房屋类)实现接口Payment,Commercial类中重写了计算房屋首付款的方法。

主类Test中,创建住宅产权房屋对象,并完成住宅产权房屋首付款的计算,再将定义商业产权房屋对象传递给Payment接口对象,之后利用接口回调完成商业产权房屋首付款的计算。

*/

接口Payment

package ccut.cn;

public interface Payment {
	
	double downPay(double price,double area);
	
}

 Residence类

package ccut.cn;

public class Residence implements Payment {

    public double downPay(double price, double area) {
    
        return 0.2*price*area;
    }

}

Commercial类

package ccut.cn;

public class Commercial implements Payment {

    @Override
    public double downPay(double price, double area) {
        // TODO Auto-generated method stub
        return 0.45*price*area;
    }

}

主类Test

package ccut.cn;
import java.util.*;
public class Test {

    public static void main(String[] args) {
        Residence residence = new Residence();
        Scanner r =new Scanner(System.in);
        System.out.println("请输入住宅产权房屋的房屋每平方米的价格");
        double price=r.nextDouble();
        System.out.println("请输入住宅产权房屋的房屋的总平方数");
        double area=r.nextDouble();
        System.out.println("住宅产权房屋首付款为:"+residence.downPay(price, area));
        Payment payment=new Commercial();
        System.out.println("请输入商业产权房屋的房屋每平方米的价格");
        double price1=r.nextDouble();
        System.out.println("请输入商业产权房屋的房屋的总平方数");
        double area1=r.nextDouble();
        System.out.println("商业产权房屋首付款为:"+payment.downPay(price1, area1));
    }

}

 3.运行结果

Java 练习_第1张图片

 

 

 

 

 

 

 

你可能感兴趣的:(Java 练习)