Java only and please makesure it works. Add the following method to the BankAcco
ID: 663103 • Letter: J
Question
Java only and please makesure it works.
Add the following method to the BankAccount class:
public String toString()
Your method should return a string that contains the account's name and balance separated by a comma and space. For example, if an account object named benben has the name "Benson" and a balance of 17.25, the call of benben.toString() should return:
There are some special cases you should handle. If the balance is negative, put the - sign before the dollar sign. Also, always display the cents as a two-digit number. For example, if the same object had a balance of -17.5, your method should return:
Your code is being added to the following class:
Explanation / Answer
public class BankAccount {
private String name;
private double balance;
// your code goes here
BankAccount()
{
this.name = "Benson";
this.balance = -22.33;
}
public String toString()
{
StringBuilder stringValue = new StringBuilder();
stringValue.append(this.name);
if (this.balance <0){
stringValue.append(", -$");
stringValue.append(this.balance);
return stringValue.toString();
}
else{
stringValue.append(", $");
stringValue.append(this.balance);
return stringValue.toString();
}
}
public static void main(String jk[])
{
BankAccount benben = new BankAccount();
System.out.println(benben.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.