用于演示抽象 BankAccount 类和 SavingsAccount 子类的 Java 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22178959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java program to demonstrate abstract BankAccount class and SavingsAccount subclass
提问by codenewb
This week I was tasked with writing an abstract BankAccount class and a SavingsAccount class which extends BankAccount. I have written out the code as the assignment asks and it seems to compile perfectly. I now must write a driver to test the two classes and here is where I am stuck.. Just to be clear I'm not asking anyone to write it for me, I want to eventually be able to do this all on my own. I'm just asking for a little guidance. (I've scheduled one on one time with my instructor and he has cancelled twice)
本周我的任务是编写一个抽象的 BankAccount 类和一个扩展 BankAccount 的 SavingsAccount 类。我已经按照作业要求写出了代码,它似乎可以完美编译。我现在必须编写一个驱动程序来测试这两个类,这就是我被卡住的地方.. 明确地说,我不是要求任何人为我编写它,我希望最终能够自己完成这一切。我只是要求一些指导。(我和我的教练安排了一对一,他已经取消了两次)
I basically am wondering how to write the driver class for these two classes. Are my classes missing anything in terms of fields or methods? How do you seasoned programmers plan out this kind of stuff?
我基本上想知道如何为这两个类编写驱动程序类。我的课程在字段或方法方面是否缺少任何内容?经验丰富的程序员如何计划这种东西?
Any suggestions you may have would be appreciated!
您的任何建议将不胜感激!
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;
}
}
and the subclass
和子类
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
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++;
}
}
采纳答案by Roberto
A driver or runner class is usually a class with a main method in which you can run code. Basically...
驱动程序或运行程序类通常是具有可在其中运行代码的 main 方法的类。基本上...
public class TestDriver {
public static void main(String[] args) {
// Run your code here...
}
}
What you probably need to do is create a few SavingsAccount objects inside of it, and show that the methods it implements work.
您可能需要做的是在其中创建一些 SavingsAccount 对象,并证明它实现的方法有效。
If this is a school assignment, you may need to get more specific details from your instructor if you are not understanding the requirements.
如果这是学校作业,如果您不了解要求,您可能需要从您的教师那里获得更具体的详细信息。