Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Create a static method in the SavingsAccount and CurrentAccount classes cal

ID: 3602377 • Letter: J

Question

JAVA

Create a static method in the SavingsAccount and CurrentAccount classes called displayNumberOfAccounts(). This method should show the number of accounts created of that particular sub class.

public class CurrentAccount extends Account {
   private double overdraftLimit;

   public CurrentAccount(int accountNumber, double overdraftLimit) {
       super(accountNumber);
       this.overdraftLimit = overdraftLimit;
   }

   public void withdraw(double amount) {
       if (amount > 0)
           if ((balance + overdraftLimit) - amount >= 0)
               balance -= amount;
           else
               System.err.println("Account.withdraw(...): " + "cannot withdraw more than your balance.");
       else
           System.err.println("Account.withdraw(...): " + "cannot withdraw negative amount.");
   }

}

public class SavingAccount extends Account {
   private double interestRate;

   public SavingAccount(int accountNumber, double interestRate) {
       super(accountNumber);
       this.interestRate = interestRate;
   }

   public void display() {
       super.display();
       System.out.println("Interest rate: " + interestRate);
   }
}

Explanation / Answer

SOLUTION :

public class CurrentAccount extends Account {
private double overdraftLimit;
static int numberOfAccouunts = 0;/*static variable it will be initialized once and retain it's value */
public CurrentAccount(int accountNumber, double overdraftLimit) {
super(accountNumber);
this.overdraftLimit = overdraftLimit;
numberOfAccouunts++; /* increment in static variable, when a object is created */
}
public void withdraw(double amount) {
if (amount > 0)
if ((balance + overdraftLimit) - amount >= 0)
balance -= amount;
else
System.err.println("Account.withdraw(...): " + "cannot withdraw more than your balance.");
else
System.err.println("Account.withdraw(...): " + "cannot withdraw negative amount.");
}
//displayNumberOfAccounts() method which shows total number of accounts created in CurrentAccount class
public static void displayNumberOfAccounts(){
System.out.println("Total number of accounts created in CurrentAccount class is : "+numberOfAccouunts);
}
}

public class SavingAccount extends Account {
private double interestRate;
static int numberOfAccouunts = 0; /*static variable it will be initialized once and retain it's value */
public SavingAccount(int accountNumber, double interestRate) {
super(accountNumber);
this.interestRate = interestRate;
numberOfAccouunts++; /* increment in static variable, when a object is created */
}
public void display() {
super.display();
System.out.println("Interest rate: " + interestRate);
}
//displayNumberOfAccounts() method which shows total number of accounts created in SavingAccount class
public static void displayNumberOfAccounts(){
System.out.println("Total number of accounts created in SavingAccount class is : "+numberOfAccouunts);
}
}