Java Reference
In-Depth Information
System.out.print("Line 12: Enter an integer: ");
//Line 12
secondNum = console.nextInt();
//Line 13
System.out.println();
//Line 14
System.out.println("Line 15: secondNum = "
+ secondNum);
//Line 15
firstNum = firstNum + NUMBER + 2 * secondNum;
//Line 16
System.out.println("Line 17: The new value of " +
"firstNum = " + firstNum);
//Line 17
}
//Line 18
}
//Line 19
Sample Run: (In this sample run, the user input is shaded.)
Line 11: firstNum = 18
Line 12: Enter an integer: 15
Line 15: secondNum = 15
Line 17: The new value of firstNum = 60
The preceding program works as follows: The statement in Line 1 imports the class
Scanner . The statement in Line 2 names the class containing statements of the
program as FirstJavaProgram . The left brace in Line 3 marks the beginning of the
class FirstJavaProgram .
The statement in Line 4 declares the named constant NUMBER and sets its value to 12 .The
statement in Line 5 declares and initializes the object console to input data from the keyboard.
The statement in Line 6 contains the heading of the method main , and the left brace in
Line 7 marks the beginning of the method main . The statements in Lines 8 and 9 declare
the variables firstNum and secondNum .
The statement in Line 10 sets the value of firstNum to 18 , and the statement in Line 11
outputs the value of firstNum .
Next, the statement in Line 12 prompts the user to enter an integer. The statement in
Line 13 reads and stores the integer into the variable secondNum , which is 15 in the
sample run. The statement in Line 14 positions the insertion point on the screen at the
beginning of the next line. The statement in Line 15 outputs the value of secondNum .
The statement in Line 16 evaluates the expression:
firstNum + NUMBER + 2 * secondNum
and assigns the value of this expression to the variable firstNum , which is 60 in the
sample run. The statement in Line 17 outputs the new value of firstNum . The right
brace in Line 18 marks the end of the method main , and the right brace in Line 19 marks
the end of the class FirstJavaProgram .
 
Search WWH ::




Custom Search