Answers to Self Review Exercises


a) left brace ({), right brace (}). b) semicolon (;). c) if. d) //. e) Blank lines, space characters, newline characters and tab characters. f) Keywords. g) main. h) System.out.print, System.out.println and System.out.printf.

  1. False. Comments do not cause any action to be performed when the program executes. They are used to document programs and improve their readability.
  2. True.
  3. False. Java is case sensitive, so these variables are distinct.
  4. False. The remainder operator can also be used with noninteger operands in Java.
  5. False. The operators *, / and % are on the same level of precedence, and the operators + and - are on a lower level of precedence.
  1. int c, thisIsAVariable, q76354, number;


    int c;
    int thisIsAVariable;
    int q76354;
    int number;
  2. System.out.print( "Enter an integer: " );
  3. value = input.nextInt();
  4. if ( number != 7 ) System.out.println( "The variable number is not equal to 7" );
  5. System.out.println("This is a Java program");
  6. System.out.println("This is a Java program");
  7. System.out.printf("%s %s ", "This is a Java", "program");

The solutions to Self-Review Exercise 2.4 are as follows:

  1. Error: Semicolon after the right parenthesis of the condition (c < 7) in the if. Correction: Remove the semicolon after the right parenthesis. [ Note : As a result, the output statement will execute regardless of whether the condition in the if is true.]
  2. Error: The relational operator => is incorrect. Correction: Change => to >=.
  1. // Calculate the product of three integers
  2. Scanner input = new Scanner( System.in );
  3. int x, y, z, result;


    int x;
    int y;
    int z;
    int result;
  4. System.out.print( "Enter first integer: " );
  5. x = input.nextInt();
  6. System.out.print( "Enter second integer: " );
  7. y = input.nextInt();
  8. System.out.print( "Enter third integer: " );
  9. z = input.nextInt();
  10. result = x * y * z;
  11. System.out.printf( "Product is %d ", result );

The solution to Exercise 2.6 is as follows:

 1 // Ex. 2.6: Product.java
 2 // Calculate the product of three integers.
 3 import java.util.Scanner; // program uses Scanner
 5 public class Product
 6 {
 7 public static void main( String args[] )
 8 {
 9 // create Scanner to obtain input from command window
10 Scanner input = new Scanner( System.in );
12 int x; // first number input by user
13 int y; // second number input by user
14 int z; // third number input by user
15 int result; // product of numbers
17 System.out.print( "Enter first integer: " ); // prompt for input
18 x = input.nextInt(); // read first integer
20 System.out.print( "Enter second integer: " ); // prompt for input
21 y = input.nextInt(); // read second integer
23 System.out.print( "Enter third integer: " ); // prompt for input
24 z = input.nextInt(); // read third integer
26 result = x * y * z; // calculate product of numbers
28 System.out.printf( "Product is %d
", result );
30 } // end method main
32 } // end class Product
Enter first integer: 10
Enter second integer: 20
Enter third integer: 30
Product is 6000



Fill in the blanks in each of the following statements:

  1. _____ are used to document a program and improve its readability.
  2. A decision can be made in a Java program with a(n) _____.
  3. Calculations are normally performed by _____ statements.
  4. The arithmetic operators with the same precedence as multiplication are and _____.
  5. When parentheses in an arithmetic expression are nested, the _____ set of parentheses is evaluated first.
  6. A location in the computer's memory that may contain different values at various times throughout the execution of a program is called a(n) _____.

Write Java statements that accomplish each of the following tasks:

  1. Display the message "Enter an integer:", leaving the cursor on the same line.
  2. Assign the product of variables b and c to variable a.
  3. State that a program performs a sample payroll calculation (i.e., use text that helps to document a program).

State whether each of the following is true or false. If false, explain why.

  1. Java operators are evaluated from left to right.
  2. The following are all valid variable names: _under_bar_, m928134, t5, j7, her_sales$, his_$account_total, a, b$, c, z and z2.
  3. A valid Java arithmetic expression with no parentheses is evaluated from left to right.
  4. The following are all invalid variable names: 3g, 87, 67H2, h22 and 2h.

Assuming that x = 2 and y = 3, what does each of the following statements display?

  1. System.out.printf( "x = %d ", x );
  2. System.out.printf( "Value of %d + %d is %d ", x, x, ( x + x ) );
  3. System.out.printf( "x =" );
  4. System.out.printf( "%d = %d ", ( x + y ), ( y + x ) );

Which of the following Java statements contain variables whose values are modified?

  1. p = i + j + k + 7 ;
  2. System.out.println( "variables whose values are destroyed" );
  3. System.out.println( "a = 5" );
  4. value = input.nextInt();

Given that y = ax3 + 7, which of the following are correct Java statements for this equation?

  1. y = a * x * x * x + 7 ;
  2. y = a * x * x * ( x + 7 );
  3. y = ( a * x ) * x * ( x + 7 );
  4. y = ( a * x ) * x * x + 7 ;
  5. y = a * ( x * x * x ) + 7 ;
  6. y = a * x * ( x * x + 7 );

State the order of evaluation of the operators in each of the following Java statements, and show the value of x after each statement is performed:

  1. x = 7 + 3 * 6 / 2 - 1 ;
  2. x = 2 % 2 + 2 * 2 - 2 / 2 ;
  3. x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );

