Java Reference
In-Depth Information
• Integer division (p. 51) yields an integer quotient.
• The remainder operator, % (p. 51), yields the remainder after division.
• Arithmetic expressions must be written in straight-line form (p. 51).
• If an expression contains nested parentheses (p. 52), the innermost set is evaluated first.
• Java applies the operators in arithmetic expressions in a precise sequence determined by the rules
of operator precedence (p. 52).
• When we say that operators are applied from left to right, we're referring to their associativity
(p. 52). Some operators associate from right to left.
• Redundant parentheses (p. 53) can make an expression clearer.
Section 2.8 Decision Making: Equality and Relational Operators
•The if statement (p. 54) makes a decision based on a condition's value (true or false).
•Conditions in if statements can be formed by using the equality ( == and != ) and relational ( > ,
< , >= and <= ) operators (p. 54).
•An if statement begins with keyword if followed by a condition in parentheses and expects one
statement in its body.
• The empty statement (p. 57) is a statement that does not perform a task.
Self-Review Exercises
2.1
Fill in the blanks in each of the following statements:
a)
A(n)
begins the body of every method, and a(n)
ends the body of
every method.
b)
You can use the
statement to make decisions.
c)
begins an end-of-line comment.
d)
,
and
are called white space.
e)
are reserved for use by Java.
f)
Java applications begin execution at method
.
g)
Methods
,
and
display information in a command win-
dow.
2.2
State whether each of the following is true or false . If false , explain why.
a)
Comments cause the computer to print the text after the // on the screen when the pro-
gram executes.
b)
All variables must be given a type when they're declared.
c)
Java considers the variables number and NuMbEr to be identical.
d)
The remainder operator ( % ) can be used only with integer operands.
e)
The arithmetic operators * , / , % , + and - all have the same level of precedence.
2.3
Write statements to accomplish each of the following tasks:
a)
Declare variables c , thisIsAVariable , q76354 and number to be of type int .
b)
Prompt the user to enter an integer.
c)
Input an integer and assign the result to int variable value . Assume Scanner variable
input can be used to read a value from the keyboard.
d)
Print "This is a Java program" on one line in the command window. Use method
System.out.println .
e)
Print "This is a Java program" on two lines in the command window. The first line
should end with Java . Use method System.out.printf and two %s format specifiers.
f)
If the variable number is not equal to 7 , display "The variable number is not equal to 7" .
Search WWH ::




Custom Search