CS 180 Final Exam Review
- WHEN
– Saturday, December 15th. 10:20 -12 :20 am
- WHERE
CS 180 Final Exam Review WHEN Saturday, December 15th. 10:20 -12 - - PowerPoint PPT Presentation
CS 180 Final Exam Review WHEN Saturday, December 15th. 10:20 -12 :20 am WHERE PHYS 112 Object-Oriented Design Methods Extract small parts of a program and calculations which will be performed multiple times
– Do NOT use == to compare objects
separate copies
before calling methods with it
– Need object – Static methods
– Pass by value *always*
public class A { private int x; public static int doStuff() { x = 100; x /= 3; x++; return x; } public static void main(String[] args) { System.out.println(A.doStuff()); } }
public class A { private int x; public static int doStuff() { x = 100; x /= 3; x++; return x; } public static void main(String[] args) { System.out.println(A.doStuff()); } }
Because this method is static, it does not have access to non-static class variables. This code will Not compile because x is non-static.
public interface I {…} public interface J extends I {…} public interface K {…} public abstract class A {…} public class B extends A {…} implements J, K public class C extends B {…} public class D extends A {…} implements I A a = new B(); B b = new J(); C c = new B(); B b = new C(); I i = new A(); I i = new B();
public interface I {…} public interface J extends I {…} public interface K {…} public abstract class A {…} public class B extends A {…} implements J, K public class C extends B {…} public class D extends A {…} implements I A a = new B(); valid – B is a subclass of A B b = new J(); invalid – cannot instantiate interfaces C c = new B(); invalid – not all B is the superclass of C B b = new C(); valid – C is a subclass of B I i = new A(); invalid – A does not implement I I i = new B(); valid – A implements J, and J is a type of I
public void cumulativeArray(int[] a) { }
public void cumulativeArray(int[] a) { if (a.length <= 1) return; for (int k=1; k<a.length; k++) a[k] = a[k] + a[k-1]; }
public int sumList(Node l) { }
public int sumList(Node l) { if (l == null) retrurn 0; return l.num + sumList(l.next); }
public String reverseString(String s) { }
public String reverseString(String s) { if (s == null || s.length() <= 1) return s; if (s.length() == 2) return “” + s.charAt(1) + s.charAt(0); return s.charAt(s.length()-1) + reverseString(s.substring(1, s.length()-1)) + s.charAt(0);
public Node removeEveryOther(Node l) { }
public Node removeEveryOther(Node l) { Node l1 = l; while (l1 != null && l1.next != null) { l1.next = l1.next.next; l1 = l1.next; } return l; }
public Node removeEveryOther(Node l) { }
public Node removeEveryOther(Node l) { // base case if (l == null || l.next == null) return l; // recursive case Node l1 = removeEveryOther(l.next.next); l.next = l1; return l; }