# CS 160, Summer 2016 Programming Assignment P4 Control Loops

### Instructions

This assignment is a practice session for writing loops. Computers are outstanding at doing repetitive tasks, thus loops are very common in all types of code. We will practice loops on the characters in a string in recitation this week, and this assignment will allow you use loops in more creative ways. For this assignment, you must follow directions exactly. Create a P4 project in Eclipse then write a class P4 with a main method, and copy the following code below the main method:
```
// Method to check whether an integer is prime.
public static boolean isPrime(int number) {
if(number < 2) return false;
for(int i=2;i<=number/2;i++)
if(number%i==0)
return false;
return true;
}

// Method to print primes in a specified range
public static void printPrimes(int start, int end) {
}

// Method to remove vowels from a string
public static String removeVowels(String input) {
}

// Method to generate and print the value of a number raised to an exponent
public static double evaluateExponent(double number, int exponent) {
}

// Method to find and print the minimum/maximum/mean of a set of positive numbers
public static void computeStatistics(int sentinel) {
}

```

### Instructions

Implement each of the methods shown above, according to these instructions.

printPrimes
1. Inside the method iterate over the specified (inclusive) range using a for loop.
2. Call the isPrime method for each number in the range.
3. If isPrime returns true for the number, print the number followed by a comma and space.
4. It is expected that the last prime number will be followed by a comma and space.
5. After the loop has completed, print a newline.
removeVowels
1. Declare a string variable for the return value, and initialize it to "".
2. Use a for loop to iterate over all the characters in the supplied string.
3. Use a conditional or switch statement to check whether the character is a vowel.
4. The vowels are 'a','e','i','o', and 'u', uppercase or lowercase.
5. If it is a vowel, do nothing, otherwise add the character to the return string.
6. After the loop has completed, return the string.
evaluateExponent
1. Declare a double variable called result and initialize it to 1.0;
2. If the exponent parameter is 0, return the result immediately.
3. You do not need to handle a negative exponent value.
4. Otherwise declare an integer variable called loop and initialize it to 0.
5. Write a do while loop, as follows:
6. Inside the loop:
• Multiply the result times the number.
• Increment the loop variable.
• The terminating condition is when loop is equal to exponent.
7. After the loop has completed, return the result.
computeStatistics
1. Declare and initialize a Scanner to read from the keyboard.
2. Declare an integer variable called value and initialize it to 0.
3. Declare an integer variable called count and initialize it to 0.
4. Declare a double variable called mean and initialize it to 0.0.
5. Declare an integer variable called minimum and set it equal to Integer.MAX_VALUE.
6. Declare an integer variable called maximum and set it equal to Integer.MIN_VALUE;
7. Write a while loop that never terminates!
8. Inside the loop:
• Read in an integer value from the keyboard, without prompting.
• If the value equals the sentinel (for example, -1), exit the loop with a break statement.
• If value is smaller than minimum, assign the value to minimum.
• If value is larger than maximum, assign the value to maximum.
• Add the value to the mean.
• Increment the count.
9. After the loop has completed, divide the mean by the count.
10. Print "Count: " and the value of count, with a newline.
11. Print "Average: " and the value of mean, with a newline.
12. Print "Maximum: " and the value of maximum, with a newline.
13. Print "Minimum: " and the value of minimum, with a newline.

### Testing

You should test your code by calling each of the methods in main. We have provided a minimal test and the output below:

#### TEST CODE

This code you can copy into main:
```    // Preliminary testing
printPrimes(1, 50);
System.out.println(removeVowels("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
double result = evaluateExponent(2.0,16);
System.out.println("2.0 to the 16 = " + result);
computeStatistics(-1);
```

#### SAMPLE OUTPUT

User input is shown in green:
```2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
BCDFGHJKLMNPQRSTVWXYZ
2.0 to the 16 = 65536.0
10
20
30
40
50
60
-1
Count: 6
Average: 35.0
Maximum: 60
Minimum: 10
```

### Specifications

Your program must meet the following specifications:
• Work on your own, as always.
• The name of the source code file must be exactly P4.java.
• Assignments should be implemented using Eclipse.
• Assignments should be implemented using Java 1.5 or 1.6 or 1.7 or 1.8.
• Make sure your code runs on machines in the COMCS 120 lab.
• Submit your program to the Checkin tab as you were shown in the recitation.
• Read the syllabus for the late policy.
• We will be checking programs for plagiarism, so please don't copy from anyone else.
• HINT: Here is a rough count of lines for each method, including lines with curly braces, print statements, loops, etc.:
• printPrimes - 4 lines, including for loop
• removeVowels - 13 lines, including switch statement
• evaluateExponent - 19 lines, with variable declarations
• computeStatistics - 21 lines, with print statements

• 100 points for perfect submission.
• 0 points for no submission, will not compile, submitted class file, etc.
• The preliminary testing for this assignment is limited to the following:
• Preliminary Tests
• compileTest: checks that program compiles. (0 points)
• Note: The compile test will pass only if you implement all methods with the correct signature.
• test1: Tests printPrimes with values selected by the instructor. (25 points)
• Final Tests
• test2: Tests removeVowels with strings selected by one of the teaching assistants. (25 points)
• test3: Tests evaluateExponent with a maximum selected by committee. (25 points)
• test4: Tests computeStatistics with a set of values from a random passerby. (25 points)
• Final grading includes the preliminary tests.
Submit your program to the Checkin tab on the course website, as you were shown in the recitation, and read the syllabus for the late policy (if necessary).