Problem of the Day At what times do the minute and hour hands on an analog clock line up?
-
Upload
alexandro-rainsford -
Category
Documents
-
view
219 -
download
0
Transcript of Problem of the Day At what times do the minute and hour hands on an analog clock line up?
![Page 1: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/1.jpg)
Problem of the Day
At what times do the minute and hour hands on an analog clock line up?
![Page 2: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/2.jpg)
Problem of the Day
At what times do the minute and hour hands on an analog clock line up?
Hands overlap one another Hands opposite one another
12:00:00.000
01:05:27.273
02:10:54.545
03:16:21.818
04:21:49.091
05:27:16.364
06:32:43.636
07:38:10.909
08:43:38.182
09:49:05.455
10:54:32.727
06:00:00.000
07:05:27.273
08:10:54.545
09:16:21.818
10:21:49.091
11:27:16.364
12:32:43.636
01:38:10.909
02:43:38.182
03:49:05.455
04:54:32.727
![Page 3: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/3.jpg)
LECTURE 21:STACK ADT
CSC 212 – Data Structures
![Page 4: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/4.jpg)
Rest of the Year
Abstract – List what is done, not how
it is done
Data – Access & use Collections of
data
Type – Will use in fields, parameters,
& locals
![Page 5: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/5.jpg)
Rest of the Year
Abstract – List what is done, not how
it is done
Data – Access & use Collections of
data
Type – Will use in fields, parameters,
& locals
![Page 6: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/6.jpg)
ADTs Mean Interfaces
Each ADT is defined by single Interface Guarantees methods exist & what they should
do But classes are free to implement however
they want Programmer knows from the interface:
Each of the method signatures Value returned by the method The effects of the method’s actions Why Exceptions thrown by method
![Page 7: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/7.jpg)
View of an ADT
IOU
![Page 8: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/8.jpg)
View of an ADT
You
Other Coder
IOU
![Page 9: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/9.jpg)
View of an ADT
You
Other Coder
IOU
ADT
![Page 10: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/10.jpg)
ADTs must remain abstract Any implementation okay, if it completes
methods Could implement an ADT with:
Implementing ADT
![Page 11: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/11.jpg)
ADTs must remain abstract Any implementation okay, if it completes
methods Could implement an ADT with:
Array
Implementing ADT
![Page 12: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/12.jpg)
ADTs must remain abstract Any implementation okay, if it completes
methods Could implement an ADT with:
Array Linked list
Implementing ADT
![Page 13: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/13.jpg)
ADTs must remain abstract Any implementation okay, if it completes
methods Could implement an ADT with:
Array Linked list Trained monkeys
Implementing ADT
![Page 14: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/14.jpg)
ADTs must remain abstract Any implementation okay, if it completes
methods Could implement an ADT with:
Array Linked list Trained monkeys College students
Implementing ADT
![Page 15: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/15.jpg)
Is linked list an ADT?
NO!
![Page 16: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/16.jpg)
Linked lists have very specific implementation Singly-, & doubly-linked versions exist… … but implementation impossible using an
array No trained monkeys could do same work
Linked lists also do not specify functionality No standard way to access or use data In fact, there is no interface serving as
guarantee!
Why is linked list not ADT?
![Page 17: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/17.jpg)
Implementation vs. ADT
Implementation ADT
![Page 18: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/18.jpg)
Superinterface for all our ADTs Define methods common to all data
structures Access & usages patterns differ with each
ADT
Collection Classes
![Page 19: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/19.jpg)
Superinterface for all our ADTs Define methods common to all data
structures Access & usages patterns differ with each
ADT
public interface Collection {public int size();public boolean isEmpty();
}
Collection Classes
![Page 20: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/20.jpg)
Superinterface for all our ADTs Define methods common to all data
structures Access & usages patterns differ with each
ADT
public interface Collection {public int size();public boolean isEmpty();
}
Collection Classes
![Page 21: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/21.jpg)
Superinterface for all our ADTs Define methods common to all data
structures Access & usages patterns differ with each
ADT
public interface Collection {public int size();public boolean isEmpty();
}
Collection Classes
![Page 22: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/22.jpg)
Awwww… our first collection class Works like PEZ dispenser:
Add by pushing data onto top Pop top item off to remove
Accessing other values impossible Top item only is available Cheap plastic/private fields get in way
Stacks
![Page 23: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/23.jpg)
Applications of Stacks
Stacks are used everywhere Back & Forward buttons in web browser Powerpoint’s Undo & Redo commands Methods’ stackframes used during
execution Java uses stacks to execute operations in
program
![Page 24: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/24.jpg)
Defines two vital methods… push(obj) add obj onto top of stack pop() remove & return item on top of
stack … an accessor method…
top() return top item (but do not remove it)
… and Collection’s methods… size() returns number of items in stack isEmpty() states if stack contains items
Stack ADT
![Page 25: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/25.jpg)
ADT also defines own exceptionpublic class EmptyStackException extends RuntimeException {public EmptyStackException(String err) { super(err);}
} EmptyStackException is unchecked
Need not be listed in throws, but could be Unchecked since there is little you can do
to fix it try-catch not required, but can crash
program
More Stack ADT
![Page 26: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/26.jpg)
public interface Stack<E> extends Collection {public E top() throws EmptyStackException;public E pop() throws EmptyStackException;public void push(E element);
}
Any type of data stored within a Stack Generics enable us to avoid rewriting this code
Minimum set of exceptions defined by interface Classes could throw more unchecked
exceptions
Stack Interface
![Page 27: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/27.jpg)
Last-In, First-Out principle used to access data Also called LIFO ordering
Top of stack is where data added & removed
Using Stack
![Page 28: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/28.jpg)
Last-In, First-Out principle used to access data Also called LIFO ordering
Top of stack is where data added & removed Pulling out tablecloth trick does not work
Using Stack
![Page 29: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/29.jpg)
Your Turn
Get into your groups and complete activity
![Page 30: Problem of the Day At what times do the minute and hour hands on an analog clock line up?](https://reader036.fdocuments.net/reader036/viewer/2022081518/551c2190550346ad4f8b5b97/html5/thumbnails/30.jpg)
For Next Lecture
Read GT5.1.2 – 5.1.3 for Friday’s class How can we use an array to implement a
Stack? Linked-list based approaches possible, too? Why would we prefer one over the other?
Week #8 weekly assignment due on Tuesday
Programming assignment #1 also on Angel Pulls everything together and shows off
your stuff Better get moving on it, since due in 10
days!