CSU Banner

CS 163/164, Spring 2018

Programming Assignment - P5

Control Loops

Due Monday, Feb. 19th at 6:00 pm

Late Tuesday, Feb. 20th at 8:00 am


Objectives of this Assignment

  1. To practice for, while, and do while loops.

Description

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.

Instructions

Create a P5 project in Eclipse then write a class P5 with a main method, and copy the following code into the class (not in the main method).
// Method to check whether an integer is prime.
public static boolean isPrime(int number) {

    // DO NOT CHANGE THIS CODE!
    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
}
Next, 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/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:
  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:
  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 to 1 decimal place, 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:

Testing code

This code you can copy into your main method for testing. It is fine to leave the testing code in P5.java when you submit.
    // 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:

Grading Criteria


Submit P5.java to the Checkin tab.


CS Banner
CS Building