used to perform a repetitive task when the number of iterations required is known beforehand
conditional loop
used to perform repetitive tasks when the number of iterations depends on a non-counting bound
for statement
a structured loop, with three parts in the header, designed to make counting loops easier to code
while statement
a conditional loop that has one part in the header, where the loop bound is checked before the loop body is executed.
do-while statement
a conditional loop that has one part in the header, where the loop bound is checked after the loop body is executed, so this kind of loop is always executed at least once
zero indexing
counting starts at zero
unit indexing
counting starts at one
sentinel bound
a special value used to stop the loop
limit bound
a value that a loop approaches incrementally and when reached the loop terminates
counting bound
a value that stops the loop when the specific count is reached
flag bound
a boolean variable that stops the loop when its value is set to true
loop initializer
a statement executed before the first iteration of a loop
loop updater
a statement that is executed (typically at the end of) each iteration
named constant
a final variable whose value remains the same throughout the program
literal
an actual value or quoted string
compound statement
A sequence of simple statements contained withing a set of curly braces
null statement
The absense of a statment, signified by empty curly braces or a semicolon by itself.
Fill in the Blank.
Fill in the blank.
2.Reading Datat.
The process of reading a data item before entering a loop is known as a .
3.Loop Types.
A loop that should iterate until the user types in a special value should use a bound.
4.More Loop Types.
A loop that should iterate until its variable goes from 5 to 100 should use a bound.
5.Loop Types Again.
A loop that should iterate until the difference between two values is less than 0.005 is an example of a bound.
6.Java Code Tracing.
Identify the syntax errors in each of the following:
(a)
for (int k = 0; k < 100; k++) System.out.println(k)
(b)
for (int k = 0; k < 100; k++); System.out.println(k);
(c)
int k = 0 while k < 100 { System.out.println(k); k++;}
(d)
int k = 0; do { System.out.println(k); k++;} while k < 100 ;
7.Trace and Determine the Output.
Determine the output and/or identify the error in each of the following code segments:
(a)
for (int k = 1; k == 100; k += 2) System.out.println(k);
(b)
int k = 0; while (k < 100) System.out.println(k); k++;
(c)
for (int k = 0; k < 100; k++) ; System.out.println(k);
8.Write Pseudocode.
Write pseudocode algorithms for the following activities, paying particular attention to the initializer, updater, and boundary condition in each case.
(a)
a softball game
(b)
a five-question quiz
(c)
looking up a name in the phone book
9.Pre and Post Conditions.
Identify the pre- and postconditions for each of the statements that follow. Assume that all variables are int and have been properly declared.
(a)Division.
int result = x / y;
(b)Modulus.
int result = x % y;
(c)Do-While.
int x = 95; do x /= 2; while(x >= 0);
10.Compare Three Loops.
Write three different loops—a for loop, a while loop, and a do-while loop—to print all the multiples of 10, including 0, up to and including 1,000.
(a)For Loop.
print all the multiples of 10, including 0, up to and including 1,000 using a for loop
(b)While Loop.
print all the multiples of 10, including 0, up to and including 1,000 using a while loop
(c)Do-While Loop.
print all the multiples of 10, including 0, up to and including 1,000 using a do-while loop
11.Three Loops Descending.
Write three different loops—a for loop, a while loop, and a do-while loop—to print the following sequence of numbers: 45, 36, 27, 18, 9, 0, \(-9\text{,}\)\(-18\text{,}\)\(-27\text{,}\)\(-36\text{,}\)\(-45\text{.}\)
12.Three loops ski jump.
Write three different loops—a for loop, a while loop, and a do-while loop—to print the following ski-jump design:
The Straight Downhill Ski Lodge in Gravel Crest, Vermont, gets lots of college students on breaks. The lodge likes to keep track of repeat visitors. Straight Downhill’s database includes an integer variable, visit, which gives the number of times a guest has stayed at the lodge (1 or more). Write the pseudocode to catch those visitors who have stayed at the lodge at least twice and to send them a special promotional package (pseudocode = send promo). (Note: The largest number of stays recorded is eight. The number nine is used as an end-of-data flag.)
14.Extra Coupon Pseudocode.
Modify your pseudocode in the previous exercise. In addition to every guest who has stayed at least twice at the lodge receiving a promotional package, any guest with three or more stays should also get a $40 coupon good for lodging, lifts, or food.
15.Even Number Loop Method.
Write a method that is passed a single parameter, N, and displays all the even numbers from 1 to N.
16.Odd Number Loop Method.
Write a method that is passed a single parameter, N, that prints all the odd numbers from 1 to N.
17.Count Down By Ten Method.
Write a method that is passed a single parameter, N, that prints all the numbers divisible by 10 from N down to 1.
18.N Characters Method.
Write a method that is passed two parameters—a charCh and an intN—and prints a string of N Chs.
19.Multiplication Table Method.
Write a method that uses a nested for loop to print the following multiplication table:
Write a method that uses nested for loops to print the patterns that follow. Your method should use the following statement to print the patterns: System.out.print('#');.
Write a program that asks the user for the number of rows and the number of columns in a box of asterisks. Then use nested loops to generate the box.
22.Consecutive Number Program.
Write a Java application that lets the user input a sequence of consecutive numbers. In other words, the program should let the user keep entering numbers as long as the current number is one greater than the previous number.
23.Min and Max Program.
Write a Java application that lets the user input a sequence of integers terminated by any negative value. The program should then report the largest and smallest values that were entered.
24.How Many Guesses.
How many guesses does it take to guess a secret number between 1 and N? For example, I’m thinking of a number between 1 and 100. I’ll tell you whether your guess is too high or too low. Obviously, an intelligent first guess would be 50. If that’s too low, an intelligent second guess would be 75. And so on. If we continue to divide the range in half, we’ll eventually get down to one number. Because you can divide 100 seven times (50, 25, 12, 6, 3, 1, 0), it will take at most seven guesses to guess a number between 1 and 100. Write a Java Swing program that lets the user input a positive integer, N, and then reports how many guesses it would take to guess a number between 1 and N. Separate the Swing GUI from the rest of the program, so that an alternate command line interface (CLI) can be used, and put the CLI (instead of the Swing UI) into the version below.
25.Fire Extinguisher Expiration.
Suppose you determine that the fire extinguisher in your kitchen loses X percent of its foam every day. How long before it drops below a certain threshold (Y percent), at which point it is no longer serviceable? Write a Java Swing program that lets the user input the values X and Y and then reports how many weeks the fire extinguisher will last. Separate the Swing GUI from the rest of the program, so that an alternate command line interface (CLI) can be used, and put the CLI (instead of the Swing UI) into the version below.
26.Computing Pi.
Leibnitz’s method for computing \(\pi\) is based on the following convergent series:
How many iterations does it take to compute \(\pi\) to a value between 3.141 and 3.142 using this series? Write a Java program to find out.
27.Compute Square Root.
Newton’s method for calculating the square root of N starts by making a (nonzero) guess at the square root. It then uses the original guess to calculate a new guess, according to the following formula:
guess = (( N / guess) + guess) / 2;
No matter how wild the original guess is, if we repeat this calculation, the algorithm will eventually find the square root. Write a square root method based on this algorithm. Then write a program to determine how many guesses are required to find the square roots of different numbers. Uses Math.sqrt() to determine when to terminate the guessing.
28.Prime Finder.
Your employer is developing encryption software and wants you to develop a Java Swing Program that will display all of the primes less than N, where N is a number to be entered by the user. In addition to displaying the primes themselves, provide a count of how many there are. Separate the Swing GUI from the rest of the program, so that an alternate command line interface (CLI) can be used, and put the CLI (instead of the Swing UI) into the version below.
29.Multiplication Quiz.
Your little sister asks you to help her with her multiplication and you decide to write a Java application that tests her skills. The program will let her input a starting number, such as 5. It will generate multiplication problems ranging from from \(5 \times 1\) to \(5 \times 12\text{.}\) For each problem she will be prompted to enter the correct answer. The program should check her answer and should not let her advance to the next question until the correct answer is given to the current question.
30.ASCII bar graph.
Write an application that prompts the user for four values and draws corresponding bar graphs using an ASCII character. For example, if the user entered 15, 12, 9, and 4, the program would draw
******************
************
*********
****
31.ASCII Vertical Bar Graph.
Revise the application in the previous problem so that the bar charts are displayed vertically. For example, if the user inputs 5, 2, 3, and 4, the program should display
The Fibonacci sequence (named after the Italian mathematician Leonardo of Pisa, ca. 1200) consists of the numbers \(0,1,1,2,3,5,8,13,\dots\) in which each number (except for the first two) is the sum of the two preceding numbers. Write a method fibonacci(N) that prints the first N Fibonacci numbers.
33.Half Life Program.
The Nuclear Regulatory Agency wants you to write a program that will help determine how long certain radioactive substances will take to decay. The program should let the user input two values: a string giving the substance’s name and its half-life in years. (A substance’s half-life is the number of years required for the disintegration of half of its atoms.) The program should report how many years it will take before there is less than 2 percent of the original number of atoms remaining.
34.Better Car Loan Program.
Modify the CarLoan program so that it calculates a user’s car payments for loans of different interest rates and different loan periods. Let the user input the amount of the loan. Have the program output a table of monthly payment schedules.