import java.util.*; public class Test { public static void main(String[] args) throws java.io.IOException { System.out.println(List.empty.cons("b").cons("a")); List.test(); Object[] ab = {"a", "b"}; Vector v = new ArrayVector(ab); System.out.println(List.fromEnumeration(v.elements())); System.out.println(List.fromVector(v)); int[] ints = {1, 3, 3, 3, 2, 6, 8, 8, 9}; System.out.println(removeDuplicates(ints)); printInts(ints); PrintComponent pc1 = new Printer("What"); PrintComponent pc2 = new SuffixDecorator("?", pc1); PrintComponent pc3 = new ParenDecorator(pc2); pc3.print(); baz(); betweenTest(1, 3, 5); betweenTest(1, 5, 1); betweenTest(3, 1, 5); betweenTest(3, 3, 5); System.out.println(List.fromVector(v).length()); System.out.println(List.empty.length()); System.in.read(); } static void printInts(int[] ints) { for (int i = 0; i < ints.length; i++) { System.out.print(ints[i] + " "); } } /** * problem 8 * @param ints is sorted and has length > 0 * @return the number, N, of distinct values in ints and modifies ints * such that ints[0],...,ints[N-1] contain the distinct elements of ints. */ static int removeDuplicates(int[] ints) { int j = 1; int lastSeen = ints[0]; for (int i = 1; i < ints.length; i++) { if (ints[i] != lastSeen) { ints[j] = ints[i]; j++; lastSeen = ints[i]; } } return j; } /** * problem 2: prints 2,2,14 */ static void baz() { int i = 8, j = 3; System.out.println(i/j+","+i%j+","+(i+j*2)); } /** * problem 3 */ static int between(int a, int b, int c) { if (a < b) return b < c ? b : c; else return a < c ? b : c; } static void betweenTest(int a, int b, int c) { int[] ints = {a, b, c, between(a, b, c)}; System.out.println(ints); } } /** * problem 1 */ class Fooey { // lower cased Class public static void main(String[] gooey) { // added void $total = 5 + 2; // added semicolon System.out.println($total); // added semicolon } } abstract class List { static List empty = new EmptyList(); static void test() { Object[] ab = {"a", "b"}; System.out.println(List.fromArray(ab)); // prints (a b) } abstract boolean isEmpty(); /** * problem 5 */ int length(); List cons(Object obj) { return new Cons(obj, this); } /** * problem 4 * @return a list containing the elements of array in the same order. */ static List fromArray(Object[] array) { List ls = List.empty; for (int i = array.length-1; i >= 0; i--) { ls = ls.cons(array[i]); } return ls; } /** * problem 4 * @return a list of the elements of enum in the same order. */ static List fromEnumeration(Enumeration enum) { if (enum.hasMoreElements()) { return new Cons(enum.nextElement(), fromEnumeration(enum)); } else return empty; } /** * problem 4 * @return a list of the elements of vec in the same order. */ static List fromVector(Vector vec) { return List.fromEnumeration(vec.elements()); } } class EmptyList extends List { boolean isEmpty() { return true; } /** * problem 5 */ int length() { return 0; } public String toString() { return "()"; } } class Cons extends List { Object head; List tail; Cons(Object head, List tail) { this.head = head; this.tail = tail; } boolean isEmpty() { return false; } /** * problem 5 */ int length() { return 1 + tail.length(); } public String toString() { return "(" + head + " . " + tail + ")"; } } interface PrintComponent { void print(); } class Printer implements PrintComponent { Object obj; Printer(Object obj) { this.obj = obj; } public void print() { System.out.print(obj); } } class PrintDecorator implements PrintComponent { PrintComponent pc; PrintDecorator(PrintComponent pc) { this.pc = pc; } public void print() { pc.print(); } } class SuffixDecorator extends PrintDecorator { Object suffix; SuffixDecorator(Object suffix, PrintComponent pc) { super(pc); this.suffix = suffix; } public void print() { super.print(); System.out.print(suffix); } } class ParenDecorator extends PrintDecorator { ParenDecorator(PrintComponent pc) { super(pc); } public void print() { System.out.print("("); super.print(); System.out.print(")"); } } class ArrayVector extends Vector { ArrayVector(Object[] array) { for (int i = 0; i < array.length; i++) { this.addElement(array[i]); } } } /* Problem 6: (I wasn't expecting an answer this formal, but this shows how to be precise about such things.) Let < be the total order relation on protection modifiers defined as: private < package < protected < public. Also let P1 and P2 be the protection modfiers of methods M1 and M2, resp. Then M1 can override M2 only if P2 <= P1. Assume on the contrary that P1 < P2 and consider an invocation of method m (the name of M1 and M2) via an instance of the class of M1 in a context in which the instance has the type of M2, as in this program skeleton: class C2 { M2 } class C1 extends C2 { M1 } ... C2 c = new C1(); c.m(); ... Now if c.m() is in a context in which P2 access is possible, but not P1 access, the access will be allowed (since at this point c has type C2). But this violates the P1 access restriction that the author of class C1 declared. */ /** * problem 7 */ class SingletonFoo extends Foo { static SingletonFoo unique = null; private SingletonFoo() { super(); } static SingletonFoo singleton() { if (unique == null) unique = new SingletonFoo(); return unique; } }