For the following code: class Account { private String name; private long amount
ID: 3813345 • Letter: F
Question
For the following code:
class Account
{
private String name;
private long amount;
Account(String name, long amount)
{
this.name = name;
setAmount(amount);
}
void deposit(long amount)
{
this.amount += amount;
}
String getName()
{
return name;
}
long getAmount()
{
return amount;
}
void setAmount(long amount)
{
this.amount = amount;
}
}
class SavingsAccount extends Account
{
SavingsAccount(long amount)
{
super("savings", amount);
}
}
class CheckingAccount extends Account
{
CheckingAccount(long amount)
{
super("checking", amount);
}
void withdraw(long amount)
{
setAmount(getAmount() - amount);
}
}
//add certificate of Deposit subclass
class CertificateofDeposit extends Account
{
CertificateofDeposit(long amount)
{
super("Certificate of Deposit", amount);
}
}
//add mortgage subclass of Account
class Mortgage extends Account
{
Mortgage(long amount)
{
super("Mortgage", amount);
}
void withdraw(long amount)
{
setAmount(getAmount() - amount);
}
}
//class to execute main
class AccountDemo
{
public static void main(String[] args)
{
//info on saving account
SavingsAccount sa = new SavingsAccount(10000);
System.out.println("account name: " + sa.getName());
System.out.println("initial amount: " + sa.getAmount());
sa.deposit(5000);
System.out.println("new amount after deposit: " + sa.getAmount());
System.out.println("");
//info on checking account
CheckingAccount ca = new CheckingAccount(20000);
System.out.println("account name: " + ca.getName());
System.out.println("initial amount: " + ca.getAmount());
ca.deposit(6000);
System.out.println("new amount after deposit: " + ca.getAmount());
ca.withdraw(3000);
System.out.println("new amount after withdrawal: " + ca.getAmount());
System.out.println("");
//add certificate of deposit
CertificateofDeposit cd = new CertificateofDeposit(150000);
System.out.println("account name: " + cd.getName());
System.out.println("initial amount: " + cd.getAmount());
cd.deposit(50000);
System.out.println("new amount after renewal: " + cd.getAmount());
System.out.println("");
//add mortgage information
Mortgage mt = new Mortgage(250000);
System.out.println("account name: " + mt.getName());
System.out.println("initial amount: " + mt.getAmount());
mt.withdraw(50000);
System.out.println("new amount after renewal: " + cd.getAmount());
System.out.println("");
}
}
Make the CertificateOfDeposit class a subclass of the SavingsAccount class. [Hint: you might need to overload the constructor of the SavingsAccount class].
Explanation / Answer
Note :As You mentioned Just One change I made it.Please check it.If need further modifications I will Do it.
____________
Account.java
public class Account {
private String name;
private long amount;
Account(String name, long amount) {
this.name = name;
setAmount(amount);
}
void deposit(long amount) {
this.amount += amount;
}
String getName() {
return name;
}
long getAmount() {
return amount;
}
void setAmount(long amount) {
this.amount = amount;
}
}
__________________
SavingsAccount.java
public class SavingsAccount extends Account {
SavingsAccount(long amount) {
super("savings", amount);
}
}
__________________
CheckingAccount.java
public class CheckingAccount extends Account {
CheckingAccount(long amount) {
super("checking", amount);
}
void withdraw(long amount) {
setAmount(getAmount() - amount);
}
}
_______________
CertificateofDeposit.java
public class CertificateofDeposit extends SavingsAccount {
CertificateofDeposit(long amount) {
super(amount);
}
}
________________
Mortgage.java
public class Mortgage extends Account {
Mortgage(long amount) {
super("Mortgage", amount);
}
void withdraw(long amount) {
setAmount(getAmount() - amount);
}
}
___________________
AccountDemo.java
public class AccountDemo {
public static void main(String[] args) {
// info on saving account
SavingsAccount sa = new SavingsAccount(10000);
System.out.println("account name: " + sa.getName());
System.out.println("initial amount: " + sa.getAmount());
sa.deposit(5000);
System.out.println("new amount after deposit: " + sa.getAmount());
System.out.println("");
// info on checking account
CheckingAccount ca = new CheckingAccount(20000);
System.out.println("account name: " + ca.getName());
System.out.println("initial amount: " + ca.getAmount());
ca.deposit(6000);
System.out.println("new amount after deposit: " + ca.getAmount());
ca.withdraw(3000);
System.out.println("new amount after withdrawal: " + ca.getAmount());
System.out.println("");
// add certificate of deposit
CertificateofDeposit cd = new CertificateofDeposit(150000);
System.out.println("account name: " + cd.getName());
System.out.println("initial amount: " + cd.getAmount());
cd.deposit(50000);
System.out.println("new amount after renewal: " + cd.getAmount());
System.out.println("");
// add mortgage information
Mortgage mt = new Mortgage(250000);
System.out.println("account name: " + mt.getName());
System.out.println("initial amount: " + mt.getAmount());
mt.withdraw(50000);
System.out.println("new amount after renewal: " + cd.getAmount());
System.out.println("");
}
}
_____________
Output:
account name: savings
initial amount: 10000
new amount after deposit: 15000
account name: checking
initial amount: 20000
new amount after deposit: 26000
new amount after withdrawal: 23000
account name: savings
initial amount: 150000
new amount after renewal: 200000
account name: Mortgage
initial amount: 250000
new amount after renewal: 200000
__________Thank You