// Earl Bergquist, Garfield High School
//  Strings Review then...
//  Demonstrates fencepost loops

public class StringsFencePostExamples {
	public static void main(String[] args) {
		System.out.println("    isPlural for 'sneakers' then 'sneaker'");
		System.out.println(isPlural("sneakers"));
		System.out.println(isPlural("sneaker"));
		System.out.println("    countSlowly to 10");
		System.out.println(countSlowly(10));
		System.out.println("    Fencepost Solutions for Counting with commas");
		printNumbers(10);
		System.out.println();
		printNumbers2(10);
		System.out.println("\n    Prime Logic");
		System.out.println(isPrime(21));
		System.out.println(isPrime(17));
		System.out.println("    Fencepost Solutions for Counting Primes");
		printPrimes(50);
	}
	
	//s.endsWith("s");
	//s.charAt(s.length() - 1) == 's'
	public static boolean isPlural(String s) {
		return s.charAt(s.length() - 1) == 's';
	}
	
	// Count Slowly inserting "onethousand"
	public static String countSlowly(int a) {
		String result = "";
		for(int i = 1; i <= a; i++) {
			result = result + i + "onethousand";
		}
		return result;
	}
	
	// Prints to the max number provide with a comma between each
	public static void printNumbers(int max) {
		System.out.print("1");
		for(int i = 2; i <= max; i++) {
			System.out.print(", " + i);
		}
	}
	
	// Alternative Method to Prints 1 to the max number provide 
	//    with a comma between each numeral
	public static void printNumbers2(int max) {
		for(int i = 1; i < max; i++) {
			System.out.print(i + ", ");
		}
		System.out.print(max);
	}
	
	// classic cummulative sum!!  To count factors of a number
	public static int countFactors(int num) {
		int factors = 0;
		for(int i = 2; i < num; i++) {
			if(num % i == 0) {
				factors++;
			}
		}
		return factors;
	}
	
	// Returns boolean true if the num is Prime, uses countFactors 
	public static boolean isPrime(int num) {
		return countFactors(num) == 0;
	}
	
	// Prints out the factors up to max, separated by a comma
	//    demonstrating Fence post solution
	public static void printPrimes(int max) {
		System.out.print("2");
		for(int i = 3; i <= max; i++) {
			if(isPrime(i)) {
				System.out.print(", " + i);
			}
		}
	}
}
