/************************************************************************* * Name: * NetID: * Precept: * * Description: Four methods, each of which produce a string of N * consecutive x's. (e.g., x, xx, xxx, xxxx) *************************************************************************/ public class Repeat { // returns a String of N consecutive x's public static String repeat1(int N) { char[] temp = new char[N]; for (int i = 0; i < N; i++) temp[i] = 'x'; return new String(temp); } // returns a String of N consecutive x's public static String repeat2(int N) { if (N == 0) return ""; else if (N == 1) return "x"; else return repeat2(N / 2) + repeat2(N - (N / 2)); } // returns a String of N consecutive x's public static String repeat3(int N) { if (N == 0) return ""; String temp = repeat3(N / 2); if (N % 2 == 0) return temp + temp; else return temp + temp + "x"; } // returns a String of N consecutive x's public static String repeat4(int N) { String s = ""; for (int i = 0; i < N; i++) s = s + "x"; return s; } public static void main(String[] args) { int N = Integer.parseInt(args[0]); double start, end; String s; // run experiment 1 - invoke repeat1 start = System.currentTimeMillis() / 1000.0; s = repeat1(N); end = System.currentTimeMillis() / 1000.0; StdOut.println("Elapsed time for repeat1() = " + (end - start)); // run experiment 2 - invoke repeat2 start = System.currentTimeMillis() / 1000.0; s = repeat2(N); end = System.currentTimeMillis() / 1000.0; StdOut.println("Elapsed time for repeat2() = " + (end - start)); // run experiment 3 - invoke repeat3 start = System.currentTimeMillis() / 1000.0; s = repeat3(N); end = System.currentTimeMillis() / 1000.0; StdOut.println("Elapsed time for repeat3() = " + (end - start)); // run experiment 4 - invoke repeat4 start = System.currentTimeMillis() / 1000.0; s = repeat4(N); end = System.currentTimeMillis() / 1000.0; StdOut.println("Elapsed time for repeat4() = " + (end - start)); } }