Write a Java program Lab42.java: implement a superclass Account with the protect
ID: 3621354 • Letter: W
Question
Write a Java program Lab42.java: implement a superclass Account with the protected data member double amount. Constructor takes no arguments and intializes amount to 0.00. Write the following member methods:* void deposit(float m) which adds m to amount
* void withdraw(float m) which subtracts m from amount if there are sufficient funds.
Declare two abstractmethods:
* abstract public void show()
* abstract public double dailyInterest()
Create two subclasses: Saving and Checking. Provide a method dailyInterest that computes and adds the daily interest. Checking accounts yield interest of 3% on balances over $1000. Savings accounts yield interest of 6% on the entire balance. Method show prints the current balance. Your driver class Lab42 should create an array Account A[] = new Account[2]. Assign object of type Saving to A[0] and object of type Checking to A[1]. Test all member methods.
Explanation / Answer
public abstract class Account
{
protected double amount;
public Account()
{
amount = 0.0;
}
public void deposit(float m)
{
amount += m;
}
public void withdraw(float m)
{
if(amount >= m)
{
amount -= m;
}
}
public abstract void show();
public abstract double dailyInterest();
}
public class Saving extends Account
{
public void show()
{
System.out.println("Balance: "+amount);
}
public double dailyInterest()
{
double interest = 0.06*amount;
deposit((float)interest);
return interest;
}
}
public class Checking extends Account
{
public void show()
{
System.out.println("Balance: "+String.format("%.2f", amount));
}
public double dailyInterest()
{
double interest = 0;
if(amount >= 1000)
{
interest = 0.03*amount;
}
deposit((float)interest);
return interest;
}
}
public class Lab42
{
public static void main(String[] args)
{
Account a[] = new Account[2];
a[0] = new Saving();
a[1] = new Checking();
a[0].deposit(1500.0f);
a[1].deposit(1500.0f);
System.out.println(a[0].dailyInterest());
System.out.println(a[1].dailyInterest());
a[0].show();
a[1].show();
a[0].withdraw(1000.0f);
a[1].withdraw(1000.0f);
System.out.println(a[0].dailyInterest());
System.out.println(a[1].dailyInterest());
a[0].show();
a[1].show();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.