Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 160 Practice Final Review. Question 1 class A { public void blah() { System.out.println("a"); } class B extends A { public void blah() { System.out.println("b");

Similar presentations


Presentation on theme: "CSC 160 Practice Final Review. Question 1 class A { public void blah() { System.out.println("a"); } class B extends A { public void blah() { System.out.println("b");"— Presentation transcript:

1 CSC 160 Practice Final Review

2 Question 1 class A { public void blah() { System.out.println("a"); } class B extends A { public void blah() { System.out.println("b"); } class Test { public void printObject(A a) { a.blah(); } public static void main(String[] args) { A a = new A(); B b = new B(); 1:b.blah(); 2:((A)b).blah(); 3:((B)a).blah(); 4:printObject(b); 5:printObject((A)b); } Answers: 1)b 2)b 3)ClassCastException 4)Compile Error 5)Compile Error

3 Question 2 class Point { public int x; public int y; public boolean equals(Object o) { // Fill in this method. It should return true if the // value of o is equal to the value of this, return // false otherwise. Two points should be considered // equal if both their x and y values are the same } public boolean equals(Object o) { boolean toReturn = false; if(o instanceof Point) { Point p = (Point)o; toReturn = (this.x==p.x) &&(this.y==p.y); } return toReturn; }

4 Question 3 public static boolean substring(String a, String b) { boolean toReturn = false; if(a == null || b == null || a.length()==0 || b.length()==0) toReturn = false; else if(a.length() > b.length()) toReturn = false; else { String toCompare = b.substring(0, a.length()); if(a.equals(toCompare)) toReturn = true; else if(a.length() < b.length()) toReturn = substring(a, b.substring(1, b.length()); } return toReturn; }

5 Question 4 public class MyListIterator implements Iterator { private Object[] items; private int curIndex; public MyListIterator(MyStack stack) { items = new Object[stack.size()]; for(int i = stack.size()-1; i > 0; i--) { items[i] = stack.pop(); } for(int i = 0; i < items.length; i++) { stack.push(items[i]); }

6 Question 4 cont’d public boolean hasNext() { return curIndex < items.length; } public Object next() { return items[curIndex++]; } public void remove() { throw new UnsupportedOperationException(); }

7 Question 5 No. If working on a problem where you know n < some number, possible O(n^2) algorithm is faster. Big O notation doesn’t take into account constants in the running time. O(n) algorithm could have a very big constant

8 Question 6 Java Easier –Java comes with very large API –Java has garbage collection –Java has exceptions –Java is object-oriented C Sometimes Better –Garbage collection not suitable for real-time –Takes up less memory – no Virtual Machine –Lower level access

9 Question 7 public class BinaryTree implements BinaryTreeInterface { private BTNode root; public void printTree() { if (root != null) { System.out.println(root.element(); printPostOrder(root.getLeft()); printPreOrder(root.getRight()); }

10 Question 7 cont’d public void printPostOrder (BTNode node) { if (node != null) { printPostOrder(node.getLeft()); printPostOrder(node.getRight()); System.out.println(node.element()); } public void printPreOrder(BTNode node) { if (node != null) { System.out.println(node.element()); printPreOrder(node.getLeft()); printPreOrder(node.getRight()); } } // Ends Class


Download ppt "CSC 160 Practice Final Review. Question 1 class A { public void blah() { System.out.println("a"); } class B extends A { public void blah() { System.out.println("b");"

Similar presentations


Ads by Google