Im messing around with inheritance and confused on how to do

I\'m messing around with inheritance and confused on how to do a few different things. Here is what I have:

Looking at the Account class and write a main method in a different class Bank to brief experiment with some instances of the Account class.

1. Using the Account class as a base class, write two derived classes called SavingsAccount and CheckingAccount. A SavingsAccount object, in addition to the attributes of an Account object, should have an interest variable and a method which adds interest to the account. A CheckingAccount object, in addition to the instance variables of an Account object, should have an overdraft limit variable. Ensure that you have overridden methods of the Account class as necessary in both derived classes.

2. Now create a Bank class, an object of which contains an array of Account objects. Accounts in the array could be instances of the Account class, the SavingsAccount class, or the CheckingAccount class. Create some test accounts (some of each type).

3. Write an update method in the bank class. It iterates through each account, updating it in the following ways: Savings accounts get interest added (via the method you already wrote); Checking Account get a letter sent if they are in overdraft.

savingsaccount:

checkingaccount:

bank class:

Please help me correct the errors i made. The codes won\'t run for me.

I\'m messing around with inheritance and confused on how to do a few different things. Here is what I have:

Looking at the Account class and write a main method in a different class Bank to brief experiment with some instances of the Account class.

1. Using the Account class as a base class, write two derived classes called SavingsAccount and CheckingAccount. A SavingsAccount object, in addition to the attributes of an Account object, should have an interest variable and a method which adds interest to the account. A CheckingAccount object, in addition to the instance variables of an Account object, should have an overdraft limit variable. Ensure that you have overridden methods of the Account class as necessary in both derived classes.

2. Now create a Bank class, an object of which contains an array of Account objects. Accounts in the array could be instances of the Account class, the SavingsAccount class, or the CheckingAccount class. Create some test accounts (some of each type).

3. Write an update method in the bank class. It iterates through each account, updating it in the following ways: Savings accounts get interest added (via the method you already wrote); Checking Account get a letter sent if they are in overdraft.

Solution

Account.java

public class Account {

private double balance;
private int acctNum;

public Account (int num)
{
balance = 0.0;
acctNum = num;
}
public void deposit (double amt)
{
if (amt >0)
balance +=amt;
else
System.out.println(\"Account.deposit(...): \"
+\"cannot deposit negative amount.\");
}
public void withdraw (double amt)
{
if (amt>0)
balance -=amt;
else
System.err.println(\"Account.withdraw(...): \"
+\"cannot withdraw negative amount.\");

}
public double getBalance()
{
return balance;
}
public double getAccountNumber()
{
return acctNum;
}
public String toString()
{
return \"Acc \" + acctNum + \": \" + \"balance = \"+ balance;   
}
public final void print()
{
System.out.println( toString());
}

}

______________________________

SavingsAccount.java

public class SavingsAccount extends Account {
private double interest;

public SavingsAccount(int acctNum, double interest) {
super(acctNum);
this.interest=interest;
}

public double getInterest() {
double x= getBalance() + getBalance()*interest;
return x;

// public void setInterest((interest))
// this.interest=interest;
}
public void AddInterest (double interest) {
double x = super.getBalance() * interest;
super.deposit(x);
}
public String toString() {
return super.toString()+\" Interest : \" + interest;
}
}

_________________________________

CheckingAccount.java
public class CheckingAccount extends Account {
private double limit;

public CheckingAccount(int acctNum, double limit) {
super(acctNum);
this.limit=limit;
}

public double getLimit() {
return this.limit;
}

public void setLimit(double limit) {
this.limit=limit;
}
public void withdraw (double limit) {
if (limit <= this.limit)
super.withdraw(limit);
else {
System.out.println(\" Sorry, Limit Exceeded\" );
}

}


public String toString() {
return super.toString() +\"Limit : \"+limit;
}
}

________________________________

Bank.java

public class Bank {

public static void main(String[] args) {
Account[] accounts ={new SavingsAccount(2, 0.25),new CheckingAccount(23, 50)};
for(int i=0; i<accounts.length;i++) {
accounts[i].deposit(50000);
  
}
SavingsAccount s=(SavingsAccount)accounts[0];

s.AddInterest(0.025);
  
System.out.println(\"Interest :\"+s.getInterest());
CheckingAccount ca=(CheckingAccount)accounts[1];
  
System.out.println(\"____ Setting the limit 20000____\");
ca.setLimit(20000);
  
System.out.println(\"____ Trying to Withdrawn amount 21000____\");
ca.withdraw(21000);
  
System.out.println(\"____ Trying to Withdrawn amount 19000____\");
ca.withdraw(19000);
System.out.println(\"Balance after withdrawn amount:\"+ca.getBalance());
}
}

__________________________________

Output:

Interest :64062.5
____ Setting the limit 20000____
____ Trying to Withdrawn amount 21000____
Sorry, Limit Exceeded
____ Trying to Withdrawn amount 19000____
Balance after withdrawn amount:31000.0

________Thank You

I\'m messing around with inheritance and confused on how to do a few different things. Here is what I have: Looking at the Account class and write a main method
I\'m messing around with inheritance and confused on how to do a few different things. Here is what I have: Looking at the Account class and write a main method
I\'m messing around with inheritance and confused on how to do a few different things. Here is what I have: Looking at the Account class and write a main method
I\'m messing around with inheritance and confused on how to do a few different things. Here is what I have: Looking at the Account class and write a main method

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site