The end is near ObjectOriented Design Methods Extract

The end is near…

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 • Null. Pointer. Exception – 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 – char. At – index. Of – 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 Array. Index. Out. Of. Bounds. Exception • 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 Run. Time. Exceptions – These should be checked for instead • User defined exceptions must extend Exception

File I/O • Many classes – File. Input. Stream, Data. Input. Stream, File. Reader, Buffered. Reader, Scanner, Print. Writer, Data. Output. Stream, etc. – Object. Input. Stream, Object. Output. Stream • 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 – Array. List – Linked. List

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, JMenu. Item – JButton • Layouts

Challenges

What is the output? public class A { private int x; public static int do. Stuff() { x = 100; x /= 3; x++; return x; } public static void main(String[] args) { System. out. println(A. do. Stuff()); } }

What is the output? public class A { private int x; 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 static int do. Stuff() { x = 100; x /= 3; x++; return x; } public static void main(String[] args) { System. out. println(A. do. Stuff()); } }

Types • Given the following classes, which of the following declarations are valid? public public A B C B I I I K a b c b i i i k = = = = interface I {…} interface J extends I {…} interface K {…} abstract class A {…} class B extends A {…} implements J, K class C extends B {…} class D extends A {…} implements I new new B(); J(); B(); C(); A(); B(); D(); C();

Types • Given the following classes, which of the following declarations are valid? public public A B C B I I I K a b c b i i i k = = = = interface I {…} interface J extends I {…} interface K {…} abstract class A {…} class B extends A {…} implements J, K class C extends B {…} class D extends A {…} implements I new new B(); J(); B(); C(); A(); B(); D(); C(); valid – invalid – valid – B – – C – A D C is a subclass of A cannot instantiate interfaces not all B is the superclass of C is a subclass of B A does not implement I implements J, and J is a type of I impelements I extends B which implements K

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 cumulative. Array(int[] a) { }

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 cumulative. Array(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 sum. List(Node l) { }

Linked Lists • Given an appropriate (integer) Node class, write a recursive method which sums up the numbers in the list. public int sum. List(Node l) { if (l == null) retrurn 0; return l. num + sum. List(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 reverse. String(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 reverse. String(String s) { if (s == null || s. length() <= 1) return s; if (s. length() == 2) return “” + s. char. At(1) + s. char. At(0); return } s. char. At(s. length()-1) + reverse. String(s. substring(1, s. length()-1)) + s. char. At(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 remove. Every. Other(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 remove. Every. Other(Node l) { Node l 1 = l; while (l 1 != null && l 1. next != null) { l 1. next = l 1. next; l 1 = l 1. next; } return l; }

Linked Lists • Now write the same method, but recursively. public Node remove. Every. Other(Node l) { }

Linked Lists • Now write the same method, but recursively. public Node remove. Every. Other(Node l) { // base case if (l == null || l. next == null) return l; // recursive case Node l 1 = remove. Every. Other(l. next); l. next = l 1; return l; }

Recursion • Given an appropriate node class, write a recursive method which reverses a singly linked list. public Node reverse(Node l) { }

Recursion • Given an appropriate node class, write a recursive method which reverses a singly linked list. public Node reverse(Node l) { if (l == null || l. next == null) return l; Node restreversed = reverse(l. next); l. next = l; l. next = null; return restreversed; }

Linked Lists • Given an appropriate Node class, one way to detect a cycle in your list is by moving two pointers around the list, one index at a time and the other two indices at a time. Write a method which determines whether or not there is a cycle in a list. public boolean has. Cycle(Node l) { }

Linked Lists • Given an appropriate Node class, one way to detect a cycle in your list is by moving two pointers around the list, one index at a time and the other two indices at a time. Write a method which determines whether or not there is a cycle in a list. public boolean has. Cycle(Node l) { Node l 1 = l; Node l 2 = l; while (l 1 != null && l 2 != null & l 1. next != null && l 2. next != null) { l 1 = l 1. next; l 2 = l 2. next; if (l 1 == l 2) return true; } return false; }
- Slides: 33