Java Reference
In-Depth Information
4.29 (Square of Asterisks) Write an application that prompts the user to enter the size of the side
of a square, then displays a hollow square of that size made of asterisks. Your program should work
for squares of all side lengths between 1 and 20.
4.30 (Palindromes) A palindrome is a sequence of characters that reads the same backward as for-
ward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554
and 11611. Write an application that reads in a five-digit integer and determines whether it's a pal-
indrome. If the number is not five digits long, display an error message and allow the user to enter
a new value.
4.31 (Printing the Decimal Equivalent of a Binary Number) 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 remainder and division operators to pick off the binary number's digits one at a time, from right
to left. In the decimal number system, the rightmost digit has a positional value of 1 and the next
digit to the left a positional value of 10, then 100, then 1000, and so on. The decimal number 234
can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. In the binary number system, the rightmost digit has
a positional value of 1, the next digit to the left a positional value of 2, then 4, then 8, and so on.
The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or, 13.]
4.32
(Checkerboard Pattern of Asterisks) Write an application that uses only the output state-
ments
System.out.print( "* " );
System.out.print( " " );
System.out.println();
to display the checkerboard pattern that follows. A System.out.println method call with no argu-
ments causes the program to output a single newline character. [ Hint: Repetition statements are
required.]
********
********
********
********
********
********
********
********
4.33 (Multiples of 2 with an Infinite Loop) Write an application that keeps displaying in the
command window the multiples of the integer 2—namely, 2, 4, 8, 16, 32, 64, and so on. Your loop
should not terminate (i.e., it should create an infinite loop). What happens when you run this pro-
gram?
4.34 (What's Wrong with This Code?) 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.35 (Sides of a Triangle) Write an application that reads three nonzero values entered by the
user and determines and prints whether they could represent the sides of a triangle.
4.36 (Sides of a Right Triangle) Write an application that reads three nonzero integers and de-
termines and prints whether they could represent the sides of a right triangle.
4.37 (Factorial) The factorial of a nonnegative integer n is written as n ! (pronounced “ n factori-
al”) and is defined as follows:
 
Search WWH ::




Custom Search