1. |
It is often necessary to repeat a portion of code several times in a program. A simple loop can automate the repetition. Here is a program that computes the number of digits needed to represent a number in base 10. /** Count number of digits needed to express an integer in base 10 using multiple if statements. */ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Input an integer between 1 and 9999: "); int n = in.nextInt(); if (n < 1 || n > 9999) return; int temp = n; int d = 1; if (temp > 9) { temp = temp / 10; d++; } if (temp > 9) { temp = temp / 10; d++; } if (temp > 9) { temp = temp / 10; d++; } if (temp > 9) { temp = temp / 10; d++; } System.out.println(n + " can be expressed in " + d + " digits"); } Repeating code four times, even using copy/paste, is tedious, and the solution works only for n <= 9999. Here
is the same program, with a while
loop: /** Count number of digits needed to express an integer in base 10 using while loop. */ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Input an integer: "); int n = in.nextInt(); int d = 1; int temp = n; while (temp > 9) { temp = temp / 10; d++; } System.out.println(n + " can be expressed in " + d + " digits"); }
The fractions 1/2, 1/4, 1/8 get closer and closer to 0 as they are divided by two. Change the previous program to count the number of divisions by two needed to be within 0.0001 of zero. |
2. |
Which values of year cause the following
loop to terminate? /** Count the number of years from a user-input year until the year 3000. */ public static void main(String[] args) { int millennium = 3000; Scanner in = new Scanner(System.in); System.out.print("Please enter the current year: "); int year = in.nextInt(); int nyear = year; while (nyear != millennium) { nyear++; } System.out.println("Another " + (nyear - year) + " years to the millennium."); } |
3. |
Re-write the preceding while loop so that it will terminate for any integer input. |
4. |
A variable that counts the iterations of a loop
is called a loop index or loop control variable. In the
preceding examples nyear
serves as an index, counting the number of years to the next
millennium. This type of loop is frequently written using a for loop. for (initialization; condition; update) statement Write a program controlled by two (non-nested) for loops that produces the following listing of inclusive dates, from the fifth century B.C. through the fifth century A.D. Century 5 BC 400-499 Century 4 BC 300-399 Century 3 BC 200-299 Century 2 BC 100-199 Century 1 BC 1-99 Century 1 AD 1-99 Century 2 AD 100-199 Century 3 AD 200-299 Century 4 AD 300-399 Century 5 AD 400-499
|
5. |
Write the same program with a single loop for (i = -5 ; i <= 5 ; i++) and an if in the body of the loop. |
6. |
One loop type might be better suited than
another to a particular purpose. The following usages are idiomatic.
for Known number of iterations while Unknown number of iterations do/while At least one iteration Convert the following while loop to a do/while loop. public static void main(String[] args) { Scanner in = new Scanner(System.in); int sum = 0; int n = 1; while (n != 0) { System.out.print("Please enter a number, 0 to quit: "); n = in.nextInt(); if (n != 0) { sum = sum + n; System.out.println("Sum = " + sum); } } }
|
7. |
Is this an improvement? Why or why not? |
8. |
Convert the while loop to a for loop. /** Program to compute the first integral power to which 2 can be raised that is greater than that multiple of a given integer. */ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter a number, 0 to quit: "); int n = in.nextInt(); int i = 1; while (n * n > Math.pow(2,i)) { i++; } System.out.println("2 raised to " + i + " is the first power of two greater than " + n + " squared"); } } |
9. |
Convert to a while loop: public static void main(String[] args) { for (int i = 1; i <= 10; i++) { System.out.println(i + " squared equals " + i * i); } } |
10. |
What is the output of the following loop? for (int i = 0; i < 5; i++) { System.out.print(i + " "); } |
11. |
Leave the loop as it is and change only the expression inside System.out.print so that the program will display "1 2 3 4 5 ". What change do you make to the argument of System.out.print? |
12. |
What is the output of the following loop? int decimals = 1; while (decimals < 100000) { System.out.print(decimals + " "); decimals = decimals * 10; }
|
13. |
Leave the loop as it is and change only the expression inside System.out.print so that the program will display "1 2 3 4 5 ". What change do you make to the argument of System.out.print? |
14. |
What is the output of the following loop? int i = 5;do { System.out.print(i + " "); i--; } while( i > 0 );
|
15. |
Write a program to draw a top view of 24 soda
cans, that is 24 circles, arranged in a 4 x 6 grid like this:
What
is the code for the SodaCanComponent
class? |
16. |
To generate random numbers, you construct an
object of the Random
class, and then apply one of the following methods:
nextInt(n): A random integer between the integers 0 (inclusive) and n (exclusive) nextDouble(): A random floating-point number between 0 (inclusive) and 1 (exclusive) Write a program that simulates the drawing of one playing card (for example, Ace of Spades). |