我是java的新手,我试图找出如何修改构造函数来创建BankAccount对象,当用户输入格式的年利率时,该对象存储每月利息?我无能为力,有什么想法吗?另外,BankAccount构造函数最初存储了BankAccount对象的实例字段的月利率,但年费率需要转换为月费率。那么如何切换它呢?
/**
* BankAccount class
* This class simulates a bank account.
*
* (Taken from "Starting Out with Java - Early Objects
* (Third Edition) by Tony Gaddis, 2008 by Pearson Educ.)
*
*/
public class BankAccount {
public static void main(String[] args) {
}
private double balance; // Account balance
private double interestRate; // Interest rate
private double interest; // Interest earned
/**
* The constructor initializes the balance
* and interestRate fields with the values
* passed to startBalance and intRate. The
* interest field is assigned to 0.0.
*/
public BankAccount(double startBalance, double intRate)
{
balance = startBalance;
interestRate = intRate;
interest = 0.0;
}
/**
* The deposit method adds the parameter
* amount to the balance field.
*/
public void deposit(double amount)
{
balance += amount;
}
/**
* The withdraw method subtracts the
* parameter amount from the balance
* field.
*/
public void withdraw(double amount)
{
balance -= amount;
}
/**
* The addInterest method adds the interest
* for the month to the balance field.
*/
public void addInterest()
{
interest = balance * interestRate;
balance += interest;
}
/**
* The getBalance method returns the
* value in the balance field.
*/
public double getBalance()
{
return balance;
}
/**
* The getInterest method returns the
* value in the interest field.
*/
public double getInterest()
{
return interest;
}
}
答案 0 :(得分:0)
您可以在java中创建任意数量的构造函数,方法名称必须是类名
public BankAccount(double startBalance, double intRate,float interest )
{
balance = startBalance;
interestRate = intRate;
interest = 0.0;
}
答案 1 :(得分:0)
//以这种方式修改BankAccount构造函数。
public BankAccount(double startBalance, double intRate)
{
balance = startBalance;
interestRate = (intRate)/(12*100);
interest = 0.0;
}