zoukankan      html  css  js  c++  java
  • 找回感觉的练习

    题目:

     贷款购买房屋时,支付首付款是一种功能操作,定义在Payment接口中。住宅产权房屋类和商业产权房屋类均具有支付首付款的功能。依据以上内容,创建一个接口和两个类:

    (1)接口Payment,包含:

    • double downPay(double price,double area):一个计算房屋首付款的方法,参数price代表房屋每平方米的价格,area代表房屋的总平方数,即面积。

    (2) Payment接口的非抽象使用类——Residence类(住宅产权房屋类),包含:

    • double downPay(double price,double area):重写计算房屋首付款的方法,并将结果返回(住宅产权房屋的首付比例为总房款的20%)。

    (3) Payment接口的非抽象使用类——Commercial类(商业产权房屋类),包含:

    • double downPay(double price,double area):重写计算房屋首付款的方法,并将结果返回(商业产权房屋的首付比例为总房款的45%)。

          在主类Test中,创建住宅产权房屋对象,完成住宅产权房屋首付款的计算。再将定义商业产权房屋对象传递给Payment接口对象,之后利用接口回调完成商业产权房屋首付款的计算。输入测试数据,完成结果的输出。

    1.接口类

    1 package lianxi2;
    2 
    3 public interface Payment {                      //接口类
    4     double downPay(double price,double area);   //计算房屋首付款的方法
    5 }

    2.住宅产权房屋类

    1 package lianxi2;
    2 
    3 public class Residence implements Payment {
    4     public double downPay(double price,double area){  //方法重写
    5         return price*area*0.2;
    6     }
    7 }

    3.商业产权房屋类

    1 package lianxi2;
    2 
    3 public class Commercial implements Payment {
    4     public double downPay(double price1,double area1){   //方法重写
    5         return price1*area1*0.45;
    6     }
    7 }

    4.主类

     1 package lianxi2;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Test {
     6 
     7     public static void main(String[] args) {
     8         Residence residence = new Residence();             //创建Residence的对象
     9         Scanner reader =new Scanner(System.in);
    10         System.out.println("请输入住宅产权房屋每平米价格和房屋面积!");
    11         double price =reader.nextDouble();
    12         double area  = reader.nextDouble();
    13          System.out.println("住宅产权房屋首付款为:"+residence.downPay(price, area));
    14          Payment payment =new Commercial();                 //接口回调
    15          System.out.println("请输入商业产权房屋每平米价格和房屋面积!");
    16             double price1=reader.nextDouble();
    17             double area1=reader.nextDouble();
    18             System.out.println("商业产权房屋首付款为:"+payment.downPay(price1, area1));
    19         }
    20         
    21     }

    运行结果

  • 相关阅读:
    leetcode Remove Linked List Elements
    leetcode Word Pattern
    leetcode Isomorphic Strings
    leetcode Valid Parentheses
    leetcode Remove Nth Node From End of List
    leetcode Contains Duplicate II
    leetcode Rectangle Area
    leetcode Length of Last Word
    leetcode Valid Sudoku
    leetcode Reverse Bits
  • 原文地址:https://www.cnblogs.com/changheng/p/11808371.html
Copyright © 2011-2022 走看看