Lab: Experimenting with format strings

  1. Instructions: Do as many of the following tasks as you can. You must work entirely on your own, but you may consult your notes and/or

    request help from the instructor and lab assistant.

    You will submit this assignment to the submit system. You will notice several "submit breaks" in the assignment. Each time you reach one of these, and after testing your own work thoroughly, submit to the submit system to check your work.

    If you fail to complete the assignment during the lab period, it is encouraged that you finish this lab on your own.

    Be sure that you download a new copy of PrintLab, or add the following after the read of the double value: keyboard.nextLine();
    This will insure that you clear the newline that remains after reading in the double value to prepare the input buffer for the String.

  1. Getting Ready: Before going any further you should:
    1. Read the document: formatting.html.
    2. Open the java API's regarding formatting as described in the formatting.html document. Pay particular attention to the conversions and the flags.
    3. Create a working directory for this lab.
    4. Download a copy of PrintLab.java to your working directory.
  2. Tasks:
    1. The "printf()" method and String.format() use a format string, replacing format descriptors (identified by `%') with the values of its arguments.
    2. Compile, then run PrintLab.java.
    3. Add the following printf calls to your program. Add only one at a time, and test each before going on to the next. In the output, each item should print on its own line and there should be no blank lines between output lines.

      1. Print out a line containing the integer with a sign (plus or minus).

      2. Print out a line containing the integer in a field 5 characters wide and with leading zeros (that is, the value 5 gets printed as 00005).

      3. Print out a line containing the integer left justified in a field 5 characters wide. Print the word "next" after the 5 character field.

      4. Print out a line containing the integer in hexidecimal format.

      5. Print out a line containing the integer in octal format.

      6. Print out a line containing the word, "Int: ", followed by the integer, followed by a tab, followed by the word, "Hex: ", followed by the number in hex form, followed by a tab, followed by the word "Octal: ", followed by the number in octal form.You should only have one argument to the right of the format string. Your output should look something like this:

        Int: 55 Hex: 37 Octal: 67

        Stop here and submit to LprintfA.

      1. Print out a line containing the boolean (logical) value of the equation (ii == 5).

      2. Print out a line containing the double with a sign.

      3. Print out a line containing the double with 2 digits after the decimal.

      4. Print out a line containing the double in a field 9 characters wide, with leading zeros and with 1 digit after the decimal.

      5. Print out a line containing the double left justified in a field 9 characters wide with 1 digit after the decimal. Print the word "next" after the 9 character field.

      6. Print out a line containing the double in standard "dollar" format (preceded by a dollar sign and two digits after the decimal).

        Stop here and submit to LprintfB.

      1. Print out a line containing the string enclosed in double quotes.

      2. Print out a line containing the string enclosed in double quotes in a field 20 spaces wide.

      3. Print out a line containing the string enclosed in double quotes left justified in a field 20 spaces wide. Print the word "next" after the 20 spaces.

      4. Print out a line containing the string in a field 20 spaces wide with the field enclosed in double quotes.

        Stop here and submit to LprintfC.

      1. Optional: Print out a line containing the string left justified in a field "ii" characters long (where "ii" is the value in the variable "ii"). Print the word "next" after the "ii" spaces.

        Submit to LprintfD.