Write an application that displays the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space. Write the program using the following techniques:

  1. Use one System.out.println statement.
  2. Use four System.out.print statements.
  3. Use one System.out.printf statement.

Write an application that asks the user to enter two integers, obtains them from the user and prints their sum, product, difference and quotient (division). Use the techniques shown in Fig. 2.7.


Write an application that asks the user to enter two integers, obtains them from the user and displays the larger number followed by the words "is larger". If the numbers are equal, print the message "These numbers are equal." Use the techniques shown in Fig. 2.15.


Write an application that inputs three integers from the user and displays the sum, average, product, smallest and largest of the numbers. Use the techniques shown in Fig. 2.15. [Note: The calculation of the average in this exercise should result in an integer representation of the average. So if the sum of the values is 7, the average should be 2, not 2.3333....]


Write an application that displays a box, an oval, an arrow and a diamond using asterisks (*), as follows:


What does the following code print?

System.out.println( "*
*****" );

What does the following code print?

System.out.println( "*" );
System.out.println( "***" );
System.out.println( "*****" );
System.out.println( "****" );
System.out.println( "**" );

What does the following code print?

System.out.print( "*" );
System.out.print( "***" );
System.out.print( "*****" );
System.out.print( "****" );
System.out.println( "**" );

What does the following code print?

System.out.print( "*" );
System.out.println( "***" );
System.out.println( "*****" );
System.out.print( "****" );
System.out.println( "**" );

What does the following code print?

System.out.printf( "%s
", "*", "***", "*****" );

Write an application that reads five integers, determines and prints the largest and smallest integers in the group. Use only the programming techniques you learned in this chapter.


Write an application that reads an integer and determines and prints whether it is odd or even. [Hint: Use the remainder operator. An even number is amultiple of 2. Any multiple of 2 leaves a remainder of 0 when divided by 2.]


Write an application that reads two integers, determines whether the first is a multiple of the second and prints the result. [Hint : Use the remainder operator.]


Write an application that displays a checkerboard pattern, as follows:


Here's a peek ahead. In this chapter, you have learned about integers and the type int. Java can also represent floating-point numbers that contain decimal points, such as 3.14159. Write an application that inputs from the user the radius of a circle as an integer and prints the circle's diameter, circumference and area using the floating-point value 3.14159 for p Use the techniques shown in Fig. 2.7. [Note: You may also use the predefined constant Math.PI for the value of p. This constant is more precise than the value 3.14159. Class Math is defined in package java.lang. Classes in that package are imported automatically, so you do not need to import class Math to use it.] Use the following formulas (r is the radius):


Do not store the results of each calculation in a variable. Rather, specify each calculation as the value that will be output in a System.out.printf statement. Note that the values produced by the circumference and area calculations are floating-point numbers. Such values can be output with the format specifier %f in a System.out.printf statement. You will learn more about floating-point numbers in Chapter 3.


Here's another peek ahead. In this chapter, you have learned about integers and the type int. Java can also represent uppercase letters, lowercase letters and a considerable variety of special symbols. Every character has a corresponding integer representation. The set of characters a computer uses and the corresponding integer representations for those characters is called that computer's character set. You can indicate a character value in a program simply by enclosing that character in single quotes, as in 'A'.

You can determine the integer equivalent of a character by preceding that character with (int), as in

 (int) 'A'

This form is called a cast operator. (You will learn about cast operators in Chapter 4.) The following statement outputs a character and its integer equivalent:

 "The character %c has the value %d
" , 'A' , ((int ) 'A' ));

When the preceding statement executes, it displays the character A and the value 65 (from the so-called Unicode® character set) as part of the string. Note that the format specifier %c is a placeholder for a character (in this case, the character 'A').

Using statements similar to the one shown earlier in this exercise, write an application that displays the integer equivalents of some uppercase letters, lowercase letters, digits and special symbols. Display the integer equivalents of the following: A B C a b c 0 1 2 $ * + / and the blank character.


Write an application that inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print

4 2 3 3 9

Assume that the user enters the correct number of digits. What happens when you execute the program and type a number with more than five digits? What happens when you execute the program and type a number with fewer than five digits? [Hint: It is possible to do this exercise with the techniques you learned in this chapter. You will need to use both division and remainder operations to "pick off" each digit.]


Using only the programming techniques you learned in this chapter, write an application that calculates the squares and cubes of the numbers from 0 to 10 and prints the resulting values in table format, as shown below. [Note: This program does not require any input from the user.]

number square cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000

Write a program that inputs five numbers and determines and prints the number of negative numbers input, the number of positive numbers input and the number of zeros input.

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


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


Searching and Sorting

Data Structures



Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2



Accessing Databases with JDBC


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

show all menu

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

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