Question
We also can use the DecimalFormat class to format a report. For this problem, you will format a budget report with 7 items and a
We also can use the DecimalFormat class to format a report. For this problem, you will format a budget report with 7 items and a grade total for a monthly budget:
- Item #1 - Apartment (rent) costing $1,187.90.
- Item #2 - Food for $525.50
- Item #3 - Entertainment for $70.25.
- Item #4 - Utilities for $180.60
- Item #5 - Car and gas for $175
- Item #6 - Insurance for $80.40
- Item #7 - Miscellaneous items and taxes for $210.50
Create one format string that will identify how each line of the report should be formatted and assign it to a variable. It should contain a format specifier for a fixed-length left-justified string, a format specified for a fixed length right-justified string, and format specifier for a new-line.
Create seven string separate variables for the budget item descriptions and seven double variables for their respective monthly costs.
Using seven printf statements, print a formatted report showing each item and its costs. Each printf statement should use the format string from above as its first argument, then one of the seven budget item variables and its corresponding monthly cost variable as the second and third arguments.
Also use a printf statement to print a total line showing the total budget for the month. The total amount should be calculated as the sum of the seven monthly costs. Use the same format string as above.
A sample report might look like the following:
MY MONTHLY BUDGET Item Expense --------------------------- Apartment $1,187.90 Food $525.50 Entertainment $70.25 Utilities $180.60 Car Gas/Maint. $175.00 Insurance 80.40 Misc. and Taxes $210.50 ========== (Total) $2,430.15
Each number on the report should includes a dollar sign and a comma (when appropriate):
- Use a DecimalFormat object to format each number to a String with a dollar sign and comma with two digits to the right of the decimal point.
- Display that String (right-justified) using the printf format specifier %(n)s, where (n) is an integer specifying the length of the field on the report.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started