NOTE: The completed code must pass in the following compiler. Please make absolu
ID: 3573694 • Letter: N
Question
NOTE: The completed code must pass in the following compiler. Please make absolutely sure it does before posting: http://codecheck.it/codecheck/files?repo=bj4fp&problem=ch09/c09_exp_9_15
PLEASE also make sure of the following:
-Post this as text, not as an image.
-Avoid making alterations to the original code unless necessary.
-Post the passing output- from the this compiler specifically- as evidence this code was accepted and passed.
~
The System.out.printf method has predefined formats for printing integers, floating-point numbers, and other data types. But it is also extensible. If you use the S format, you can print any class that implements the Formattable interface. That interface has a single method:
void formatTo(Formatter formatter, int flags, int width, int precision)
In this exercise, you should make the BankAccount class implement the Formattable interface. Ignore the flags and precision and simply format the bank balance, using the given width. In order to achieve this task, you need to get an Appendable reference like this:
Appendable a = formatter.out();
Appendable is another interface with a method
void append(CharSequence sequence)
CharSequence is yet another interface that is implemented by (among others) the String class. Construct a string by first converting the bank balance into a string and then padding it with spaces so that it has the desired width. Pass that string to the append method.
Use the following class as your tester class:
Complete the following file:
BankAccount.java
Explanation / Answer
HI,Please find my implementation of BankAccount class.
Please let me know in case of any issue.
public class BankAccount {
private double balance;
public BankAccount(double b) {
balance = b;
}
@Override
public String toString() {
return String.valueOf(balance);
}
}
/*
Sample output:
| 1000.0| 55.0| 345.0| 99000.0|
Expected: | 1000.0| 55.0| 345.0| 99000.0|
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.