Chapter 4

Test Your Thinking

As you may have figured out by now, there are two kinds of flow control operators: a branching operator (if, switch) and a looping operator (for, while). With these operators it is possible to do a lot of work without doing a lot of coding.

1) Write code to find all of the prime numbers between 2 and 100. A prime number is a number that is evenly divisible only by itself and 1. The number 10 is not a prime because it is evenly divisible by 2 and 5.

Answer:

The easiest way to find prime numbers is to use the "brute force" method and just check each number from 2 to 100. This works fine for small numbers, but really bogs down for large (VERY large) numbers. However, that's a completely different subject.

The following code will print the primes from 2 to 100:

Click here for the Primes.java source.

Tricks:

· Don't forget that number++ adds 1 to the value of number and then stores in back in number. It's a shortcut for "number = number + 1".

· We can return from a method at any point, no just at the end. By calling "return" inside the for loop. We don't waste time because it's pointless to keep testing the number once we know it's not a prime number.

This code has the following output:

2 is prime.

3 is prime.

5 is prime.

7 is prime.

11 is prime.

13 is prime.

17 is prime.

19 is prime.

23 is prime.

29 is prime.

31 is prime.

37 is prime.

41 is prime.

43 is prime.

47 is prime.

53 is prime.

59 is prime.

61 is prime.

67 is prime.

71 is prime.

73 is prime.

79 is prime.

83 is prime.

89 is prime.

97 is prime.

2) Write code to find the factorial of a number. The factorial of a number is the product of multiplying the number by every whole number less than the number, all they way down to 1. For example, the factorial of 4 (written "4!") is 4 * 3 * 2 * 1 = 24. 3! = 3 * 2 * 1 = 6.

Answer:

This is a very simple problem. The following code will generate factorials values:

Click here for the Factorial.java source code.

Here is the output:

1! = 1

2! = 2

3! = 6

4! = 24

5! = 120

6! = 720

7! = 5040

8! = 40320

9! = 362880

10! = 3628800

3) Write code to find average of all of the numbers in the scores array. Also, using the names array, print out the names of all of the students whose score is 70 or above. Assume that student's name has the same index as their score.

double scores[] = {50, 100, 75, 81, 90.5, 66, 33, 99};

String names[] = {"David", "Mary", "Joannie", "Nancy",

"Susan", "Elizabeth", "Tommy", "Nicholas"};

Answer:

The following code will do what we want:

Click here for the Scores.java source code.

This solution is a good example of "killing two birds with one stone." We can do both tasks in the same for loop. In computer programs, speed is usually important. One of the major bottlenecks in program speed is looping. Slow programs are always looping through all kinds of data. Conversely, fast programs minimize the amount of looping they need to do.

Here is the output of the program:

Mary (100.0)

Joannie (75.0)

Nancy (81.0)

Susan (90.5)

Nicholas (99.0)

Average: 74.3125