Create and abstract class named account for a bank. Include an integer field for
ID: 3544851 • Letter: C
Question
Create and abstract class named account for a bank. Include an integer field for the account number and a double field for the account balance. Also include a constructor that requires an account number and that sets the balance to 0.0. Include a set method for the balance. Also include two abstract get methods--one for each field. Create two child classes of Account: Checking and Savings. Within the Checking class, the get method displays the String "Checking Account Information", the account number, and the balance. Within the Savings class, add a field to hold the interest rate and require the Savings constructor to accept an argument for the value of the interest rate. The Savings get method displays the String "Savings Account Information", the account number, the balance, and the interest rate.
Write an application that demonstrates you can instantiate and display both Checking and Savings objects. Save your files as Account.java, Checking.java, Savings.java, and DemoAccounts.java.
Explanation / Answer
public abstract class Account
{
int accountNumber;
double accountBalance;
public Account(int an)
{
accountNumber = an;
accountBalance = 0.0;
}
public void setBalance(double bal)
{
accountBalance = bal;
}
public abstract int getNumber();
public abstract double getBalance();
public abstract void getInfo();
}
public class Checking extends Account
{
public Checking(int a)
{
super(a);
}
public void getInfo()
{
System.out.println("Checking Account Information " +
getNumber() + " $" + getBalance());
}
public int getNumber()
{
return accountNumber;
}
public double getBalance()
{
return accountBalance;
}
}
public class Savings extends Account
{
double rate;
public Savings(int a, double r)
{
super(a);
rate = r;
}
public void getInfo()
{
System.out.println("Savings Account Information " +
getNumber() + " $" +getBalance() + " rate " + rate);
}
public int getNumber()
{
return accountNumber;
}
public double getBalance()
{
return accountBalance;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.