22
Object-Oriented Programming (Java) Review 2014

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

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

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

Object-Oriented Programming (Java)

Review 2014

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

2

Unit 1 Class Design

• Basic Console I/O• StringTokenizer• Exception• UML class diagram

Page 3: 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

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

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

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

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

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

6

UML class diagram (Inheritance/Association)

Dashed line: implements

Solid line: extends

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

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

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

8

Vector and Iterator

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

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

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

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

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

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

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

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

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

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.

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

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

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

15

Design Pattern

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

16

Singleton Pattern

Constructor private

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

17

Strategy Pattern

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

18

Decorator

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

19

Observer

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

Unit 3Advanced Class Implementation

• 3.1 Input and Output Programming

20

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

21

Abstract Classes

• InputStream & OutputStream• Reader & Writer

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

22

Line-oriented I/O