ece 462 object-oriented programming using c++ and java ...yhl container class 1 ece 462...
TRANSCRIPT
YHL Container Class 1
ECE 462Object-Oriented Programming
using C++ and Java
Container Classes
Yung-Hsiang [email protected]
YHL Container Class 2
Why Container Classes?
• Many programs require arrays, vectors, lists, queues, stacks, sets ... to store information.
• Both C++ and Java provide container classes that automatically manage memory: allocate additional memory when more elements are added.
• The supported container classes greatly reduce the amount of code needed by programmers and improve productivity.
• Container classes and OOP are closely related: – a container can hold objects of derived classes– polymorphism properly invokes the correct methods
YHL Container Class 3
Container Class (For Code Reuse)
• A container needs to be able to hold items of different types (i.e. classes). Examples– list of strings, integers, floating points, student objects– queues of customer objects, car objects– maps: name → address, student ID → name, course
title → classroom• C++ standard template library (STL) and Java container
classes provide such functionality.
YHL Container Class 4
Select Container Class
• random or sequential accesses?• allow unique or duplicate items?• O(1) or O(N) for array-like access (using [index])• efficient insert / delete?
– front– end– middle
• Java containers cannot store primitive types (int, char, float ...) and can store objects only. C++ containers can store primitives.
YHL Container Class 5
Efficiency
O(1)O(N)O(N)insert/delete in middle
O(1)O(1)+O(1)+insert/delete at end
O(1)O(1)+O(N)insert/delete at front
O(N)O(1)O(1)array-like access
listdequevectoroperation
N: current number of items
YHL Container Class 6
Java Containers
YHL Container Class 7
Java List
YHL Container Class 8
YHL Container Class 9
Interface and Class
• A Java interface serves as an abstract class and cannot be instantiated.
• An interface can be implemented by classes.• Typically, an interface is a common base for several
related classes, for example, interface List as the base of ArrayList, LinkedList, Stack, and Vector.
YHL Container Class 10
YHL Container Class 11
YHL Container Class 12
YHL Container Class 13
YHL Container Class 14
YHL Container Class 15
Java Set
YHL Container Class 16
YHL Container Class 17
YHL Container Class 18
no duplicate element in Setelements sorted
YHL Container Class 19
Java Map
YHL Container Class 20
Map (Hash Table)
• array: integer → element (object)• map: key (object, integer, or string ...) → value (object)• example:
– name → phone number– student ID → department– city name → zip code
• Keys must be unique and do not have to be continuous (unlike array indexes). Values do not have to be unique.
YHL Container Class 21
Java MapHistogram of Words
YHL Container Class 22
YHL Container Class 23
YHL Container Class 24
YHL Container Class 25
YHL Container Class 26
YHL Container Class 27
YHL Container Class 28
C++ Container(Standard Template Library)
YHL Container Class 29
C++ Vector
YHL Container Class 30
YHL Container Class 31
YHL Container Class 32
C++ Vector
contiguous memory • efficient access (array-like [index])• efficient insert / delete at the end (allocate more memory
occasionally)• inefficient insert / delete at the front (allocate memory
for every insertion)• automatic expand / shrink allocated memory (allocate
more than necessary to reduce allocation / release / copy overhead)
• occasional copying of the whole vector
YHL Container Class 33
C++ Iterator
pointer to traverse a vector or other STL (standard template library) container
vector<int> v;cout << "\nvector size is: " << v.size() << endl;vector<int>::iterator p = v.begin();while ( p != v.end() ) {
cout << *p << " "; p++;
}
YHL Container Class 34
C++ List
YHL Container Class 35
YHL Container Class 36
YHL Container Class 37
YHL Container Class 38
YHL Container Class 39
C++ Vector of Objects
YHL Container Class 40
YHL Container Class 41
YHL Container Class 42
YHL Container Class 43
YHL Container Class 44
YHL Container Class 45
Self Test
YHL Collision Detection 1
ECE 462Object-Oriented Programming
using C++ and Java
Collision Detection
Yung-Hsiang [email protected]
YHL Collision Detection 2
Collision Detection
Almost every interactive game requires collision detection: a bullet hits a player, an airplane lands on a runway, a ball hits a brick, and a Tetrix piece hits a wall
YHL Collision Detection 3
Law of Reflection
• N: normal vector• I: incident vector• R: reflection vector• α : angle between N and I• β : angle between N and R
• Law of Reflection
N
IR αβ
α = β
YHL Collision Detection 4
Calculate R
• Suppose N, I, and R are unit vectors, i.e. |N| = |I| = |R| = 1
• R + (- I) = 2 (N ⋅ (- I)) N⇒ The sum of R and (-I) has the
same direction as N⇒ The length is twice the inner
product of N and (-I).
R = 2 (N ⋅ (- I)) N + I = - 2 (N ⋅ I) N + I
N
- IR αβ
YHL Collision Detection 5
Collision between two Circles
N = (x1, y1) - (x2, y2)C1 is moving.
(x1, y1)
(x2, y2)
I
R
YHL Collision Detection 6
Collision between Circle and Square
• approximate by using two squares• if the bounding square of the circle intersects with the
square ⇒ collision• if the intersection is tall ⇒ collide horizontally
(x1, y1)
(x2, y2)
I
RN
YHL Collision Detection 7
YHL Collision Detection 8
YHL Collision Detection 9
YHL Collision Detection 10
YHL Collision Detection 11
YHL Collision Detection 12
YHL Collision Detection 13
YHL Collision Detection 14
YHL Collision Detection 15
YHL Collision Detection 16
YHL Collision Detection 17
YHL Collision Detection 18
YHL Collision Detection 19
YHL Collision Detection 20
YHL Collision Detection 21
YHL Collision Detection 22
YHL Collision Detection 23
YHL Collision Detection 24
YHL Collision Detection 25
YHL Collision Detection 26
YHL Collision Detection 27
Known Problem 1
• When the ball moves towards the gap between two bricks, brick 1 may be eliminated first even though the ball would actually hit brick 2 first.
• This can happen because – brick 1 precedes brick 2 in the brick
list so brick 1 is checked first– the ball moves in discrete steps
(one pixel length each time).• Solution: ignore. This is not easily
noticeable when the ball moves fast.
1 2
YHL Collision Detection 28
Known Problem 2
• Once the ball moves horizontally (or vertically) and there is no circle brick along the ball's movement, the ball's direction will never change.
• Solution: detect whether |vx| or |vy| is too small (for example less than 0.1). If it is too small, make it larger (for example, 0.15).
YHL Collision Detection 29
Self Test
YHL New Container 1
ECE 462Object-Oriented Programming
using C++ and Java
Create New C++ Containers
Yung-Hsiang [email protected]
YHL New Container 2
Why to Create New Containers?
• need efficient ways to store and access objects• reuse the same implementation for different classes
YHL New Container 3
C++ Binary Search Tree
YHL New Container 4
Binary Search Tree
• A tree contains a group of nodes. • A node can have a left child and a right child. • If a node has no child, the node is a called a leaf node.• Each node has a unique value. • When a new value is inserted to the tree, if the value
already appears, the insertion has no effect.• If the value is smaller than a node, the value is inserted
as the left child of the node. If the node already has a left child, the value is inserted as a child of the child (recursively).
YHL New Container 5
• insert the first value, 17• insert 9• insert 23
• insert 4
1717
917
9 2317
9 23
4
YHL New Container 6
• insert 11
• insert 31
17
9 23
4 11
17
9 23
4 11 31
YHL New Container 7
Traverse Tree (In-Order)
if (left child is not empty){ visit left child; }print value;if (right child is not empty){ visit right child; }
⇒ The output values are sorted.4, 9, 11, 17, 23, 31
17
9 23
4 11 31
YHL New Container 8
YHL New Container 9
YHL New Container 10
YHL New Container 11
YHL New Container 12
YHL New Container 13
YHL New Container 14
YHL New Container 15
YHL New Container 16
YHL New Container 17
Self Test