Java Reference
In-Depth Information
expressions for each of the following, then write an application to show that both the original ex-
pression 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 ))
5.24 (Diamond Printing Program) Write an application that prints the following diamond
shape. You may use output statements that print a single asterisk ( * ), a single space or a single new-
line character. Maximize your use of repetition (with nested for statements), and minimize the
number of output statements.
*
***
*****
*******
*********
*******
*****
***
*
5.25 (Modified Diamond Printing Program) Modify the application you wrote in Exercise 5.24
to read an odd number in the range 1 to 19 to specify the number of rows in the diamond. Your
program should then display a diamond of the appropriate size.
5.26 A criticism of the break statement and the continue statement is that each is unstructured.
Actually, these statements can always be replaced by structured statements, although doing so can
be awkward. Describe in general how you'd remove any break statement from a loop in a program
and replace it with some structured equivalent. [ Hint: The break statement exits a loop from the
body of the loop. The other way to exit is by failing the loop-continuation test. Consider using in
the loop-continuation test a second test that indicates “early exit because of a 'break' condition.”]
Use the technique you develop here to remove the break statement from the application in
Fig. 5.13.
5.27 What does the following program segment do?
for (i = 1 ; i <= 5 ; i++)
{
for (j = 1 ; j <= 3 ; j++)
{
for (k = 1 ; k <= 4 ; k++)
System.out.print( '*' );
System.out.println();
} // end inner for
System.out.println();
} // end outer for
5.28 Describe in general how you'd remove any continue statement from a loop in a program
and replace it with some structured equivalent. Use the technique you develop here to remove the
continue statement from the program in Fig. 5.14.
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
 
Search WWH ::




Custom Search