Wrap-Up

Answers to Self Review Exercises

28.1

a) Streams. b) Standard input. c) Standard output. d) printf. e) d. f) o, x or X. g) e or E. h) 6. i) s or S, c or C. j) t, T. k) - (minus). l) + (plus). m) 2$. n) Formatter.

28.2
  1. Error: Conversion character c expects an argument of primitive type char.

    Correction: To print the character 'c', change "c" to 'c'.

  2. Error: Trying to print the literal character % without using the format specifier %%.

    Correction: Use %% to print a literal % character.

  3. Error: Argument index does not start with 0, e.g., the first argument is 1$.

    Correction: To print the third argument use 3$.

  4. Error: Trying to print the literal character " without using the " escape sequence.

    Correction: Replace each quote in the inner set of quotes with ".

  5. Error: The format string is not enclosed in double quotes.

    Correction: Enclose %d %d in double quotes.

  6. Error: The string to be printed is enclosed in single quotes.

    Correction: Use double quotes instead of single quotes to represent a string.

28.3
  1. System.out.printf( "%10d ", 1234 );
  2. System.out.printf( "%+.3e ", 123.456789 );
  3. System.out.printf( "%#o ", 100 );
  4. System.out.printf( "%tD ", calendar );
  5. System.out.printf( "%1$tH:%1$tM:%1$tS ", calendar );
  6. System.out.printf( "%+20.3f ", 3.333333 );

Exercises

28.4

Write statement(s) for each of the following:

  1. Print integer 40000 right justified in a 15-digit field.
  2. Print 200 with and without a sign.
  3. Print 100 in hexadecimal form preceded by 0x.
  4. Print 1.234 with three digits of precision in a nine-digit field with preceding zeros.

28.5

Show what is printed by each of the following statements. If a statement is incorrect, indicate why.

  1. System.out.printf( "%-10d ", 10000 );
  2. System.out.printf( "%c ", "This is a string" );
  3. System.out.printf( "%8.3f ", 1024.987654 );
  4. System.out.printf( "%#o %#X ", 17, 17 );
  5. System.out.printf( "% d %+d ", 1000000, 1000000 );
  6. System.out.printf( "%10.2e ", 444.93738 );
  7. System.out.printf( "%d ", 10.987 );

28.6

Find the error(s) in each of the following program segments. Show the corrected statement.

  1. System.out.printf( "%s ", 'Happy Birthday' );
  2. System.out.printf( "%c ", 'Hello' );
  3. System.out.printf( "%c ", "This is a string" );
  4. The following statement should print "Bon Voyage" with the double quotes:

    System.out.printf( ""%s"", "Bon Voyage" );

  5. The following statement should print "Today is Friday":

    System.out.printf( "Today is %s ", "Monday", "Friday" );

  6. System.out.printf( 'Enter your name: ' );
  7. System.out.printf( %f, 123.456 );
  8. The following statement should print the current time in the format "hh:mm:ss":

    Calendar dateTime = Calendar.getInstance(); System.out.printf( "%1$tk:1$%tl:%1$tS ", dateTime );

28.7

(Printing Dates and Times) Write a program that prints dates and times in the following forms:

GMT-05:00 04/30/04 09:55:09 AM GMT-05:00 April 30 2004 09:55:09 2004-04-30 day-of-the-month:30 2004-04-30 day-of-the-year:121 Fri Apr 30 09:55:09 GMT-05:00 2004  

[ Note: Depending on your location, you may get the time zone other than GMT-05:00.]

28.8

Write a program to test the results of printing the integer value 12345 and the floating-point value 1.2345 in various size fields.

28.9

(Rounding Numbers) Write a program that prints the value 100.453627 rounded to the nearest digit, tenth, hundredth, thousandth and ten thousandth.

28.10

Write a program that inputs a word from the keyboard and determines the length of the word. Print the word using twice the length as the field width.

28.11

(Converting Fahrenheit Temperature to Celsius) Write a program that converts integer Fahrenheit temperatures from 0 to 212 degrees to floating-point Celsius temperatures with three digits of precision. Use the formula

celsius = 5.0 / 9.0 * ( fahrenheit - 32 );  

to perform the calculation. The output should be printed in two right-justified columns of 10 characters each, and the Celsius temperatures should be preceded by a sign for both positive and negative values.

28.12

Write a program to test all the escape sequences in Fig. 28.23. For the escape sequences that move the cursor, print a character before and after the escape sequence so that it is clear where the cursor has moved.

28.13

Write a program that uses the conversion character g to output the value 9876.12345. Print the value with precisions ranging from 1 to 9.

Категории