For programming 5.12 Account class (the actual code is below): { Account acct1 =
ID: 3601616 • Letter: F
Question
For programming 5.12 Account class (the actual code is below):
{
Account acct1 = new Account("Ted Murphy", 72354, 25.59);
Account acct2 = new Account("Angelica Adams", 69713, 500.00);
Account acct3 = new Account("Edward Demsey", 93757, 769.32);
acct1.deposit(44.10); // return value ignored
double adamsBalance = acct2.deposit(75.25);
System.out.println("Adams balance after deposit: " + adamsBalance);
System.out.println("Adams balance after withdrawal: " + acct2.withdraw(480, 1.50));
acct3.withdraw(-100.00, 1.50); // invalid transaction
acct1.addInterest();
acct2.addInterest();
acct3.addInterest();
System.out.println();
System.out.println(acct1);
System.out.println(acct2);
System.out.println(acct3);
}
}
Account.Java
import java.text.NumberFormat;
public class Account
{
private final double RATE = 0.035; // interest rate of 3.5%
private long acctNumber;
private double balance;
private String name;
// -----------------------------------------------------------------
// Sets up the account by defining its owner, account number,
// and initial balance.
// -----------------------------------------------------------------
public Account(String owner, long account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
// -----------------------------------------------------------------
// Deposits the specified amount into the account and returns
// the new balance. The balance is not modified if the deposit
// amount is invalid.
// -----------------------------------------------------------------
public double deposit(double amount)
{
if (amount > 0)
balance = balance + amount;
return balance;
}
// -----------------------------------------------------------------
// Withdraws the specified amount and fee from this account and
// returns the new balance. The balance is not modified if the
// withdraw amount is invalid or the balance is insufficient.
// -----------------------------------------------------------------
public double withdraw(double amount, double fee)
{
if (amount + fee > 0 && amount + fee < balance)
balance = balance - amount - fee;
return balance;
}
// -----------------------------------------------------------------
// Adds interest to the account and returns the new balance.
// -----------------------------------------------------------------
public double addInterest()
{
balance += (balance * RATE);
return balance;
}
// -----------------------------------------------------------------
// Returns the current balance of this account.
// -----------------------------------------------------------------
public double getBalance()
{
return balance;
}
// -----------------------------------------------------------------
// Returns a one-line description of this account as a string.
// -----------------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (acctNumber + " " + name + " " + fmt.format(balance));
}
}
The question is add a new constructor Account(String owner, long account). Also add a new instance method public double compoundInterestAfter(int yrs) which returns the balance after accumulating interest for yrs years at the given interest rate. But: do NOT change the current balance field when this method is called; just calculate and return what the new balance would be.
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.NumberFormat;
class Account
{
private final double RATE = 0.035; // interest rate of 3.5%
private long acctNumber;
private double balance;
private String name;
// -----------------------------------------------------------------
// Sets up the account by defining its owner, account number,
// and initial balance.
// -----------------------------------------------------------------
public Account(String owner, long account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
// -----------------------------------------------------------------
// Deposits the specified amount into the account and returns
// the new balance. The balance is not modified if the deposit
// amount is invalid.
// -----------------------------------------------------------------
Account(String owner, long account)
{
name = owner;
acctNumber = account;
}
public double compoundInterestAfter(int yrs)
{
return ( balance * Math.pow(1 + RATE, yrs));
}
public long getAccountNo()
{
return acctNumber;
}
public double deposit(double amount)
{
if (amount > 0)
balance = balance + amount;
return balance;
}
// -----------------------------------------------------------------
// Withdraws the specified amount and fee from this account and
// returns the new balance. The balance is not modified if the
// withdraw amount is invalid or the balance is insufficient.
// -----------------------------------------------------------------
public double withdraw(double amount, double fee)
{
if (amount + fee > 0 && amount + fee < balance)
balance = balance - amount - fee;
return balance;
}
// -----------------------------------------------------------------
// Adds interest to the account and returns the new balance.
// -----------------------------------------------------------------
public double addInterest()
{
balance += (balance * RATE);
return balance;
}
// -----------------------------------------------------------------
// Returns the current balance of this account.
// -----------------------------------------------------------------
public double getBalance()
{
return balance;
}
// -----------------------------------------------------------------
// Returns a one-line description of this account as a string.
// -----------------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (acctNumber + " " + name + " " + fmt.format(balance));
}
}
class Test
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
Account acct1 = new Account("Ted Murphy", 72354, 25.59);
Account acct2 = new Account("Angelica Adams", 69713, 500.00);
Account acct3 = new Account("Edward Demsey", 93757, 769.32);
acct1.deposit(44.10); // return value ignored
double adamsBalance = acct2.deposit(75.25);
System.out.println("Adams balance after deposit: " + adamsBalance);
System.out.println("Adams balance after withdrawal: " + acct2.withdraw(480, 1.50));
acct3.withdraw(-100.00, 1.50); // invalid transaction
acct1.addInterest();
acct2.addInterest();
acct3.addInterest();
System.out.println();
System.out.println(acct1);
System.out.println(acct2);
System.out.println(acct3);
System.out.println(" Enter the number of years for compound interest for acct3 :");
int years = sc.nextInt();
System.out.println(" Compound interest of acct# "+acct3.getAccountNo()+" after "+ years+" years : "+acct3.compoundInterestAfter(years));
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.