Java Reference
In-Depth Information
f) The primitive types ( boolean , char , byte , short , int , long , float and double ) are por-
table across only Windows platforms.
g) Specifying the order in which statements execute in a program is called program control.
h) The unary cast operator (double) creates a temporary integer copy of its operand.
i) Instance variables of type boolean are given the value true by default.
j) Pseudocode helps you think out a program before attempting to write it in a program-
ming language.
4.3 Write four different Java statements that each add 1 to integer variable x .
4.4 Write Java statements to accomplish each of the following tasks:
a) Use one statement to assign the sum of x and y to z , then increment x by 1.
b) Test whether variable count is greater than 10. If it is, print "Count is greater than 10" .
c) Use one statement to decrement the variable x by 1, then subtract it from variable total
and store the result in variable total .
d) Calculate the remainder after q is divided by divisor , and assign the result to q . Write
this statement in two different ways.
4.5 Write a Java statement to accomplish each of the following tasks:
a) Declare variables sum of type int and initialize it to 0 .
b) Declare variables x of type int and initialize it to 1 .
c) Add variable x to variable sum , and assign the result to variable sum .
d) Print "The sum is: " , followed by the value of variable sum .
4.6 Combine the statements that you wrote in Exercise 4.5 into a Java application that calcu-
lates and prints the sum of the integers from 1 to 10. Use a while statement to loop through the
calculation and increment statements. The loop should terminate when the value of x becomes 11.
4.7 Determine the value of the variables in the statement product *= x++; after the calculation
is performed. Assume that all variables are type int and initially have the value 5.
4.8
Identify and correct the errors in each of the following sets of code:
a) while (c <= 5 )
{
product *= c;
++c;
b) if (gender == 1)
System.out.println( "Woman" );
else ;
System.out.println( "Man") ;
4.9
What is wrong with the following while statement?
while (z >= 0 )
sum += z;
Answers to Self-Review Exercises
4.1 a) sequence, selection, repetition. b) if else . c) counter-controlled (or definite). d) sen-
tinel, signal, flag or dummy. e) sequence. f) 0 (zero). g) strongly typed. h) prefixed.
4.2 a) True. b) False. A set of statements contained within a pair of braces ( { and } ) is called a
block. c) False. A repetition statement specifies that an action is to be repeated while some condi-
tion remains true. d) True. e) True. f) False. The primitive types ( boolean , char , byte , short , int ,
long , float and double ) are portable across all computer platforms that support Java. g) True.
h) False. The unary cast operator (double) creates a temporary floating-point copy of its operand.
i) False. Instance variables of type boolean are given the value false by default. j) True.
Search WWH ::




Custom Search