Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming (Java) Review 2014. 2 Unit 1 Class Design Basic Console I/O StringTokenizer Exception UML class diagram.

Similar presentations


Presentation on theme: "Object-Oriented Programming (Java) Review 2014. 2 Unit 1 Class Design Basic Console I/O StringTokenizer Exception UML class diagram."— Presentation transcript:

1 Object-Oriented Programming (Java) Review 2014

2 2 Unit 1 Class Design Basic Console I/O StringTokenizer Exception UML class diagram

3 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 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 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 6 UML class diagram (Inheritance/Association) Dashed line: implements Solid line: extends

7 7 Unit 2 Class Implementation equals()/toString() Vector/ArrayList JDK 5 –Vector –ArrayList –For each loop (Iterable interface/iterator method) JUnit Design Pattern

8 8 Vector and Iterator

9 9 methods defined in class Vector Vector(). Constructs an empty collection.Vector() int size(). Returns the number of objects in the collection.int size() boolean isEmpty(). Determines if there are no objects in the collection.boolean isEmpty() boolean contains(Object elem). Determines if the specified object is an element of the collection (as determined by the method equals).boolean contains(Object elem) boolean add(E o). Appends the specified object to the end of the collection.boolean add(E o) 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).void add(int index, E element) E get(int index). Returns the object at the specified position.E get(int index) public E set(int index, E element). Replaces the element at the specified index position with the specified object.public E set(int index, E element) 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).public boolean remove(Object o) 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).E remove(int index)

10 10 Methods Defined in Class ArrayList ArrayList(). Constructs an empty collection.ArrayList() int size(). Returns the number of objects in the collection.int size() boolean isEmpty(). Determines if there are no objects in the collection.boolean isEmpty() boolean contains(Object elem). Determines if the specified object is an element of the collection (as determined by the method equals).boolean contains(Object elem) boolean add(E o). Appends the specified object to the end of the collection.boolean add(E o) 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).void add(int index, E element) E get(int index). Returns the object at the specified position.E get(int index) public E set(int index, E element). Replaces the element at the specified index position with the specified object.public E set(int index, E element) 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).public boolean remove(Object o) 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).E remove(int index)

11 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 12 JDK 5 public class Client implements Iterable { … private Vector accounts; public Client(String initialName) { accounts = new Vector (); } public Iterator iterator() { return accounts.iterator(); } … } for (BankAccount account : client) { totalBalance += account.getBalance(); }

13 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 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 15 Design Pattern

16 16 Singleton Pattern Constructor private

17 17 Strategy Pattern

18 18 Decorator

19 19 Observer

20 Unit 3Advanced Class Implementation 3.1 Input and Output Programming 20

21 21 Abstract Classes InputStream & OutputStream Reader & Writer

22 22 Line-oriented I/O


Download ppt "Object-Oriented Programming (Java) Review 2014. 2 Unit 1 Class Design Basic Console I/O StringTokenizer Exception UML class diagram."

Similar presentations


Ads by Google