2021-10-28-Java学习笔记

该程序计算储蓄账户和支票账户的利息收入。对于储蓄账户,余额增加20%,而对于支票账户,余额增加5%。已给出Account类以及 SavingAcc 和 CheckingAcc 类作为继承基础,需要在子类中重写getIncome()方法以返回正确的余额加利息收入。输入示例为两个账户的初始余额,输出分别为两个账户的最终余额。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are writing a program that calculates interest income based on account type. If it is a saving account, 20% is added to the balance in the account. If it is a checking account, 5% more is added to the balance in the account.
The program you are given should the balance of the savings and checking accounts and output the appropriate balance plus interest income
Account class and SavingAcc & CheckingAcc classes inherited from it are already defined.
Override getIncome() methods in the inherited classes to calculate and return the account balances, so that the given outputs work correctly.

Sample Input
100.0
100.0

Sample Output
120.0
105.0

/*
Write a program that calculates the income based on the type of the account.
***************************************************************************
Overriding


You are writing a program that calculates interest income based on account type. If it is a saving account, 20% is added to the balance in the account. If it is a checking account, 5% more is added to the balance in the account.
The program you are given should the balance of the savings and checking accounts and output the appropriate balance plus interest income
Account class and SavingAcc & CheckingAcc classes inherited from it are already defined.
Override getIncome() methods in the inherited classes to calculate and return the account balances, so that the given outputs work correctly.
*/



import java.util.Scanner;
class Main {
   public static void main(String[] args) {
       Scanner read = new Scanner(System.in);
       double saving = read.nextDouble();
       double checking = read.nextDouble();

       Account savingAcc = new SavingAcc(saving);
       Account checkingAcc = new CheckingAcc(checking);

       System.out.println(savingAcc.getIncome());
       System.out.println(checkingAcc.getIncome());
   }
}
class Account {

   protected double amount;

   public Account(double amount) {
       this.amount = amount;
   }

   public double getAmount() {
       return amount;
   }

   public double getIncome() {
       return 0;
   }
}

class SavingAcc extends Account {

   public SavingAcc(double amount) {
       super(amount);
   }
   //Override the method for saving account
   public double getIncome() {
       amount=amount+amount*0.2;
       return amount;
   }
}
class CheckingAcc extends Account {

   public CheckingAcc(double amount) {
       super(amount);
   }
   //Override the method for checking account
   public double getIncome() {
   amount=amount+amount*0.05;
       return amount;
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萱帧闯江湖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值