JAVA Make the Account class as an abstract class. Define an abstract method prin
ID: 3602372 • Letter: J
Question
JAVA
Make the Account class as an abstract class. Define an abstract method printAccountInfo() which should be implemented by the sub classes. This method should display the type of account along with the account details.
public class Account {
protected int accountNumber;
protected double balance;
// Constructor
public Account(int a) {
balance = 0.0;
accountNumber = a;
}
public void deposit(double amount) {
if (amount > 0)
balance += amount;
else
System.err.println("Account.deposit(...): " + "cannot deposit negative amount.");
}
public void withdraw(double amount) {
if (amount > 0)
if (balance - 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 double getbalance() {
return balance;
}
public double getAccountNumber() {
return accountNumber;
}
public void display() {
System.out.println("Account " + accountNumber + " : " + "Balance = " + balance);
}
}
Explanation / Answer
Account.java
public abstract class Account {
protected int accountNumber;
protected double balance;
// Constructor
public Account(int a) {
balance = 0.0;
accountNumber = a;
}
public void deposit(double amount) {
if (amount > 0)
balance += amount;
else
System.err.println("Account.deposit(...): " + "cannot deposit negative amount.");
}
public void withdraw(double amount) {
if (amount > 0)
if (balance - 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 double getbalance() {
return balance;
}
public double getAccountNumber() {
return accountNumber;
}
public void display() {
System.out.println("Account " + accountNumber + " : " + "Balance = " + balance);
}
public abstract void printAccountInfo();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.