[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]

Back to Vectors. On to Lists, Concluded.

**Held** Friday, March 12

**Summary**

- Selecting a core set of list operations
- Lists in Java vs. Lists in Scheme
- Lists vs. nodes
- Dynamic-sized lists.
- Supporting additional list operations.
- Reading:
*Java Plus Data Structures*, Chapter 6 - Reading:
*Java Plus Data Structures*, Section 8.3 - Exam 2 distributed. Due Friday, March 19.

**Contents**

**Handouts**

**Notes**

- Some of you have failed to note that I have made an answer key available for the first exam. I do my best to create answer keys for every assignment and exam.
- I was sorry to see so few of you at convocation yesterday. It was pretty good, and focused on applications of Fibonacci numbers.

- While there are a number of ways to implement sorted lists
with arrays, it turns out to be easiest to implement sorted lists with
sorted arrays.
- You could use other techniques; we may see others in the future.

- How do you specify the sort order?
- The individual objects could provide it.
- The sort order could be specified with the constructor.

- One option in building sorted lists is to permit objects to
compare themselves to each other. To do so, we'll need to
create an appropriate
`Comparable`

interface>**/** * Objects that can sometimes be compared to each other. * * @author Samuel A. Rebelsky * @aversion 1.0 of March 1999 */**__public____interface__Comparable {**/** * Determine if this object is ``less than'' another object. * If the other object can't be compared to this one, then * throws an exception. */**__public____boolean__lessThan(Comparable other)__throws__Exception;**/** * Determine if this object is equal to another object. */**} // interface Comparable - We can then use this to develop particular kinds of comparable
objects. For example, here is an extension to our
`Fraction`

class to support comparision of fractions.__public____class__Fraction__implements__Comparable { ...**/** * Determine if this fraction is less than another fraction. * Precondition: Neither fraction has a denom. of 0. * Precondition: Both fractions are simplified. * Postcondition: Returns true if this fraction is smaller; * returns false otherwise. */**__public____boolean__lessThan(Fraction other) {**// a/b < c/d if a*d < c*b. Why? because (1) a/b = a*d/b*d,****// c/d = c*b/b*d. Since the denominators are now the same,****// we need only compare numerators.**__return____this__.num * other.denom < other.num *__this__.denom; } // lessThan(Fraction)**/** * Determine if this fraction is less than another comparable * object. Only successful if the other object is a Fraction. * Otherwise, throws an exception. * Precondition: This fraction does not have a denominator of 0. * Precondition: The fraction is simplified. * Precondition: If the parameter is a Fraction, then it has * a nonzero demoninator and is simplified. * Postcondition: If the parameter is a Fraction, and this Fraction * is smaller than the parameter, returns true. If the parameter * is a Fraction, and this Fraction is not smaller than the parameter, * returns false. Otherwise, throws an exception. */**__public____boolean__lessThan(Comparable other)__throws__Exception {__if__(other__instanceof__Fraction) {__return__lessThan((Fraction) other); }__else____throw____new__Exception(); } // lessThan(Comparable) ... } // class Fraction - Why do we use the exceptions? So that it's clear when two objects can't be compared (so that we don't assume that just because x is not less than y, and x is not equal to y, then x is greater than y).

- Another option (and perhaps a more general option), is to create objects that can be used to compare pairs of other objects.
- That way, we can use different comparisons to acheive different
sort orders.
- E.g., we could sort students by id or name.

- Here's a class. (It's a class, rather than an interface, because
we want to define one of the methods.
**/** * Things that know how to compare other things. * * @author Samuel A. Rebelsky * @version 1.0 of March 1999 */**__public____class__Comparator {**/** Compare two objects for equality. */**__public____boolean__equals(Object alpha, Object beta) {__return__alpha.equals(beta); } // equals(Object,Object)**/** * See if one object is less than another. Throws an * exception if they can't be compared. */**__public____boolean__lessThan(Object left, Object right)__throws__Exception; {**// Assume we can't compare them.**__throw____new__Exception("*Can't compare*" + left.toString() + "*and*" + right.toString()); } // lessThan(Object,Object) } // class Comparator - Here's a comparator for fractions.
**/** * Something that can compare two fractions. */**__public____class__FractionComparator__implements__Comparator {**/** * Is one fraction less than another? */**__public____boolean__lessThan(Fraction left, Fraction right) {__return__left.num*right.denom < right.num*left.denom; } // lessThan(Fraction,Fraction)**/** * Is one object less than another? */**__public____boolean__lessThan(Object left, Object right)__throws__Exception {__if__((left__instanceof__Fraction) && (right__instanceof__Fraction)) {__return__lessThan((Fraction) left, (Fraction) right); }__else__{__return____super__(left,right); } } lessThan(Object,Object) } // class FractionComparator

**History**

- Created Monday, January 11, 1999.
- Added short summary on Friday, January 22, 1999.
- Added some details on Thursday, March 11, 1999. Most were taken from outline 26 (and were not covered in that class).
- Added more details on Friday, March 12, 1999. A few were taken from outline 27 of CS152 98S and then modified. The discussion of comparable objects and comparators is new.
- Removed uncovered topics on Sunday, March 14, 1999.

Back to Vectors. On to Lists, Concluded.

[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]

**Disclaimer** Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99S/Outlines/outline.28.html

Source text last modified Sun Mar 14 13:55:46 1999.

This page generated on Sun Mar 14 14:29:13 1999 by SiteWeaver. Validate this page's HTML.

Contact our webmaster at rebelsky@math.grin.edu