Provide printf statements for each of the following. You should choose appropria
ID: 3546399 • Letter: P
Question
Provide printf statements for each of the following. You should choose appropriate variable names given the requirements:
1. Display the first 15 characters of a student's last name left justified, and the student's GPA to one decimal place. The names and GPAs should line up properly in columns if you print multiple students.
2.Display a tax rate as a percentage to 2 decimal places, a total dollar amount purchased for purchases up to $1,000,000.00 (include the commas), and the amount of tax on that purchase (also in dollars and cents with commas). Make sure that all of the amounts line up properly in columns.
Explanation / Answer
1)
if(LastName.length()>15)
LastName = LastName.substring(0, 15);
System.out.println(String.format("%-15s %.1f",LastName,GPA));
2)
DecimalFormat formatter = new DecimalFormat("#,###,###.00");
System.out.println(String.format("%.2f $%-12s $%-12s",RateTax,formatter.format(TaxAmount),formatter.format(Purchase)));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.