Case Studies on Loops

Case Studies on Loops

Loops are fundamental in programming. The ability to write loops is essential in learning Java programming. If you can write programs using loops, you know how to program!

Finding the Greatest Common Divisor

The greatest common divisor (gcd) of the two integers 4 and 2 is 2. The greatest common divisor of the two integers 16 and 24 is 8. How would you write this program to find the greatest common divisor? Would you immediately begin to write the code? No. It is important to think before you code. Thinking enables you to generate a logical solution for the problem without concern about how to write the code.

Let the two input integers be n1 and n2. You know that number 1 is a common divisor, but it may not be the greatest common divisor. So, you can check whether k (for k = 2, 3, 4, and so on) is a common divisor for n1 and n2, until k is greater than n1 or n2. Store the common divisor in a variable named gcd. Initially, gcd is 1. Whenever a new common divisor is found, it becomes the new gcd. When you have checked all the possible common divisors from 2 up to n1 or n2, the value in variable gcd is the greatest common divisor. Once you have a logical solution, type the code to translate the solution into a Java program as follows:

int gcd = 1; // Initial gcd is 1
int k = 2; // Possible gcd
while (k <= n1 && k <= n2) {
if (n1 % k == 0 && n2 % k == 0)
gcd = k; // Update gcd
k++; // Next possible gcd
}
// After the loop, gcd is the greatest common divisor for n1 and n2

The program prompts the user to enter two positive integers and finds their greatest common divisor.

Translating a logical solution to Java code is not unique. For example, you could use a for loop to rewrite the code as follows:

for (int k = 2; k <= n1 && k <= n2; k++) {
if (n1 % k == 0 && n2 % k == 0)
gcd = k;
}

A problem often has multiple solutions, and the gcd problem can be solved in many ways.

Case Study: Predicting the Future Tuition

Suppose that the tuition for a university is $10,000 this year and tuition increases 7% every year. In how many years will the tuition be doubled?

Before you can write a program to solve this problem, first consider how to solve it by hand. The tuition for the second year is the tuition for the first year * 1.07. The tuition for a future year is the tuition of its preceding year * 1.07. Thus, the tuition for each year can be computed as follows:

double tuition = 10000; int year = 0; // Year 0
tuition = tuition * 1.07; year++; // Year 1
tuition = tuition * 1.07; year++; // Year 2
tuition = tuition * 1.07; year++; // Year 3

Keep computing the tuition for a new year until it is at least 20000. By then you will know how many years it will take for the tuition to be doubled. You can now translate the logic into the following loop:

double tuition = 10000; // Year 0
int year = 0;
while (tuition < 20000) {
tuition = tuition * 1.07;
year++;
}

The complete program is shown below:

The while loop (lines 8-11) is used to repeatedly compute the tuition for a new year. The loop terminates when the tuition is greater than or equal to 20000.

Case Study: Converting Decimals to Hexadecimals

Hexadecimals are often used in computer systems programming. How do you convert a decimal number to a hexadecimal number? These hexadecimal digits can be found by successively dividing d by 16 until the quotient is 0. The hexadecimal digits include the
decimal digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, plus A, which is the decimal value 10; B, which is the decimal value 11; C, which is 12; D, which is 13; E, which is 14; and F, which is 15.

For example, the decimal number 123 is 7B in hexadecimal. The conversion is done as follows. Divide 123 by 16. The remainder is 11 (B in hexadecimal) and the quotient is 7. Continue divide 7 by 16. The remainder is 7 and the quotient is 0. Therefore 7B is the hexadecimal number for 123. The program prompts the user to enter a decimal number and converts it into a hex number as a string.

The program prompts the user to enter a decimal integer (line 12), converts it to a hex number as a string (lines 15–25), and displays the result (line 27). To convert a decimal to a hex number, the program uses a loop to successively divide the decimal number by 16 and obtain its remainder (line 18). The remainder is converted into a hex character (lines 21). The character is then appended to the hex string (line 23). The hex string is initially empty (line 15). Divide the decimal number by 16 to remove a hex digit from the number (line 24). The loop ends when the remaining decimal number becomes 0.

The program converts a hexValue between 0 and 15 into a hex character. If hexValue is between 0 and 9, it is converted to (char)(hexValue + ‘0’) (line 21). Recall that when adding a character with an integer, the character’s Unicode is used in the evaluation. For example, if hexValue is 5, (char)(hexValue + ‘0’) returns 5. Similarly, if hexValue is between 10 and 15, it is converted to (char)(hexValue – 10 + ‘A’) (line 21). For instance, if hexValue is 11, (char)(hexValue – 10 + ‘A’) returns B.

Leave a Reply

Your email address will not be published. Required fields are marked *