CS 160, Spring 2015
Programming Assignment P5
Control Loops
Programming due Monday, Mar. 2 at 6:00pm; late deadline Mar. 2 at 11:59pm.
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 P5 project in Eclipse then write a class P5 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) {
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) {
// add code here
}
// Method to remove vowels from a string
public static String removeVowels(String input) {
// add code here
}
// Method to generate and print the value of a number raised to an exponent
public static double evaluateExponent(double number, int exponent) {
// add code here
}
// Method to find and print the minimum/maximum/mean of a set of positive numbers
public static void computeStatistics(int sentinel) {
// add code here
}
Instructions
Implement each of the methods shown above, according to these instructions.
printPrimes
- Inside the method iterate over the specified (inclusive) range using a for loop.
- Call the isPrime method for each number in the range.
- If isPrime returns true for the number, print the number followed by a comma and space.
- It is expected that the last prime number will be followed by a comma and space.
- After the loop has completed, print a newline.
removeVowels
- Declare a string variable for the return value, and initialize it to "".
- Use a for loop to iterate over all the characters in the supplied string.
- Use a conditional or switch statement to check whether the character is a vowel.
- The vowels are a/e/i/o/u, uppercase or lowercase.
- If it is a vowel, do nothing, otherwise add the character to the return string.
- After the loop has completed, return the string.
evaluateExponent
- Declare a double variable called result and initialize it to 1.0;
- If the exponent parameter is 0, return the result immediately.
- You do not need to handle a negative exponent value.
- Otherwise declare an integer variable called loop and initialize it to 0.
- Write a do while loop, as follows:
- Inside the loop:
- Multiply the result times the number.
- Increment the loop variable.
- The terminating condition is when loop is equal to exponent.
- After the loop has completed, return the result.
computeStatistics
- Declare and initialize a Scanner to read from the keyboard.
- Declare an integer variable called value and initialize it to 0.
- Declare an integer variable called count and initialize it to 0.
- Declare a double variable called mean and initialize it to 0.0.
- Declare an integer variable called minimum and set it equal to Integer.MAX_VALUE.
- Declare an integer variable called maximum and set it equal to Integer.MIN_VALUE;
- Write a while loop that never terminates!
- 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.
- After the loop has completed, divide the mean by the count.
- Print "Count: " and the value of count, with a newline.
- Print "Average: " and the value of mean, with a newline.
- Print "Maximum: " and the value of maximum, with a newline.
- 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:
1, 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 P5.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
Grading Criteria
- 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).
© 2015 CS160 Colorado State University. All Rights Reserved.