# Problem of the Day  At what times do the minute and hour hands on an analog clock line up?

## Presentation on theme: "Problem of the Day  At what times do the minute and hour hands on an analog clock line up?"— Presentation transcript:

Problem of the Day  At what times do the minute and hour hands on an analog clock line up?

Problem of the Day  At what times do the minute and hour hands on an analog clock line up? Hands overlap one anotherHands 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

CSC 212 – Data Structures

Rest of the Year

ADTs Mean Interface s  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 Exception s thrown by method

You Other Coder IOU

 ADTs must remain abstract  Any implementation okay, if it completes methods  Could implement an ADT with: Implementing ADT

 ADTs must remain abstract  Any implementation okay, if it completes methods  Could implement an ADT with:  Array Implementing ADT

 ADTs must remain abstract  Any implementation okay, if it completes methods  Could implement an ADT with:  Array  Linked list Implementing ADT

 ADTs must remain abstract  Any implementation okay, if it completes methods  Could implement an ADT with:  Array  Linked list  Trained monkeys Implementing ADT

 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

 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?

 Superinterface for all our ADTs  Define methods common to all data structures  Access & usages patterns differ with each ADT Collection Classes

 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

 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

 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

 Awwww… our first collection class  Works like PEZ dispenser:  Add by pushing data onto top  Pop top item off to remove impossible  Accessing other values impossible  Top item only is available  Cheap plastic/private fields get in way Stacks

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

 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

 ADT also defines own exception public 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

public interface Stack 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

Using Stack