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.


Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net