lab 10 nested loops

Upload: aqil-shamsi

Post on 09-Oct-2015

10 views

Category:

Documents


0 download

DESCRIPTION

Lab 10 Nested Loops

TRANSCRIPT

Manual

Java Lab Manual

Purpose of this lab session

In this lab you will print a table using a single loop. In addition, nested loops are an important tool in programming. Some tables can be printed using a nested loop but, there are many other uses. Since you can't write a nested loop unless understand how it is executed, you will practice doing a walk through of some nested loops. To prepare for this lab

Read Wu: Chapter 6 Read through this laboratory session

Using your memory device, copy the files Primes.java and NestedLoops.java from http://www.mscs.mu.edu/~marian/60/Labs/lab10To Complete this lab

This is an individual lab. You may ask the lab tutor for help and you may consult with your neighbor if you are having difficulties. In this lab, you will modify the and add to the two copied programs. When done with each program, do a final run of each program and place a copy of the printed output in each file. When you have completed the lab, hand in your printouts to the lab tutor.10.1 Finding Prime NumbersThe positive factors, or divisors, of 2 are 1 and 2 .

The positive factors, or divisors, of 6 are 1, 2, 3, and 6.

The positive factors, or divisors, of 9 are 1, 3 and 9.

Step 1: List the factors of 7 ________________________________________________

List the factors of 12 ________________________________________________

List the factors of 13 ________________________________________________

List the factors of 49 ________________________________________________

Defintion: A prime number is a positive integer that has two distinct factors, 1 and itself.

The number one is not a prime number since it does not have two distinct factors. The number two is the smallest prime number and the only even number that is a prime.

Circle the numbers that are prime numbers:

-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

int number, is divisible by int divisor if number % divisor == 0.

For example: since 12 % 4 == 0 is true, 12 is divisible by 4

since 12 % 5 == 0 is false, 12 is not divisible by 5

Therefore, to determine if a positive integer number is a prime number, check if any of the integers between 2 and (number 1) are divisors, or factors, of number.The following algorithm, described using pseudocode, determines if int number is a prime number.

assume boolean isPrime = true

for factor between values of 2 and (number -1)

if (number % divisor == 0)

isPrime = false

Notice the following about the above pseudocode:

begin by assuming that num is a prime number: boolean isPrime = true; Since all numbers are divisible by 1 and by themselves, only the numbers between 2 and (number 1) are checked for divisibility into number once a factor of num is found, isPrime is false and the existence of additional divisors is irrelevant.

Step 1:Open the file Primes.java and read it for understanding.class Primes

{

public static void main(String[] args)

{

int number = -1;

do

{

System.out.println(number + " is a prime is " + Primes.isPrime(number));

number++;

}while(number < 26); }

private static boolean isPrime(int number)

{

return false;

}

}The method

private static boolean isPrime(int number)

returns true if number is a prime number and false otherwise. In order for the above file to compile, the dummy method isPrime must return a boolean value, I chose false.

Compile and run the program. For which of the test numbers is true printed?

__________________________________________________________________________________________

__________________________________________________________________________________________

Step 2: Let's begin to write the isPrime method. Initially, we assume number is a prime number by assigning true to isPrime. If number is less than two, false is assigned to isPrime. If number is greater than two, a loop that tries to find a divisor of number is needed. Notice that it is not necessary to explicitly check if number is equal to two since isPrime is initially set to true and will not be changed. Revise the isPrime method as shownprivate static boolean isPrime(int number)

{

boolean isPrime = true;

if(number < 2)

isPrime = false;

else if(number > 2)

{

//look for a divisor of number

//if a divisor is found, set isPrime to false

}

return isPrime;

}

Compile and run the program. For which of the numbers tested in the main method, does isPrime return true?

__________________________________________________________________________________________

__________________________________________________________________________________________

Step 3: Add the loop that looks for a divisor of number. When a divisor is found, isPrime is set to false and, for efficiency, the loop should stop. When a break statement is executed inside of a loop, the loop terminates and the next statement following the loop is executed.

private static boolean isPrime(int number)

{

boolean isPrime = true;

if(number < 2)

isPrime = false;

else if(number > 2)

{

for(int divisor = 2; divisor < number; divisor++)

{

if(number % divisor == 0) { isPrime = false; break;

} }

}

return isPrime; //when the loop ends, isPrime is returned

}

Compile and run the program. For which of the numbers tested in the main method, does isPrime return true?

__________________________________________________________________________________________

__________________________________________________________________________________________

We now have a working method. However, that does not mean that we are done. You should always re-read your code to see if improvements can be made to its appearance, its readability, its logic or its efficiency. What improvements can be made to the isPrime method?

Since two is the only even prime number, we could make a special case for even numbers, so that only odd numbers are checked by the loop.

If the loop no longer needs to check for divisibility by two, it no longer needs to check for divisibility by any other even number. That is, we only need to check for divisibility by the odd numbers 3, 5, 7, ...

The number 53 is a prime number. How many possible divisors must be checked. Do we need to check all of the odd numbers between 3 and 52, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 24, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, and 51? The answer is no, but where can we stop?

The factors of 36, are 1, 2, 3, 4, 6, 9, 12, 18 and 36. Note:

1 x 36 = 36

2 x 18 = 36

3 x 12 = 36

4 x 9 = 36

6 x 6 = 26

The factors of 49, are 1, 7 and 49. Note:

1 x 49 = 49

7 x 7 = 49

Note that for each example, half of the factors are greater than the square root and the other half are less than the

square root.

In addition, if a divisor of the number (other than 1 or itself) does exist, the loop will find the smaller number in the pair of factors first. This is sufficient to determine that the number is not prime.

Step 4: Revise the isPrime method to reflect the suggested improvements.

private static boolean isPrime(int number)

{

boolean isPrime = true;

if(number < 2)

isPrime = false;

else if(number > 2)

{

if(number % 2 == 0) //even numbers are not prime

isPrime = false;

else

{

double sqrt = Math.sqrt(number); //evaluate once

for(int divisor = 3; isPrime && divisor