// Helene Martin, CSE 142 // Several methods that have fencepost/loop-and-a-half problems public class Fencepost { public static void main(String[] args) { printConsonants("We are all sick and ready for the weekend."); printCommas("We are all sick and ready for the weekend."); System.out.println(factors(7)); printPrimes(91); } // prints non-vowel letters from a given phrase // e.g.: vowels -> vwls public static void printConsonants(String phrase) { for (int i = 0; i < phrase.length(); i++) { char c = phrase.charAt(i); //if (c == 'b' || c == 'c' || c == 'd' ..... if (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u') { System.out.print(c); } } System.out.println(); } // prints letters from a given phrase separated by commas // e.g.: vowels -> v, o, w, e, l, s public static void printCommas(String phrase) { System.out.print(phrase.charAt(0)); for (int i = 1; i < phrase.length(); i++) { char c = phrase.charAt(i); System.out.print(", " + c); } System.out.println(); } // counts and returns the factors of a given number public static int factors(int value) { int factors = 0; for (int i = 1; i <= value; i++) { if (value % i == 0) { factors++; } } return factors; } // prints all primes up to and including the max // precondition: max >= 1 public static void printPrimes(int max) { System.out.print(1); for (int i = 2; i <= max; i++) { if (factors(i) == 2) { System.out.print(", " + i); } } } }