Chapter 4 Control Structures: Part 1 195 Use (Web space)
Chapter 4 Control Structures: Part 1 195 Use an input dialog to read the size from the user. Your program should work for squares of all lengths of side between 1 and 20. 4.24 A palindrome is a number or a text phrase that reads the same backward as forward. For example, each of the following five-digit integers are palindromes: 12321, 55555, 45554 and 11611. Write an application that reads in a five-digit integer and determines whether or not it is a palindrome. If the number is not five digits long, display an error message dialog indicating the problem to the user. When the user dismisses the error dialog, allow the user to enter a new value. 4.25 Write an application that inputs an integer containing only 0s and 1s (i.e., a binary integer) and prints its decimal equivalent. [Hint: Use the modulus and division operators to pick off the binary number s digits one at a time, from right to left. Just as in the decimal number system, where the rightmost digit has a positional value of 1 and the next digit to the left has a positional value of 10, then 100, then 1000, etc., in the binary number system the rightmost digit has a positional value of 1, the next digit to the left has a positional value of 2, then 4, then 8, etc. Thus, the decimal number 234 can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8,or 1 +0 + 4 + 8 or,13.] 4.26 Write an application that displays the following checkerboard pattern: ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** Your program may use only three output statements, one of the form System.out.print( “* ” ); one of the form System.out.print( ” ” ); and one of the form System.out.println(); Note that the preceding statement indicates that the program should output a single newline character to drop to the next line of the output. [Hint: Repetition structures are required in this exercise.] 4.27 Write an application that keeps displaying in the command window the multiples of the integer 2, namely 2, 4, 8, 16, 32, 64, etc. Your loop should not terminate (i.e., you should create an infinite loop). What happens when you run this program? 4.28 What is wrong with the following statement? Provide the correct statement to add one to the sum of x and y. System.out.println( ++(x + y) ); 4.29 Write an application that reads three nonzero values entered by the user in input dialogs and determines and prints if they could represent the sides of a triangle. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01