Yes, it did compile when the following statement was placed in the Account class:
System.out.println("Account Owner2 Balance with interest:" +
owner[1].add_monthly_interest());
However, the output was "Account Owner2 balance with interest: 0"
The add_monthly_interest() method in SavingAccount didn't override the same method in Account class.
So then I moved the
System.out.println("Account Owner2 Balance with interest:" +
owner[1].add_monthly_interest());
statement down into the SavingsAccount class and get a bunch of errors: ; expected, ) expected, and illegal start of type and identifier expected.
They all reference that one statement.
Updated code:
class Account {
//Declare the variables
private String name;
protected double balance;
private double deposit;
private double withdraw;
protected double intrate;
//Declare getter and setter methods
public String getName() {
return name;
}
public double getDeposit() {
return deposit;
}
public double getWithraw() {
return withdraw;
}
public double getBalance() {
return balance;
}
public double getIntrate() {
return intrate;
}
public void setName(String n) {
name = n;
}
public void setDeposit(double d) {
deposit = d;
}
public void setWithdraw(double w) {
withdraw = w;
}
public void setBalance(double b) {
balance = b;
}
public void setIntrate(double i) {
intrate = i;
}
//Calculate the balance after the deposit.
public double BalAfterDeposit() {
double total = balance + deposit;
return total;
}
//Calculate the balance after the withdraw.
public double BalAfterWithdraw() {
double total = balance - withdraw;
return total;
}
public double add_monthly_interest() {
return 0;
}
}
class AccountTestDrive {
public static void main (String[] args) {
//Create an array that holds 2 new accounts
Account[] owner;
owner = new Account[2];
owner[0] = new Account();
owner[0].setName("Joe");
owner[0].setBalance(5000);
owner[0].setDeposit(7000);
owner[0].setWithdraw(1000);
System.out.println("Account Owner1 Name:" +
owner[0].getName());
System.out.println("Account Owner1 Current Balance:" +
owner[0].getBalance());
System.out.println("Account Owner1 Balance after Deposit:"
+ owner[0].BalAfterDeposit());
System.out.println("Account Owner1 Balance after Withdraw:"
+ owner[0].BalAfterWithdraw());
owner[1] = new Account();
owner[1].setName("Mary");
owner[1].setBalance(3000);
owner[1].setDeposit(4000);
owner[1].setWithdraw(2000);
System.out.println("Account Owner2 Name:" +
owner[1].getName());
System.out.println("Account Owner2 Current Balance:" +
owner[1].getBalance());
System.out.println("Account Owner2 Balance after Deposit:"
+ owner[1].BalAfterDeposit());
System.out.println("Account Owner2 Balance after Withdraw:"
+ owner[1].BalAfterWithdraw());
}
}
class SavingsAccount extends Account {
public double add_monthly_interest() {
double intrate = 0.05;
double total = balance + (balance * intrate)/12;
return total;
}
System.out.println("Account Owner2 Balance with interest:" +
owner[1].add_monthly_interest());
}