Download presentation
Presentation is loading. Please wait.
Published byHannah Bishop Modified over 9 years ago
1
Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs 1’ apart Tide rises at a rate of 9” per hour After 6 hours what length of ladder will still be above water?
2
Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs 1’ apart Tide rises at a rate of 9” per hour After 6 hours what length of ladder will still be above water? 9’ – the ladder will rise with the boat!
3
CSC 212 – Data Structures
4
Problem With Position Really, really limited in how it can be used Provides way of holding & accessing an element No way to use element with a Position Great for keeping unorganized lists of information Sucks for anything else like: Keeping a list whose data is automatically sorted Searching for something within huge collection Use data for analysis by linking related items Prioritizing items to stay up-to-date on demands
5
How Much Info Can You Get?
6
How Do We Organize Data?
8
Key Key To How We Organize Data
9
Entry To The Rescue Entry provides 2 items to view & use data valu(e) Preserves keeping the valu(e)able data we care about Key Key detail used to organize valuables also maintained
10
Entry To The Rescue Entry provides 2 items to view & use data valu(e) Preserves keeping the valu(e)able data we care about Key Key detail used to organize valuables also maintained interface Entry { K key(); V value(); }
11
KeyValue What’s The Key & Value?
14
Comparable Interface Provides simple, abstract way to compare data Found in java.lang package just like Iterable Makes coding easier by simplifying search & compare Total ordering relation needed to implement exactly one Must be exactly one of a b If a < b & b < c then a < c implied by this definition
15
Comparable Interface Single method defined by Comparable int compareTo(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: negative zero positive negative if this o
16
Comparable Example positive 0 negative class Student implements Comparable { private float qpa;... public int compareTo(Student other) { if (qpa > other.qpa) { // Need to return positive number here return 73; } else if (qpa == other.qpa) { // Need to return 0 in this case return 0; } else { // Need to return negative number here return -45864; } }
17
Using Comparable Student match(float grade, Iterable it){ Student seeker = 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; }
18
What Does compareTo() Match?
22
And Your Reaction Should Be…
23
NHL team class can only have 1 compareTo But many ways to compare and rank teams
24
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?
25
Comparator to the Rescue! Can also use Comparator s to compare data Interface in java.util package (like Iterator ) Like Iterator, is separate class performing work Comparator defines single method int compare(E o1, E o2) Specific value meaningless; result defined by: negative zero positive negative if o1 o2
26
positive 0 negative class WinComp implements Comparator public int compare(NHLTeam o1, NHLTeam o2) { 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 public int compare(NHLTeam o1, NHLTeam o2) { return (o1.getPoints() - o2.getPoints()); } } Comparator Example
27
Using Comparator int rank(NHLTeam team, Comparator comp, Iterable it){ int count = 1; for (NHLTeam goons : it) { if (comp.compare(team, goons) < 0) { count++; } } // Rank of the given team for this metric return count; }
28
Your Turn Get into your groups and complete activity
29
For Next Lecture Read GT 8.1.3 – 8.2.2 for Friday's lecture What is a PriorityQueue and what does it do? Queue sounds familiar; is there any relationship? How can I link Marx, Orwell, & Homer Simpson? Week #14 assignment posted & due Tuesday Programming Assignment #3 Programming Assignment #3 due next Friday
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.