For This Assignment Each Reference To Display Should Be On A Separate Output Line Question …

For This Assignment Each Reference To Display Should Be On A Separate Output LineQuestion Description An area of San Juan PR has a zip code of 00901 and a sales tax of 6%.Create the appropriate numeric variable to store any valid 5 digit zip code.Create a double variable for my total cost.My hotel charge is $55.00 Store this in a constant.Prompt for the San Juan Zip Code.Display the unformatted zip code.Format and display the zip code so that it has leading zeros and a size of 5.Calculate my total cost by multiplying my hotel charge by 1.06Display the unformatted total cost.Format and display the total cost rounded to 2 decimal places. Use descriptive variable names with the standard naming convention. Book Information:3.5 Formatting Output
When you output a double using print or println, it displays up to 16
decimal places:
System.out.print(4.0 / 3.0);
The result is:
That might be more than you want. System.out provides another method,
called printf, that gives you more control of the format. The f” in printf
stands for formatted”. Here’s an example:
System.out.printf(“Four thirds = %.3f”, 4.0 / 3.0);
The first value in the parentheses is a format string that specifies how the
output should be displayed. This format string contains ordinary text followed
by a format specifier, which is a special sequence that starts with a percent
sign. The format specifier %.3f indicates that the following value should be
displayed as floating-point, rounded to three decimal places. The result is:
Four thirds = 1.333
The format string can contain any number of format specifiers; here’s an example with two of them:
int inch = 100;
double cm = inch * CM_PER_INCH;
System.out.printf(“%d in = %f cmn”, inch, cm);
The result is:
100 in = 254.000000 cm
Like print, printf does not append a newline. So format strings often end
with a newline character.
The format specifier %d displays integer values (d” stands for decimal”,
meaning base 10 integer). The values are matched up with the format specifiers
in order, so inch is displayed using %d, and cm is displayed using %f.
40 Chapter 3 Input and Output
Learning about format strings is like learning a sub-language within Java.
There are many options, and the details can be overwhelming. Table 3.1 lists
a few common uses, to give you an idea of how things work.
%d integer in base 10 (decimal”) 12345%,d integer with comma separators 12,345%08d padded with zeros, at least 8 digits wide 00012345%f floating-point number 6.789000%.2f rounded to 2 decimal places 6.79%s string of characters “Hello”%x integer in base 16 (hexadecimal”) bc614eTable 3.1: Example format specifiers
For more details, refer to the documentation of java.util.Formatter. The
easiest way to find documentation for Java classes is to do a web search for
Java” and the name of the class.