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 • Encapsulation • Classes, Objects, and Polymorphism – Covered in more detail later
Primitives • int, double, boolean, float, double, char… • These are NOT Objects – Can be compared with ==, !=, <=, >=, etc. – Cannot call methods – Each primitive has an associated wrapper class • Auto-boxing/unboxing • Explicit type-casting needs to be done double float long int short byte • Implicit type-casting need not be done byte short int long float double
Objects and Classes and Strings • Objects are handled with a reference address • Two objects should be compared with the .equals() method – Do NOT use == to compare objects • Assignment operators assign references – they do not make separate copies • NullPointerException – always make sure your object is not null before calling methods with it • Constructors should be used to initialize class variables • Calling methods – Need object – Static methods • Can use class name • Cannot access non-static methods/variables inside – Pass by value *always* • Keyword: this
Objects and Classes and Strings • Strings are a type of object – Also should not be compared with == • Can be concatenated with the ‘+’ operator • Important String functions – charAt – indexOf – substring – length
Selection Statements • Modifies the flow of control of the program • if/else construct – Must have a boolean condition to check against – { } are important, but not necessary for one line statements – else branch is optional • switch construct – Multi-way branch which makes a decision based on a char, byte, short, or int – default case – break statement
Repetition Statements • for • while • do-while • Pitfalls – Off-by-one errors – Infinite looping – Overflow
Arrays • Linear collection of data – Can hold primitives or Objects, but must all be of the same type • length tells the number of elements in the array – Member, not a method • Indexed from 0 to length – 1 • Protect against ArrayIndexOutOfBoundsException • Can create multiple dimension arrays • Usually use for-loops to iterate through members of the array
Exceptions • Use a try/catch/finally block to handle exceptions thrown by a program • Use throw statement to notify caller of an error • Do not need to catch RunTimeExceptions – These should be checked for instead • User defined exceptions must extend Exception
File I/O • Many classes – FileInputStream, DataInputStream, FileReader, BufferedReader, Scanner, PrintWriter, DataOutputStream, etc. – ObjectInputStream, ObjectOutputStream • Used to write objects to a file • Any object serialized by a stream must implement Serializable • Which classes are used to write text and which are used to write binary? • Always close files you open
Inheritance and Polymorphism • Differences between abstract classes and interfaces • Polymorphism can simplify code • Extend specific classes from general classes • Use protected keyword to protect information and methods • No need to rewrite methods which are the same as in a parent class • Superconstructor is always called as the first line of constructor
Dynamic Data Structures and Generics • Inner classes • Lists – Node class – Circularly linked lists – Doubly linked lists • Java Collections – Vector – ArrayList – LinkedList
Recursion • Think about what the sub-problem is • Only be concerned with the current level of recursion • Two necessary cases – Base case – Recursive case
GUI and Event-driven Programming • Common classes – JFrame – JPanel – JLabel – JMenu, JMenuItem – JButton • Layouts
Challenges
What is the output? 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()); } }
What is the output? public class A Because this method is static, it does not have { access to non-static class variables. This code will private int x; Not compile because x is non-static. public static int doStuff() { x = 100; x /= 3; x++; return x; } public static void main(String[] args) { System.out.println(A.doStuff()); } }
Types • Given the following classes, which of the following declarations are valid? 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();
Types • Given the following classes, which of the following declarations are valid? 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
Arrays • Write a method which takes in an array of integers and replaces the values of the array with a value c i . Define c i to be the sum of the numbers in indices 0…i in the incoming array. public void cumulativeArray(int[] a) { }
Arrays • Write a method which takes in an array of integers and replaces the values of the array with a value c i . Define c i to be the sum of the numbers in indices 0…i in the incoming array. 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]; }
Linked Lists • Given an appropriate (integer) Node class, write a recursive method which sums up the numbers in the list. public int sumList(Node l) { }
Linked Lists • Given an appropriate (integer) Node class, write a recursive method which sums up the numbers in the list. public int sumList(Node l) { if (l == null) retrurn 0; return l.num + sumList(l.next); }
Strings • Write a recursive method reverse which takes in a String and returns the reverse of the String. You may NOT use the reverse method in the String class, however you may use other methods available to you. public String reverseString(String s) { }
Strings • Write a recursive method reverse which takes in a String and returns the reverse of the String. You may NOT use the reverse method in the String class, however you may use other methods available to you. 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);
Linked Lists • Given an appropriate Node class, write a method which removes every other node from the list, starting with the second. public Node removeEveryOther(Node l) { }
Linked Lists • Given an appropriate Node class, write a method which removes every other node from the list, starting with the second. public Node removeEveryOther(Node l) { Node l1 = l; while (l1 != null && l1.next != null) { l1.next = l1.next.next; l1 = l1.next; } return l; }
Linked Lists • Now write the same method, but recursively. public Node removeEveryOther(Node l) { }
Linked Lists • Now write the same method, but recursively. 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; }
TA Evaluation!
Recommend
More recommend