定义抽象类Bank,它包含oneyearRate(一年定期利率)oneyearNationalbebt(一年国债利率)和CurrentDepositRate(按年计算的活期利率)三个利息率常数(分别为1.75%,2.0%,0.35%),本金Principal=1000, 设计 count()方法及show()方法。count()方法用于计算存款一年所的利息及缴利息税后的总金额(原存款额+税后利息),而show()方法用于显示总金额。
由于定期存款、活期存款和国债利率不同,要求定义3个类,分别表示这3种储蓄。3个类都继承抽象类Bank,实现Bank中的count()及show()抽象方法,完成对利息和总金额的计算和显示功能(本金1000元)。
注意:除国债外,定期存款和活期存款利息都要交纳20%的个人所得税。
源代码
abstract public class Bank {
double oneyearRate=0.0175; //一年定期利率
double oneyearNationalbebt=0.02; //一年国债利率
double CurrentDepositRate=0.0035; //按年计算的活期利率
double Principal=1000; //本金
abstract double count(); //计算存款一年所得利息及缴利息税后的总金额
abstract void show(); //显示总金额
}
public class CurrentDeposit extends Bank{
double count(){
double interest;
interest=(1-0.2)*CurrentDepositRate;
return Principal*(1+interest);
}
void show() {
System.out.println("定期存款后总金额:"+count());
}
}
public class Test {
static Bank[] deposit= {new TimeDeposit(),new CurrentDeposit(),new TreasuryRates()};
public static void main(String[] args) {
for(int i=0;i<deposit.length;i++) {
deposit[i].show();
}
}
}
public class TimeDeposit extends Bank{
double count(){
double interest;
interest=(1-0.2)*oneyearRate;
return Principal*(1+interest);
}
void show() {
System.out.println("定期存款后金额:"+count());
}
}
public class TreasuryRates extends Bank{
double count(){
return Principal*(1+oneyearNationalbebt);
}
void show() {
System.out.println("国债后金额:"+count());
}
}
运行结果
(2)将bank改为接口重新完成
public interface Bank {
double oneyearRate=0.0175; //一年定期利率
double oneyearNationalbebt=0.02; //一年国债利率
double CurrentDepositRate=0.0035; //按年计算的活期利率
double Principal=1000; //本金
double count(); //计算存款一年所得利息及缴利息税后的总金额
void show(); //显示总金额
}
public class CurrentDeposit implements Bank{
public double count(){
double interest;
interest=(1-0.2)*CurrentDepositRate;
return Principal*(1+interest);
}
public void show() {
System.out.println("定期存款后金额:"+count());
}
}
public class Test {
static Bank[] deposit= {new TimeDeposit(),new CurrentDeposit(),new TreasuryRates()};
public static void main(String[] args) {
for(int i=0;i<deposit.length;i++) {
deposit[i].show();
}
}
}
public class TimeDeposit implements Bank{
public double count(){
double interest;
interest=(1-0.2)*oneyearRate;
return Principal*(1+interest);
}
public void show() {
System.out.println("定期存款后金额:"+count());
}
}
public class TreasuryRates implements Bank{
public double count(){
return Principal*(1+oneyearNationalbebt);
}
public void show() {
System.out.println("国债后金额:"+count());
}
}