// Allison Obourn, CSE 142 // several methods that have fencepost/loop-and-a-half problems public class Fencepost { public static void main(String[] args) { printLetters("hello world"); System.out.println(countFactors(20)); printPrimes(50); } // takes a String and prints out its consonants public static void printConsonants(String str) { for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u') { System.out.print(c); } } } // takes a String and prints out its letters separated by commas public static void printLetters(String str) { System.out.print(str.charAt(0)); for (int i = 1; i < str.length(); i++) { char c = str.charAt(i); System.out.print(", " + c); } System.out.println(); } // prints all the primes up to the passed in max, // in increasing order with commas between them public static void printPrimes(int max) { if (max >= 2) { System.out.print(2); for (int i = 3; i <= max; i++) { if (countFactors(i) == 2) { System.out.print(", " + i); } } } } // returns the number of factors that the passed // in value has public static int countFactors(int value) { int factors = 0; for (int i = 1; i <= value; i++) { if (value % i == 0) { factors++; } } return factors; } }