C5complete

advertisement
Learn to Program with Java
Chapter 5
1.
Complete the following table for the given For loop, as demonstrated on page 187:
for (int counter = 35; counter > -10; counter = counter – 5)
{
System.out.println(counter);
}
Step
Value of
counter
counter > -10
Body of
Loop
Executed?
1
2
3
4
5
6
7
8
9
10
11
12
35
30
25
20
15
10
5
0
-5
-10
True
True
True
True
True
True
True
True
True
False
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
System.out.println
(Number displayed
in Java Console
window)
35
30
25
20
15
10
5
0
-5
New Value of
counter after
counter =
counter - 5
30
25
20
15
10
5
0
-5
-10
(Hint: you may not need to use every row in the table.)
2.
Write one For loop header, using counter as your loop variable, for each case
described below: (Assume the constants MIN_VALUE and MAX_VALUE)
final int MIN_VALUE = -20;
final int MAX_VALUE = 20;
Example: Count from MIN_VALUE up to, and including, MAX_VALUE:
for (int counter = MIN_VALUE; counter <= MAX_VALUE; counter++)
a. Count from MAX_VALUE down to, but not including, MIN_VALUE:
for (int counter = MAX_VALUE; counter > MINVALUE; couter--)
b. Count from 0 up to, and including, MAX_VALUE, by twos:
for (int counter = 0; counter <= MAX_VALUE; counter += 2)
c. Starting at one, double counter with each loop iteration until it exceeds
MAX_VALUE:
For(int counter = 1; MAX_VALUE > counter; counter *= 2)
d. Starting at double the value of MAX_VALUE, count down by fives until counter
is less than MIN_VALUE:
for (int counter = 2 * MAX_VALUE; counter < MIN_VALUE; counter -= 5)
3.
Write a Java class named C5E3 according to the following instructions. Use
appropriate data types and good coding style (indenting, whitespace).
 Write a program that displays the following dialog box:


4.
Using a While loop, respond to the user’s input appropriately:
 If the user enters anything in the input box increment a counter variable
and display another dialog box with the prompt “Enter another student’s
name, or press Cancel:”. Continue to display this dialog box as long as the
user does not press Cancel, incrementing the counter variable each time.
 If the user presses cancel show a message box that says “You entered X
students”, where X is the total number of students entered. If the user
presses cancel before entering even one student, you should show 0
students as the number entered.
Terminate the program.
Write a new Java class named C5E4, based on C5E3 above, that uses a Do-While
loop in place of the While loop. Use appropriate data types and good coding style
(indenting, whitespace).
5.
Write a new Java class named C5E5 that calculates and displays the factorial of some
number entered by the user, according to the following instructions. Use appropriate
data types and good coding style (indenting, whitespace).
 Declare two integer constants, MIN_VALUE and MAX_VALUE, and assign them
the values 0 and 10, respectively.
 Declare an integer named factorial and initialize its value to zero.
 Declare a string named response and initialize its value to null.
 Display a dialog box asking the user to input a number between MIN_VALUE and
MAX_VALUE, inclusive.
 Check to see if the user pressed Cancel in the dialog box. If so, display a dialog
box with the message “You pressed Cancel”, and terminate the program.
 Check to see if the user entered nothing in the dialog box. If so, display a dialog
box with the message “You didn’t enter anything”, and terminate the program.
 Convert the number the user entered in the dialog box to an integer and put the
numeric value in the factorial variable.
 With a single If statement, check to see if the number entered by the user is
outside of the range specified by MIN_VALUE and MAX_VALUE. If it is, display
a dialog box with the message “You entered an invalid number”, and terminate
the program.
 Check to see if the number entered by the user was zero. If so, display a dialog
box with the message “The factorial of the number you entered is 1”, and
terminate the program.
 Using a For loop, calculate the factorial of the number entered by the user.
Remember that except for zero, whose factorial is one, the factorial of a number is
that number multiplied by every number less than itself, down to one. For
instance, the factorial of three (written 3!) is 3*2*1 = 6.
 Display a dialog box with the message “The factorial of the number you entered is
X”, where X is the value you calculated in the previous step.
 Terminate the program.
Use this table to double-check the accuracy of your program:
Input
0
1
2
3
4
5
6
7
8
9
10
Output
1
1
2
6
24
120
720
5040
40320
362880
3628800
Download