Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Answers to Self-Review Exercises

2.1

  1. left brace ({), right brace (}).
  2. if.
  3. //.
  4. Space characters, newlines and tabs.
  5. Keywords.
  6. main.
  7. System.out.print, System.out.println and System.out.printf.

2.2

  1. False. Comments do not cause any action to be performed when the program executes. They're 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 higher precedence than operators + and -.

2.3

  1. int c, thisIsAVariable, q76354, number;

    or

    int c;

    int thisIsAVariable;

    int q76354;

    int number;

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

2.4

  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 >=.

2.5

  1. // Calculate the product of three integers
  2. Scanner input = new Scanner( System.in );
  3. int x, y, z, result;

    or

    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\n", result );

2.6

The solution to Self-Review 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
 4
 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 );
11
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
16
17         System.out.print( "Enter first integer: " ); // prompt for input
18         x = input.nextInt(); // read first integer
19
20         System.out.print( "Enter second integer: " ); // prompt for input
21         y = input.nextInt(); // read second integer
22
23         System.out.print( "Enter third integer: " ); // prompt for input
24         z = input.nextInt(); // read third integer
25
26         result = x * y * z; // calculate product of numbers
27
28         System.out.printf( "Product is %d\n", result );
29      } // end method main
30   } // end class Product
  • + Share This
  • 🔖 Save To Your Account