220 likes | 336 Views
Object-Oriented Programming (Java) Review 201 4. Unit 1 Class Design. Basic Console I/O StringTokenizer Exception UML class diagram. Console I/O. private static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); private static PrintWriter stdOut =
E N D
Object-Oriented Programming (Java) Review 2014
Unit 1 Class Design • Basic Console I/O • StringTokenizer • Exception • UML class diagram
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
StringTokenizer • Import java.util.*; • StringTokenizer tokenizer = new StringTokenizer(data, "_"); • tokenizer.countTokens(); //number of tokens • while(tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); } // traverse each token
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"); }
UML class diagram (Inheritance/Association) Solid line: extends Dashed line: implements
Unit 2 Class Implementation • equals()/toString() • Vector/ArrayList • JDK 5 • Vector<String> • ArrayList<String> • For each loop (Iterable interface/iterator method) • JUnit • Design Pattern
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).
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).
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
JDK 5 public 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(); }
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.
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
Singleton Pattern Constructor private
Unit 3Advanced Class Implementation • 3.1 Input and Output Programming
Abstract Classes • InputStream & OutputStream • Reader & Writer