Lab: Printf Formatting

JamesMadisonUniversity

Objectives:

At the conclusion of this lab, students will be able to use a variety of Java formatting techniques to produce formatted output.

Background:

The format method of the String class and the printf method of the PrintStream class provide convenient ways to produce formatted output. These methods are new to Java 1.5. Prior to that time, DecimalFormatting or NumberFormatting classes were used to produce formatting objects. These are still present in the language.

New Terms:

  • printf-style formatting
  • actual parameters
  • formal parameters
  • parameter list

Materials:

PrintfLab.java
PrintfLab.html (documentation page)
printf Tutorial

General Instructions:

This lab is divided into a number of parts. Each part requires you to make changes to PrintfLab.java and test your work to verify it is working correctly.

Part A

  1. Login to the Linux lab computer under your JMU user id.
  2. Create a directory for this lab: mkdir L09-22
  3. Make that directory your working directory: cd L09-22
  4. Download a copy of PrintfLab.java to your working directory.
  5. Formatted output is accomplished using a format string and one or more arguments. Inside the format string are format specifiers which indicate which argument to substitute, what kind of conversion is required for the value, and the format in which it should be printed. At its simplest, format specifiers are in the form  %Conversion  where Conversion is a single letter that specifies the type of conversion to be performed. This tables summarizes the more frequently used Conversion letters.

    b
    B
    If the argument is null, then the result is false. If the argument is a boolean or Boolean, then the result is the string returned by String.valueOf(). Otherwise, the result is true.
    s
    S
    If the argument is null, then the result is null. If the argument arg is null, then the result is "null". If the argument implements Formattable, then the result is the value returned by calling the argument's formatTo method. Otherwise, the result is the value returned by calling the argument's toString method.
    c
    C
    The result is a Unicode character.
    d The result is formatted as a decimal integer.
    o The result is formatted as an octal integer.
    x
    X
    The result is formatted as a hexadecimal integer.
    e
    E
    The result is formatted as a decimal number in computerized scientific notation.
    f The result is formatted as a decimal number.
    g
    G
    The result is formatted using computerized scientific notation or decimal format, depending on the precision and the value after rounding.
    % The result is a literal '%'.

    Look at the code in the program PrintfLab.java and predict what it will do. Compile and run the program. Does it work as expected? Run the program a few times with a variety of input values.

Part B

  1. Before the Conversion letter, a format specifier may indicate the "field width" in which it outputs its argument. Adding width information, format specifiers are in the form   %[Width][.Precision]Conversion    where the square brackets mean those portions of a specifier are optional. Width indicates the minimal field width for outputting the argument. The meaning of Precision varies depending on the type of conversion performed. For floating point output formats, it is the number of digits output after the decimal point. For strings, it is the maximum number of characters in the string to be printed.
  2. Add code to print out ii in a field at least 5 characters wide (in this and all output statements that follow, output an exclamation point at the end of the output line just before the newline character).
  3. Add code to print out dd in decimal format with 2 digits after the decimal.
  4. Add code to print out dd in computerized scientific notation in a field at least 10 characters wide and with 4 digits after the decimal.
  5. Add code to print out dd in decimal format in a field at least 10 characters wide.
  6. Add code to print out ss in a field at least 10 characters wide.
  7. Add code to print out at most 7 characters of ss in a field at least 10 characters wide.

Part C

  1. Before the Width, one or more flags may be specified. With flags, format specifiers are in the form   %[Flag]...[Width][.Precision]Conversion    where the ... means more than one flag may be specified. Not all flags can be specified with all format specifiers.

    - The result will be left-justified.
    + Numerical results will always include a sign.
    space Numerical results will include a leading space for positive values.
    0 The result will be zero-padded.
    , The result will include grouping separators, 12,345.
    ( The result will enclose negative numbers is parenthesis.

  2. Add code to print out ii with a sign (plus or minus).
  3. Add code to print out ii in a field 5 characters wide and with leading zeros (that is, the value 5 gets printed as 00005).
  4. Add code to print out dd in decimal format left-justified in a field at least 10 characters wide.
  5. Add code to print out dd in computerized scientific notation in a field 9 characters wide, with leading zeros and with 1 digit after the decimal.
  6. Add code to print out dd in decimal format with leading zeros and a sign, in a field at least 10 characters wide.
  7. Add code to print out dd with grouping separators and with negative numbers surrounded by parentheses.
  8. Add code to print out dd in standard "dollar" decimal format (preceded by a dollar sign, commas as needed, and two digits after the decimal) left-justified in a field at least 15 characters wide.

Upload your completed program to Canvas.

Part D - OPTIONAL and Challenging

  1. Add code to print out ss enclosed in double quotes.
  2. Add code to print out ss in a field at least 8 spaces wide with the field enclosed in double quotes (not counting the quotes as part of the field). For example, the string "abc" should be output as The value of ii: "     abc"! (5 spaces after the opening double quote).
  3. Add code to print out ss enclosed in double quotes. left justified in a field at least 10 spaces wide (counting the quotes as part of the field). For example, the string "abc" should be output as The value of ss: "abc"     ! (5 spaces after the closing double quote).

Part E - OPTIONAL and Challenging

  1. Before the flags, an argument index may be specified,   %[ArgIndx$][.Precision]Conversion    where the ArgIndx is a number specifying the argument to be specified with the format specifier. Thus the specifier %5$-12d outputs the 5'th argument as a decimal integer left justified in a field at least 12 spaces wide.
  2. Add code to print out ii in decimal, octal, and hex formats (separate the three values with an equal sign). You must only have one argument to the right of the format string. Your output must look like this: The value of ii: 20=24=14!
  3. This one requires a bit of cleverness. Add code to print out at most ii characters of ss left justified in a field at least ii characters wide (where ii is the value in the variable ii).

Reference

See the document Java Tutorial for a more complete description of format specifiers.