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 provides a convenient way 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)
remote-submit
submit
Sun's printf-style formatting documentation

When:

Lab Date: 01/12/09
Submit Due Date: 01/14/09, 10:00am

General Instructions:

This lab is divided into a number of parts. Each part requires you to make changes to PrintfLab.java, test your work to verify it is working correctly, then submit the revised program to the submit system.

Part A

  1. Login to the Linux lab computer under your JMU user id.
  2. Create a directory for this lab: mkdir lab-a1
  3. Make that directory your working directory: cd lab-a1
  4. Download a copy of PrintfLab.java to your working directory.
  5. Download a copy of remote-submit to your working directory. The program remote-submit is a new tool that allows you to submit directly from the linux lab computer. To execute remote-submit, enter the command
        sh remote-submit Course Assignment Files...
    from a linux lab computer command line where
  6. 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.
  8. After you have tested your code to verify it is working, submit your program as assignment lab-a1-b . The command to submit using remote-submit is sh remote-submit cs139 lab-a1-b PrintfLab.java. (We are submitting to cs139 instead of cs239 because submit has not been configured for cs239 yet.)

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.
  9. After you have tested your code to verify it is working, submit your program as assignment lab-a1-c .

Part D

  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).
  4. After you have tested your code to verify it is working, submit your program as assignment lab-a1-d .

Part E

  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 the a field at least 5 space 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).
  4. After you have tested your code to verify it is working, submit your program as assignment lab-a1-e .

Reference

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