Implement the \"this\" refernce in Accounts.java: import java.text.NumberFormat;
ID: 3791730 • Letter: I
Question
Implement the "this" refernce in Accounts.java:
import java.text.NumberFormat;
public class Account
{
private final double RATE = 0.035;
private long acctNumber;
private double balance;
private String name;
public Account(String owner, long account, double initial)
{ name = owner;
acctNumber = account;
balance = initial;
}
public double deposit(double amount)
{
balance = balance + amount; return balance;
}
public double withdraw(double amount, double fee)
{
balance = balance - amount - fee;
return balance;
public double addInterest()
{
balance += (balance * RATE);
return balance;
}
public double getBalance()
{
return balance;
}
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return acctNumber + " " + name + " " + fmt.format(balance);
}
}
Explanation / Answer
Account.java
import java.text.NumberFormat;
public class Account {
//Declaring constant
private final double RATE = 0.035;
//Declaring instance variables
private long acctNumber;
private double balance;
private String name;
//Parameterized constructor
public Account(long acctNumber, double balance, String name) {
super();
this.acctNumber = acctNumber;
this.balance = balance;
this.name = name;
}
//This method will add the amount to the balance
public double deposit(double amount) {
balance = balance + amount;
return balance;
}
//This method will deduct the amount to the balance
public double withdraw(double amount, double fee) {
balance = balance - amount - fee;
return balance;
}
//This method will add the interest amount to the balance
public double addInterest() {
balance += (balance * RATE);
return balance;
}
//This method will return the balance
public double getBalance() {
return balance;
}
//toString() method is used to display the contents of an object inside it
public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return acctNumber + " " + name + " " + fmt.format(balance);
}
}
___________________
TestAccount.java
public class TestAccount {
public static void main(String[] args) {
//Creating The Account class Object by passing the inputs arguments
Account acc1 = new Account(12344, 50000, "Willaims");
System.out.println("** Displaying the Account information **");
//Displaying the Account Information
System.out.println(acc1.toString());
//calling the deposit() method on the Account class Object
acc1.deposit(5000);
//Displaying the Account Information
System.out.println("** Displaying the Account information after deposit **");
System.out.println(acc1.toString());
//calling the withdraw() method on the Account class Object
acc1.withdraw(2500, 150);
//Displaying the Account Information
System.out.println("** Displaying the Account information after withdrawl **");
System.out.println(acc1.toString());
//calling the addInterest() method on the Account class Object
acc1.addInterest();
//Displaying the Account Information
System.out.println("** Displaying the Account information after adding Interest **");
System.out.println(acc1.toString());
}
}
____________________
Output:
** Displaying the Account information **
12344 Willaims $50,000.00
** Displaying the Account information after deposit **
12344 Willaims $55,000.00
** Displaying the Account information after withdrawl **
12344 Willaims $52,350.00
** Displaying the Account information after adding Interest **
12344 Willaims $54,182.25
_______Thank You