Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Control
Statements:
Part 2
OBJECTIVES
In this chapter you will learn:
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
5.9
Find and correct the error(s) in each of the following segments of code:
a) For ( i = 100, i >= 1, i++ )
System.out.println( i );
ANS: The F in for should be lowercase. Semicolons should be used in the for header in-
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
100
99
98
.
.
.
3
2
1
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
case 1:
System.out.println( "Odd integer" );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Even integer
Odd integer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Even integer
1
2
3
4
5
6
7
8
9
10
11
19
21
23
25
27
29
.
.
.
1
2
3
4
5
6
7
8
9
10
11
19
17
15
13
11
9
7
5
3
1
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
System.out.println( counter );
counter += 2;
} While ( counter < 100 );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
System.out.println( counter );
counter += 2;
} While ( counter < 100 );
} // end main
} // end class PartD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
System.out.println( counter );
counter += 2;
} while ( counter <= 100 );
} // end main
} // end class PartDCorrected
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
2
4
6
.
.
.
96
98
100
5.10
1
2
3
4
5
6
7
8
9
10
11
12
13
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
5.11 Write an application that finds the smallest of several integers. Assume that the first value
read specifies the number of values to input from the user.
ANS:
1
2
3
4
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
1
2
3
4
5
6
7
8
9
10
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
5.13 Factorials are used frequently in probability problems. The factorial of a positive integer n
(written n! and pronounced n factorial) is equal to the product of the positive integers from 1 to
n. Write an application that evaluates the factorials of the integers from 1 to 5. Display the results
in tabular format. What difficulty might prevent you from calculating the factorial of 20?
ANS: Calculating the factorial of 20 might be difficult because the value of 20! exceeds the
maximum value that can be stored in an int.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
n!
1
2
3
4
5
1
2
6
24
120
5.16 One interesting application of computers is to display graphs and bar charts. Write an application that reads five numbers between 1 and 30. For each number that is read, your program
should display the same number of adjacent asterisks. For example, if your program reads the number 7, it should display *******.
ANS:
1
2
3
4
5
6
7
8
9
10
11
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
int
int
int
int
int
number1
number2
number3
number4
number5
=
=
=
=
=
0;
0;
0;
0;
0;
//
//
//
//
//
first number
second number
third number
fourth number
fifth number
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
10
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
System.out.println();
} // end for loop
} // end method drawHistograms
} // end class Graphs
1
2
3
4
5
6
7
8
9
10
case 5:
value = number5;
break; // done processing case
Enter number: 20
Enter number: 6
Enter number: 15
Enter number: 28
Enter number: 5
********************
******
***************
****************************
*****
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
11
Assume that i = 1, j = 2, k = 3 and m = 2. What does each of the following statements print?
a) System.out.println( i == 1 );
ANS: true.
b) System.out.println( j == 3 );
ANS: false.
c) System.out.println( ( i >= 1 ) && ( j < 4 ) );
ANS: true.
d) System.out.println( ( m <= 99 ) & ( k < m ) );
ANS: false.
e) System.out.println( ( j >= i ) || ( k == m ) );
ANS: true.
f) System.out.println( ( k + m < j ) | ( 3 - j >= k ) );
ANS: false.
g) System.out.println( !( k > m ) );
ANS: false.
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
12
true
false
true
false
true
false
false
5.20
Print a table that shows the value of approximated by computing one term of this series, by two
terms, by three terms, and so on. How many terms of this series do you have to use before you rst
get 3.14? 3.141? 3.1415? 3.14159?
ANS: [Note: The program starts to converge around 3.14 at 119, but does not really approach 3.141 until 1688, well beyond the programs range.]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
13
Accuracy: 400
Term
1
2
3
4
5
Pi
4.0000000000000000
2.6666666666666670
3.4666666666666670
2.8952380952380956
3.3396825396825403
.
.
.
395
396
397
398
399
400
3.1441242951029738
3.1390674050903313
3.1441115412820086
3.1390800947411280
3.1440989153182923
3.1390926574960143
5.23 (De Morgans Laws) In this chapter, we have discussed the logical operators &&, &, ||, |, ^
and !. De Morgans Laws can sometimes make it more convenient for us to express a logical expression. These laws state that the expression !(condition1 && condition2) is logically equivalent to the
expression (!condition1 || !condition2). Also, the expression !(condition1 || condition2) is logically
equivalent to the expression (!condition1 && !condition2). Use De Morgans Laws to write equivalent expressions for each of the following, then write an application to show that both the original
expression and the new expression in each case produce the same value:
a) !( x < 5 ) && !( y >= 7 )
b) !( a == b ) || !( g != 5 )
c) !( ( x <= 8 ) && ( y > 4 ) )
d) !( ( i > 4 ) || ( j <= 6 ) )
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
14
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
!(
!(
!(
!(
!(
!(
!(
!(
// part b
if ( !( a == b ) || !( g != 5 ) )
System.out.println( "!( a == b ) || !( g != 5 )" );
if ( !( ( a == b ) && ( g != 5 ) ) )
System.out.println( "!( ( a == b ) && ( g != 5 ) )" );
x = 8;
y = 2;
// part c
if ( !( ( x <= 8 ) && ( y > 4 ) ) )
System.out.println( "!( ( x <= 8 ) && ( y > 4 ) )" );
if ( !( x <= 8 ) || !( y > 4 ) )
System.out.println( "!( x <= 8 ) || !( y > 4 )" );
int i = 0;
int j = 7;
// part d
if ( !( ( i > 4 ) || ( j <= 6 ) ) )
System.out.println( "!( ( i > 4 ) || ( j <= 6 ) )" );
if ( !( i > 4 ) && !( j <= 6 ) )
System.out.println( "!( i > 4 ) && !( j <= 6 )" );
} // end main
} // end class DeMorgan
x
(
a
(
(
x
(
i
5.27
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
****
****
****
****
****
****
****
****
****
****
****
****
****
****
****
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
15
16
5.29 (The Twelve Days of Christmas Song) Write an application that uses repetition and switch
statements to print the song The Twelve Days of Christmas. One switch statement should be
used to print the day (first, second, and so on). A separate switch statement should be used to
print the remainder of each verse. Visit the website en.wikipedia.org/wiki/Twelvetide for the lyrics of the song.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
case 9:
System.out.print( "ninth" );
break;
case 10:
System.out.print( "tenth" );
break;
case 11:
System.out.print( "eleventh" );
break;
case 12:
System.out.print( "twelfth" );
break;
} // end switch
System.out.println(
" day of Christmas, my true love gave to me:" );
// add remainder of verse to String
switch ( day )
{
case 12:
System.out.println( "Twelve lords-a-leaping," );
case 11:
System.out.println( "Eleven pipers piping," );
case 10:
System.out.println( "Ten drummers drumming," );
case 9:
System.out.println( "Nine ladies dancing," );
case 8:
System.out.println( "Eight maids-a-milking," );
case 7:
System.out.println( "Seven swans-a-swimming," );
case 6:
System.out.println( "Six geese-a-laying," );
case 5:
System.out.println( "Five golden rings." );
case 4:
System.out.println( "Four calling birds," );
case 3:
System.out.println( "Three French hens," );
case 2:
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
17
18
101
System.out.println( "Two turtle doves, and" );
102
103
case 1:
104
System.out.println( "a Partridge in a pear tree." );
105
} // end switch
106
107
System.out.println();
108
} // end for
109
} // end method printSong
110 } // end class Twelve
1
2
3
4
5
6
7
8
9
10
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
19
20
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.