Static variables, objects


public class ReviewOne {
    public static void main(String [] args) {
	ReviewOneObject r1 = new ReviewOneObject(5, 2);
	r1.method1(0);
	r1.method1(2);
	r1 = new ReviewOneObject(3, 5);
	r1.method1(3);
	ReviewOneObject r2 = new ReviewOneObject(1, 3);
	r2.method1(1);
	r1.printArr();

	r2.method2(3);
	r1.method1(1);
	r2 = r1;
	r2.method1(0);
	r2 = new ReviewOneObject(4, 1);
	r2.printArr();
    }
}


public class ReviewOneObject {
    private static int [] staticArr;
    private int instanceVar;

    public ReviewOneObject(int n, int value) {
	if (staticArr == null) {
	    staticArr = new int[n];
	}
	instanceVar = value;
    }

    // can we make this method static?
    public void method1(int i) {
	if (i < staticArr.length) {
	    staticArr[i] = instanceVar;
	} else {
	    System.out.println("Out of range");
	}
    }

    public void method2(int n) {
	staticArr = new int[n];
    }

    public static void printArr() {
	for (int i = 0; i < staticArr.length; ++i) {
	    System.out.print(staticArr[i] + " ");
	}
	System.out.println();
    }
	
}

Exceptions



public class ReviewTwo {
    public static void main(String [] args) {
	int [] numbers = {3, 6, 1, 2, 7};
	try {
	    for (int i = 0; i < numbers.length; ++i) {
		try {
		    method(numbers[i]);
		} catch (ReviewTwoExceptionToo e) {
		    System.out.println(e);
		}
	    }
	} catch (ReviewTwoException e) {
	    System.out.println(e);
	}
    }

    public static void method (int n) throws ReviewTwoException {
	if (n % 3 == 0) throw new ReviewTwoExceptionToo(n);
	else if (n % 2 == 0) throw new ReviewTwoException(n);
	else System.out.println("I am happy!");
    }
}



public class ReviewTwoException extends Exception {

    public ReviewTwoException(int n) {
	super("invalid value " + n);
    }

}


public class ReviewTwoExceptionToo extends ReviewTwoException {

    public ReviewTwoExceptionToo(int m) {
	super(m * m);
    }

}

Recursive methods

What does the method goFigure() do for the tree constructed in the TestIntBST class?

public class IntNode {
    private int data;
    private IntNode left;
    private IntNode right;

    public IntNode(int d) {
	data = d;
    }
    
    public int getData() {
	return data;
    }

    public IntNode getLeft() {
	return left;
    }

    public IntNode getRight() {
	return right;
    }

    public void setLeft(IntNode theleft) {
	left = theleft;
    }

    public void setRight(IntNode theright) {
	right = theright;
    }   

    public int countOdd() {
	int thisOdd = 0;
	int leftOdd = 0, rightOdd = 0;
	if (data % 2 == 1) thisOdd = 1;
	if (left != null) leftOdd = left.countOdd();
	if (right != null) rightOdd = right.countOdd();
	return thisOdd + leftOdd + rightOdd;
    }

    public int goFigure() {
	if (data % 2 == 0 && left != null) return 1 + left.goFigure();
	else if (data % 2 == 1 && right != null) return 1 + right.goFigure();
	else return 1;
    }
}


public class IntBST {
    IntNode root;

    public IntBST() {
	// do nothing
    }

    public IntNode getRoot() {
	return root;
    }

    // returns the reference to the node if there is a node with this int
    // otherwise returns null
    public IntNode search(int data) {
	IntNode node = root;
	while (node != null) {
	    if (data == node.getData()) {
		return node;
	    } else if (data < node.getData()) {
		node = node.getLeft();
	    } else {
		node = node.getRight();
	    }
	}
	return node;
    }

    public IntNode insert(int data) {
	IntNode newNode = new IntNode(data);
	if (root == null) {
	    root = newNode;
	    return root;
	} 
	IntNode current = root;
	IntNode previous = null;
	
	// finding the place for the element
	while (current != null) {
	    if (data == current.getData()) {
		System.out.println("Inserting the node that's already there");
		return null;
	    } else if (data < current.getData()) {
		previous = current;
		current = current.getLeft();
				
	    } else {
		previous = current;
		current = current.getRight();
	    }
	}
	if (data == previous.getData()) {
	    System.out.println("Inserting the node that's already there");
	    return null;
	} else if (data < previous.getData()) {
	    previous.setLeft(newNode); 
	} else {
	    previous.setRight(newNode);
	}
	return newNode;
    }

    public int countOdd() {
	if (root == null) return 0;
	return root.countOdd();
    }

    public int goFigure() {
	return root.goFigure();
    }
}


public class TestIntBST {
    public static void main(String [] args) {
	IntBST bst = new IntBST();
	bst.insert(5);
	bst.insert(4);
	bst.insert(9);
	bst.insert(7);
	bst.insert(3);
	bst.insert(6);

	IntNode result = bst.search(7);
	if (result != null) {
	    System.out.println("found " + result.getData());
	} else {
	    System.out.println(7 + " not found.");
	}
	
	result = bst.search(1);
	if (result != null) {
	    System.out.println("found " + result.getData());
	} else {
	    System.out.println(1 + " not found.");
	}

	result = bst.search(3);
	if (result != null) {
	    System.out.println("found " + result.getData());
	} else {
	    System.out.println(6 + " not found.");
	}

	System.out.println(bst.countOdd());
	System.out.println(bst.goFigure());
    }
}