Upload
evan-montgomery
View
221
Download
0
Embed Size (px)
DESCRIPTION
3 Console I/O private static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); private static PrintWriter stdOut = new PrintWriter(System.out, true); private static PrintWriter stdErr = new PrintWriter(System.err, true); true, autoflush
Citation preview
Object-Oriented Programming (Java)
Review 2014
2
Unit 1 Class Design
• Basic Console I/O• StringTokenizer• Exception• UML class diagram
3
Console I/O
• private static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
• private static PrintWriter stdOut = new PrintWriter(System.out, true);
• private static PrintWriter stdErr = new PrintWriter(System.err, true);
true, autoflush
4
StringTokenizer
• Import java.util.*;
• StringTokenizer tokenizer = new StringTokenizer(data, "_");
• tokenizer.countTokens(); //number of tokens
• while(tokenizer.hasMoreTokens()) {String token = tokenizer.nextToken();
} // traverse each token
5
Exception (checked/unchecked)
try { stdErr.print("Enter an integer > "); stdErr.flush(); // print without auto flush return Integer.parseInt(stdIn.readLine()); } catch (IOException ioe) { ioe.printStackTrace(); System.exit(1); // terminate the program} catch (NumberFormatException nfe) { stdErr.println("Invalid number format"); }
6
UML class diagram (Inheritance/Association)
Dashed line: implements
Solid line: extends
7
Unit 2 Class Implementation
• equals()/toString()• Vector/ArrayList• JDK 5
– Vector<String>– ArrayList<String>– For each loop (Iterable interface/iterator method)
• JUnit• Design Pattern
8
Vector and Iterator
9
methods defined in class Vector• Vector(). Constructs an empty collection. • int size(). Returns the number of objects in the collection. • boolean isEmpty(). Determines if there are no objects in the collection. • boolean contains(Object elem). Determines if the specified object is an element
of the collection (as determined by the method equals). • boolean add(E o). Appends the specified object to the end of the collection. • void add(int index, E element). Inserts the specified object at the specified index
position, shifting any subsequent elements to the right (adds one to their indices). • E get(int index). Returns the object at the specified position. • public E set(int index, E element). Replaces the element at the specified index
position with the specified object. • public boolean remove(Object o). Removes the first occurrence of the specified
object (using method equals), shifting any subsequent elements to the left (subtracts one from their indices).
• E remove(int index). Returns the object at the specified position after first removing it from the collection and shifting any subsequent elements to the left (subtracts one from their indices).
10
Methods Defined in Class ArrayList
• ArrayList(). Constructs an empty collection. • int size(). Returns the number of objects in the collection. • boolean isEmpty(). Determines if there are no objects in the collection. • boolean contains(Object elem). Determines if the specified object is an
element of the collection (as determined by the method equals). • boolean add(E o). Appends the specified object to the end of the collection. • void add(int index, E element). Inserts the specified object at the specified
index position, shifting any subsequent elements to the right (adds one to their indices).
• E get(int index). Returns the object at the specified position. • public E set(int index, E element). Replaces the element at the specified index
position with the specified object. • public boolean remove(Object o). Removes the first occurrence of the
specified object (using method equals), shifting any subsequent elements to the left (subtracts one from their indices).
• E remove(int index). Returns the object at the specified position after first removing it from the collection and shifting any subsequent elements to the left (subtracts one from their indices).
11
import java.util.*;public class Client { private String name; private Vector accounts; public Client(String initialName) { name = initialName; accounts = new Vector(); }
public void addAccount(BankAccount bankAccount) { accounts.add(bankAccount); }
public Iterator getAccountIterator() { return accounts.iterator(); }
public int getNumberOfAccounts() { return accounts.size(); }}
See: LibrarySystem
12
JDK 5public class Client implements Iterable<BankAccount> {
…private Vector<BankAccount> accounts;
public Client(String initialName) {
accounts = new Vector<BankAccount>();}
public Iterator<BankAccount> iterator() { return accounts.iterator();
}…
}
for (BankAccount account : client) { totalBalance += account.getBalance(); }
13
Creating a test class in JUnit• Define a subclass of TestCase
• Override the setUp() method to initialize object(s) under test.
• Override the tearDown() method to release object(s) under test.
• Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.
14
JUnit Methods
• assertEquals(x, y) – Test passes if x and y are equal– x and y can be primitives or any type with an appropriate equals
method– Three argument versions exist for floating point numbers
• assertFalse(b) – Test passes if boolean value b is false• assertTrue(b) – Test passes if boolean value b is true• assertNull(o) – Test passes if object o is null• assertNotNull(o) – Test passes if object o is not null• assertSame(ox, oy) – Test passes if ox and oy refer to the
same object • assertNotSame(ox, oy) – Test passes if ox and oy do not
refer to the same object
15
Design Pattern
16
Singleton Pattern
Constructor private
17
Strategy Pattern
18
Decorator
19
Observer
Unit 3Advanced Class Implementation
• 3.1 Input and Output Programming
20
21
Abstract Classes
• InputStream & OutputStream• Reader & Writer
22
Line-oriented I/O