Write a new class TruncatedDollarFormat that is the same as the class DollarFormat from Listing 6.14, except that it truncates rather than rounds to obtain two digits after the decimal point. When truncating, all digits after the first two are discarded, so 1.229 becomes 1.22, not 1.23. Repeat Programming Project 3 in Chapter 4 using this new class.
This project may require a little trial and error to get the code right to truncate past the two digits of the cents and not lose the cents completely. Casting a double to an int will truncate, but it has to be done after the dollars.cents is multiplied by 100, and an explicit cast is required by the java compiler (unlike C or C++):
```
int allCents = amount * 100;
```
gives a compiler error since amount is type double.
```
int allCents = (int)amount * 100;
```
loses the cents part of amount because the cast operates on amount before multiplying by 100.
Putting parentheses around the multiplication, however, makes it do the multiplication first:
```
int allCents = (int)(amount * 100);
```
so it does not lose the cents digits.
The only other “tricky” part is using the write() method in TruncatedDollars along with System.out.println() and System.out.print() to display money values interspersed with text in sentences.
See the code in TruncatedDollarFormat.java and TruncatedBankAccount.java.
You might also like to view...
In a chart, the ________ is an index that identifies the data
Fill in the blank(s) with correct word
Explain the benefits of using a subform for Sales data associated with individual customers.
What will be an ideal response?
Which type of uninterruptible power supply uses AC power to continuously charge its battery, while also providing power to devices through the battery?
a. standby UPS b. online UPS c. line conditioning UPS d. surge UPS
To ensure that documents you save in OneDrive are synchronized with copies of the same documents saved on your hard drive, you could use:
What will be an ideal response?