1 / 32

Problem Of The Day

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?. Problem Of The Day. Two missiles speed directly toward each other

diata
Download Presentation

Problem Of The Day

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 1,317 miles apart, how far apart are they 1 minute before colliding?

  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 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!

  3. CSC 212 – Data Structures Lecture 27:List, Comparable AND COMPARATOR

  4. Still Limited with Deque • Often want to use entire collection • Add element before existing one in the list • Find if element stored anywhere in the list • Loop over all elements without removing them • All these actions are impossible with Deque

  5. Who Can Save Us From This?

  6. Who Can Save Us From This?

  7. Who Can Save Us From This? • What a great solution – we can use a List!

  8. List’s Operations

  9. List’s Operations

  10. List’s Operations

  11. We Don’t Need No Stinking Elements • Note that ADT does not include methods to add • Clear sign more code used to make idea usable • Until we add elements, other methods not usable • But how to store elements in the List? • Store & maintain elements in order within List -or- • Listunordered & searched when needed • Two flavors (subinterfaces) of List defined • UnorderedList holds elements in any order • Total ordering used with OrderedList

  12. We Don’t Need No Stinking Elements • Note that ADT does not include methods to add • Clear sign more code used to make idea usable • Until we add elements, other methods not usable • But how to store elements in the List? • Store & maintain elements in order within List -or- • Listallow coders to store elements in order they need • Two flavors (subinterfaces) of List defined • UnorderedList holds elements in any order • Total ordering used with OrderedList

  13. List Subinterfaces Defined public interface OrderedList<T> extends List<T> { public void add(T element);} public interface UnorderedList<T> extends List<T> { public void addToFront(T element); public void addToRear(T element); public void addAfter(T elem, T tgt); }

  14. How Do We Access Elements?

  15. How Do We Access Elements?

  16. How Do We Access Elements?

  17. Comparable Interface • Provides simple, abstract way to compare data • Found in java.lang package just like String • Makes coding easier by simplifying search & compare • Total ordering relation needed to implement • Must be exactly one of a< b or a == b or a > b • If a < b&b < cthena < c implied by this definition

  18. Comparable Interface • Single method defined by Comparable<E> intcompareTo(E o) • Compare this instance with o(ther) instance • The only thing that matters is relative value returned • Specific value meaningless & only defines result is: negativeif this<o, zeroif this==opositiveif this>o

  19. Comparable Example class Studentimplements Comparable<Student> {private float qpa;...public intcompareTo(Studentother) { if (qpa > other.qpa) { // Need to return positive number herereturn 73; } else if (qpa== other.qpa) { // Need to return 0 in this case return 0; } else { // Need to return negative number here return -45864;} }

  20. Using Comparable Student match(float grade, Iterable<Student> it){Studentseeker = new Student();seeker.setQPA(grade);for (Student s : it) { if (seeker.compareTo(s) == 0) { return s; } }// Prof. Idiot forgot to define Exception for this unexpected event, so…return null; }

  21. What Does compareTo() Match?

  22. What Does compareTo() Match?

  23. What Does compareTo() Match?

  24. What Does compareTo() Match?

  25. And Your Reaction Should Be…

  26. And Your Reaction Should Be… • NHL team class can only have 1 compareTo • But many ways to compare and rank teams

  27. And Your Reaction Should Be… • NHL team class can only have 1 compareTo • But many ways to compare and rank teams • Is writing specific case-by-case code our fate?

  28. Comparator to the Rescue! • Can also use Comparators to compare data • Interface in java.utilpackage (like Scanner) • Unlike Comparable, is separate class doing work • Comparator<E> defines single method intcompare(E o1, E o2) • Specific value meaningless; result defined by: negativeif o1 <o2, zeroif o1 ==o2positiveif o1>o2

  29. Comparator Example class WinComp implements Comparator<NHLTeam>public intcompare(NHLTeamo1,NHLTeamo2) { if (o1.getWins()>o2.getWins()) { return 42; // Need to return positive number here } else if (o1.getWins() == o2.getWins()) { return 0; // Need to return 0 in this case } else { return -68; // Need to return negative number here} } } class PointsComp implements Comparator<NHLTeam>public intcompare(NHLTeamo1,NHLTeamo2) { return (o1.getPoints() - o2.getPoints());} }

  30. Using Comparator intrank(NHLTeamteam, Comparator<NHLTeam> comp,Iterable<NHLTeam> it){int count = 1;for (NHLTeamgoons : it) { if (comp.compare(team, goons) < 0) { count++; } }// Rank of the given team for this metricreturn count; }

  31. Your Turn • Get into your groups and complete activity

  32. For Next Lecture • Before Friday’s lecture read 6.5 – 6.6 • What is ArrayList? How is it used? • How do we implement each list with an array? • What downsides occur when we use an ArrayList? • Week #10 assignment posted to Angel • As usual, will be due next Tuesday

More Related