CS 180 Final Exam Review WHEN Saturday, December 15th. 10:20 -12 - - PowerPoint PPT Presentation

cs 180 final exam review
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS 180 Final Exam Review

  • WHEN

– Saturday, December 15th. 10:20 -12 :20 am

  • WHERE

– PHYS 112

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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
slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

Repetition Statements

  • for
  • while
  • do-while
  • Pitfalls

– Off-by-one errors – Infinite looping – Overflow

slide-8
SLIDE 8

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
  • f the array
slide-9
SLIDE 9

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

slide-10
SLIDE 10

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
slide-11
SLIDE 11

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
  • f constructor
slide-12
SLIDE 12

Dynamic Data Structures and Generics

  • Inner classes
  • Lists

– Node class – Circularly linked lists – Doubly linked lists

  • Java Collections

– Vector – ArrayList – LinkedList

slide-13
SLIDE 13

Recursion

  • Think about what the sub-problem is
  • Only be concerned with the current level of

recursion

  • Two necessary cases

– Base case – Recursive case

slide-14
SLIDE 14

GUI and Event-driven Programming

  • Common classes

– JFrame – JPanel – JLabel – JMenu, JMenuItem – JButton

  • Layouts
slide-15
SLIDE 15

Challenges

slide-16
SLIDE 16

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()); } }

slide-17
SLIDE 17

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()); } }

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.

slide-18
SLIDE 18

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();

slide-19
SLIDE 19

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

slide-20
SLIDE 20

Arrays

  • Write a method which takes in an array of integers and

replaces the values of the array with a value ci. Define ci to be the sum of the numbers in indices 0…i in the incoming array.

public void cumulativeArray(int[] a) { }

slide-21
SLIDE 21

Arrays

  • Write a method which takes in an array of integers and

replaces the values of the array with a value ci. Define ci 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]; }

slide-22
SLIDE 22

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) { }

slide-23
SLIDE 23

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); }

slide-24
SLIDE 24

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) { }

slide-25
SLIDE 25

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);

slide-26
SLIDE 26

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) { }

slide-27
SLIDE 27

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; }

slide-28
SLIDE 28

Linked Lists

  • Now write the same method, but recursively.

public Node removeEveryOther(Node l) { }

slide-29
SLIDE 29

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; }

slide-30
SLIDE 30

TA Evaluation!