problem of the day two missiles speed directly toward each other one goes 9,000 miles per hour ...

21
Problem Of The Day Two missiles speed directly toward each other One goes 9,000 miles per hour Other goes 21,000 miles per hour. If they start 1,317 miles apart, how far apart are they 1 minute before colliding?

Upload: mervin-doyle

Post on 18-Jan-2016

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

Problem Of The Day

Two missiles speed directly toward each other One goes 9,000 miles per hour Other goes 21,000 miles per hour.

If they start 1,317 miles apart, how far apart are they 1 minute before colliding?

Page 2: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

Problem Of The Day

Two missiles speed directly toward each other One goes 9,000 miles per hour Other goes 21,000 miles per hour.

If they start 1,317 miles apart, how far apart are they 1 minute before colliding?

They are closing at 30,000MPH ; 1 minute of this is:

30,000/60 = 3000/6 = 500 miles!

Page 3: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

LECTURE 29:POSITION LIST

CSC 212 – Data Structures

Page 4: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

INDEXLIST ≠ array

Extends array concepts, like using indices, but… INDEXLISTs do not have constant size Element’s index changes as data added &

removed ADTs remain completely

implementation-free Using non-tenured faculty still perfectly

acceptable

Page 5: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

INDEXLIST.add()

add(i, e) “shifts” elements to make space Traverse linked list or move array elements

Can take O(n) time for this shifting Worst case occurs in both array- & linked

list-based May only take O(1) to add to end of list

0 1 2

e

n-1i

Page 6: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

INDEXLIST.remove()

remove(i) “shifts” elements down to fill hole Not implementation-specific – only indices

are shifted O(n) time required in the general case

But for specific situations could take only O(1) time

But must consider worst case when computing big-Oh0 1 2

e

n-1i

Page 7: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

INDEXLIST

DEQUE QUEUE STACK

add()addFront()addLast()

enqueue() push()

get()getFront()getLast()

front() top()

remove()removeFront()removeLast()

dequeue() pop()

set() None! None! None!

Operations To Date

Page 8: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

Collection which we can access all elements Add element before an existing one Return the Collection’s 3rd element Loop over all elements without removing

them LIST ADTs differ in how they provide

access INDEXLIST uses indices for absolution

positioning Can only use relative positions in NODELIST

List ADT

Page 9: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

Does Absolute Position Matter?

Page 10: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

Relativity Can Be Important

Page 11: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

Based upon a conceptual linked list No implementation specified: still an ADT Relies upon Nodes implementing POSITION

ADT POSITIONs at relative locations in the

LIST first() & last() POSITIONs only

absolutes Traverse using next(p) & prev(p) methods

POSITIONLIST with 3 elements

NODELIST ADT

first last

Page 12: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

Abstracts Node idea for use anywhere Get the POSITION's value using element() Cannot change element using interface's

methods Position is ADT & works with array elements class ArrayPos<E> implements Position<E> {

private E[] theArray;private int index;

public ArrayPos(E[] arr, int idx) { … }

public E element(){ return theArray[index]; }

}

POSITION Interface

Page 13: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

public interface PositionList<E> extends Collection {Position<E> first();Position<E> last();Position<E> next(Position<E> p) throws /* … */;Position<E> prev(Position<E> p) throws /* … */;

E set(Position<E> p, E elem) throws /* … */; E remove(Position<E> p) throws /* … */;

void addFirst(E elem);void addLast(E elem);void addBefore(Position<E> p, E e) throws/*…*/;void addAfter(Position<E> p, E e) throws /* …*/;

}

Memorization Limits

Page 14: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

Good news: remove & set the same Now take POSITION as parameter and not an

index Whatever it had as its value before call returned

Relative location used to add elements to LIST addFirst & addLast are relative to all others e in new POSITION before p via addBefore addNext creates new POSITION after p for e

NODELIST Methods

Page 15: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

Linked list implements NODELIST in most cases This is not required, but is most reasonable

Few methods free with doubly-linked lists Use next & prev fields to move between

POSITIONs Call Node.setElement(e) to implement set

Most other methods we've already implemented Just adding & removing Nodes from the

linked list

Implementing NODELIST

Page 16: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

Linked list implements NODELIST in most cases This is not required, but is most reasonable

Few methods free with doubly-linked lists Use next & prev fields to move between

POSITIONs Call Node.setElement(e) to implement set

Most other methods we've already implemented Just adding & removing Nodes from the

linked list

But how to do this with POSITIONs?

Implementing NODELIST

Page 17: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

99% of typecasts are incorrect 90% fix missing generic type specification

on variable Eclipse “Quick-Fix” on illegal code is 9% 0.95% instantiate arrays of generic type When using interfaces in an ADT is 0.05%

Typecasting Explained

Page 18: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

99% of typecasts are incorrect 90% fix missing generic type specification

on variable Eclipse “Quick-Fix” on illegal code is 9% 0.95% instantiate arrays of generic type When using interfaces in an ADT is 0.05%

Typecasting Explained

Page 19: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

Used when we implement PositionList Class allocates instance, so typecasting is

safe

private Node<E> checkPosition(Position<E> p) throws InvalidPositionException {if ((p == null) || !(p instanceof Node)) { throw new InvalidPositionException();}return (Node<E>)p;

}

From Position to Node

Page 20: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

Your Turn

Get into your groups and complete activity

Page 21: Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start

For Next Lecture

Before Wednesday’s lecture read GT 6.3 What is an Iterator? How is it used? How are Iterable classes related to Lists? Are Iterable & Iterators related? If so,

how?

Programming Assignment #2 available now javadoc is your friend; please write good

comments