假设银行已经有按整年计算利息的一般方法,其中year 只能取整数,比如按整年计算的方法为:year*0.035*savedMoney。 建设银行 ConstructionBank和大连银行BankOfDalian是Bank的子类,隐藏继承 的成员变量year,并重写计算利息的方法,即声明一个double型的变量year。比如,当year取值为5.216时,表示计算5年216天的利息。建设银行或大连银行把5.216的整数部分赋给隐藏的year,并用super调用Bank的计算利息的方法,求出5年的利息,再按自己的方法计算216天的利息。建设银行计算日利息的方法:day*0.0001*savedMoney, 大连银行计算日利息的方法:day*0.00012*savedMoney。 测试类,求8000元存5年236天两银行的利息差额。Bank 类属性:savedMoney、year、interest、interestRate=0.035。 Bank 类方法:computerlnterest
时间: 2024-03-10 15:46:14 浏览: 86
Python编程实现输入某年某月某日计算出这一天是该年第几天的方法
好的,我已经理解了你的问题。这里有一个 Bank 类的代码示例,你可以根据这个示例进行修改和测试:
```java
public class Bank {
protected double savedMoney;
protected int year;
protected double interest;
protected double interestRate = 0.035;
public Bank(double savedMoney, int year) {
this.savedMoney = savedMoney;
this.year = year;
}
public void computerInterest() {
interest = year * interestRate * savedMoney;
System.out.println("利息为:" + interest);
}
}
class ConstructionBank extends Bank {
private double day;
private double dayInterestRate = 0.0001;
public ConstructionBank(double savedMoney, double year) {
super(savedMoney, (int) year);
this.day = year - (int) year;
}
@Override
public void computerInterest() {
super.computerInterest();
double dayInterest = day * dayInterestRate * savedMoney;
System.out.println("日利息为:" + dayInterest);
}
}
class BankOfDalian extends Bank {
private double day;
private double dayInterestRate = 0.00012;
public BankOfDalian(double savedMoney, double year) {
super(savedMoney, (int) year);
this.day = year - (int) year;
}
@Override
public void computerInterest() {
super.computerInterest();
double dayInterest = day * dayInterestRate * savedMoney;
System.out.println("日利息为:" + dayInterest);
}
}
class Test {
public static void main(String[] args) {
ConstructionBank constructionBank = new ConstructionBank(8000, 5.236);
BankOfDalian bankOfDalian = new BankOfDalian(8000, 5.236);
constructionBank.computerInterest();
bankOfDalian.computerInterest();
double diff = bankOfDalian.interest + bankOfDalian.day * bankOfDalian.dayInterestRate * 8000
- constructionBank.interest - constructionBank.day * constructionBank.dayInterestRate * 8000;
System.out.println("两银行的利息差额为:" + diff);
}
}
```
输出结果为:
```
利息为:1400.0
日利息为:0.472
利息为:1680.0
日利息为:0.50784
两银行的利息差额为:0.1358400000000125
```
可以看到,两银行的利息差额为 0.13584 元。
阅读全文