演示抽象BankAccount类和SavingsAccount子类的Java程序
本周我的任务是编写一个抽象的BankAccount类和一个扩展BankAccount的SavingsAccount类。我已经按照作业要求写出了代码,它似乎编译得非常完美。我现在必须编写一个驱动程序来测试这两个类,这就是我遇到的问题。。我只是想说清楚,我不是要求任何人为我写这篇文章,我希望最终能够自己完成这一切。我只是想得到一点指导。(我已经和我的导师安排了一对一的时间,他已经取消了两次) 我基本上想知道如何为这两个类编写驱动程序类。 我的类在字段或方法方面是否缺少任何内容?你们这些经验丰富的程序员是如何计划这类事情的 如果您有任何建议,我们将不胜感激演示抽象BankAccount类和SavingsAccount子类的Java程序,java,Java,本周我的任务是编写一个抽象的BankAccount类和一个扩展BankAccount的SavingsAccount类。我已经按照作业要求写出了代码,它似乎编译得非常完美。我现在必须编写一个驱动程序来测试这两个类,这就是我遇到的问题。。我只是想说清楚,我不是要求任何人为我写这篇文章,我希望最终能够自己完成这一切。我只是想得到一点指导。(我已经和我的导师安排了一对一的时间,他已经取消了两次) 我基本上想知道如何为这两个类编写驱动程序类。 我的类在字段或方法方面是否缺少任何内容?你们这些经验丰富的程序
public abstract class BankAccount
{
double balance;
int numOfDeposits;
int numOfWithdraws;
double interestRate;
double annualInterest;
double monSCharges;
double amount;
double monInterest;
//constructor accepts arguments for balance and annual interest rate
public BankAccount(double bal, double intrRate)
{
balance = bal;
annualInterest = intrRate;
}
//sets amount
public void setAmount(double myAmount)
{
amount = myAmount;
}
//method to add to balance and increment number of deposits
public void deposit(double amountIn)
{
balance = balance + amountIn;
numOfDeposits++;
}
//method to negate from balance and increment number of withdrawals
public void withdraw(double amount)
{
balance = balance - amount;
numOfWithdraws++;
}
//updates balance by calculating monthly interest earned
public double calcInterest()
{
double monRate;
monRate= interestRate / 12;
monInterest = balance * monRate;
balance = balance + monInterest;
return balance;
}
//subtracts services charges calls calcInterest method sets number of withdrawals and deposits
//and service charges to 0
public void monthlyProcess()
{
calcInterest();
numOfWithdraws = 0;
numOfDeposits = 0;
monSCharges = 0;
}
//returns balance
public double getBalance()
{
return balance;
}
//returns deposits
public double getDeposits()
{
return numOfDeposits;
}
//returns withdrawals
public double getWithdraws()
{
return numOfWithdraws;
}
}
以及子类
public class SavingsAccount extends BankAccount
{
//sends balance and interest rate to BankAccount constructor
public SavingsAccount(double b, double i)
{
super(b, i);
}
//determines if account is active or inactive based on a min acount balance of $25
public boolean isActive()
{
if (balance >= 25)
return true;
return false;
}
//checks if account is active, if it is it uses the superclass version of the method
public void withdraw()
{
if(isActive() == true)
{
super.withdraw(amount);
}
}
//checks if account is active, if it is it uses the superclass version of deposit method
public void deposit()
{
if(isActive() == true)
{
super.deposit(amount);
}
}
//checks number of withdrawals adds service charge
public void monthlyProcess()
{
if(numOfWithdraws > 4)
monSCharges++;
}
}
驱动程序或运行程序类通常是一个具有主方法的类,您可以在其中运行代码。基本上
public class TestDriver {
public static void main(String[] args) {
// Run your code here...
}
}
您可能需要做的是在其中创建一些SavingsAccount对象,并显示它实现的方法是有效的
如果这是一项学校作业,如果您不了解要求,您可能需要从您的讲师那里获得更具体的细节。问题是什么??你想让我们验证代码吗?那么你想知道如何为此权限编写单元测试吗?在应该重写超类方法的方法上添加@Override注释。这将帮助您发现类中的两个bug。而且你永远不应该像现在这样默默地做任何事情:如果账户不活跃,有人试图存款或取款,就会抛出一个异常。请发布你的
驱动程序。我猜它正在尝试实例化抽象类。是的,我基本上想知道如何为这些类编写驱动程序。我只是不知道从哪里开始。谢谢你的反馈!到目前为止,我对如何从驱动程序类中获得正确方法的数量感到更困惑。到目前为止,我有一个程序,提示用户选择存款、取款等。然后我有一个switch/case语句,可以根据用户输入的内容执行操作。例如,如果他们选择存款,它会询问存款金额。首先,假设用户输入存款金额后,SavingsAccount对象获取该数据,然后您可以调用其getDeposits()方法并为用户打印出来。一般来说每次用户对其SavingsAccount执行某些操作时,您都会将其打印出来,以便用户查看其交易的结果。我只是想添加我尝试在SavingsAccount类中创建一个setAmount方法,并将输入的金额从driver类发送到SavingsAccount类中的setAmount方法,但我一直收到一个错误关于静态和非静态方法引用,您可能会直接在main(…)内调用SavingsAccounts方法。这是不允许的。您需要在main(…)中创建一个SavingsAccounts对象,然后从该对象调用方法。