[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Project] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
Back to Graphs, Concluded. On to A History of Computer Science.
Held Wednesday, December 8, 1999
Overview
Today, we'll go over a variety of issues, from the subject matter of this course to ideas on comparing lists.
Notes
Contents
Summary
These are some of the metrics I saw for equality.
advance to step through the elements,
we need to be careful not to advance twice on the
same list.)
while there are elements left in both lists compare the corresponding elements of both lists and do something move on to the next element of both lists end while Do whatever is appropriate for when you run off the end of the lists.
CursoredList interface lacks
a hasMoreElements method.
// Are there more elements left in listA?
boolean moreA = true;
// Are there more elements left in listB?
boolean moreB = true;
// While there are elements left in both lists.
while (moreA && moreB) {
// Compare the corresponding elements of both lists.
try {
if (compare.lessThan(listA.getCurrent(), listB.getCurrent())) {
// Do something
...
}
}
catch (IncomparableException e) {
// Do something
...
}
// Move on to the next element of both lists
try { listA.advance(); }
catch (Exception e) { moreA = false; }
try { listB.advance(); }
catch (Exception e) { moreB = false; }
} // while there are more elements left in both lists.
// One list is over. Do whatever is appropriate.
...
import Comparator;
import CursoredList;
import CursoredLinkedList;
/**
* Test a list comparator.
*/
public class LCTester {
/**
* Perform some simple tests on a list comparator.
*/
public LCTester(Comparator lcomp) {
// Prepare for output.
SimpleOutput out = new SimpleOutput();
// Some sample arrays for later conversion to lists
Object[] aaa = {"a", "a", "a"};
Object[] aaaa = {"a", "a", "a", "a"};
Object[] abc = {"a", "b", "c"};
Object[] bac = {"b", "a", "c"};
Object[] cba = {"c", "b", "a"};
// Check one pair of equal lists.
checkPair(out, lcomp, buildList(aaa), buildList(aaa));
// Check two empty lists.
checkPair(out, lcomp, new CursoredLinkedList(), new CursoredLinkedList());
// Check one empty and one nonempty list.
checkPair(out, lcomp, new CursoredLinkedList(), buildList(aaa));
// Check various incomparable lists
checkPair(out, lcomp, buildList(aaa), buildList(abc));
checkPair(out, lcomp, buildList(abc), buildList(bac));
// Check two inequal lists of different lengths
checkPair(out, lcomp, buildList(aaa), buildList(aaaa));
} // LCTester(Comparator)
/**
* Compare one pair of cursored lists.
*/
public void checkPair(SimpleOutput out, Comparator lcomp,
CursoredList listA, CursoredList listB) {
out.println("Comparing A:" + toString(listA) + " to B:"
+ toString(listB) + ".");
out.print(" Equality: ");
try {
if (lcomp.equals(listA,listB))
out.println("The two lists are equal");
else
out.println("The two lists are inequal");
} // try
catch (IncomparableException e) {
out.println("The two lists are incomparable.");
}
out.print(" Inequality (A vs. B): ");
try {
if (lcomp.lessThan(listA,listB))
out.println("A is less than B.");
else
out.println("A is not less than B.");
} // try
catch (IncomparableException e) {
out.println("The two lists are incomparable.");
} // catch
out.print(" Inequality (B vs. A): ");
try {
if (lcomp.lessThan(listB,listA))
out.println("B is less than A.");
else
out.println("B is not less than A.");
} // try
catch (IncomparableException e) {
out.println("The two lists are incomparable.");
} // catch
} // checkPair
/**
* Build a list, given an array of object.
*/
public CursoredList buildList(Object[] elements) {
CursoredList newList = new CursoredLinkedList();
for (int i = elements.length-1; i >= 0; --i) {
newList.addToFront(elements[i]);
}
return newList;
} // buildList(Object[])
/**
* Turn a cursored list into a string. May change the position
* of the cursor.
*/
public String toString(CursoredList cl) {
String result = "(";
cl.front();
try {
// Keep printing the current element and then advancing the
// cursor until you advance off the end of the list. At that
// point, the advance method should throw an exception and
// we escape from the while loop.
while (true) {
result = result + cl.getCurrent() + " ";
cl.advance();
}
}
catch (Exception e) {
}
result = result + ")";
return result;
} // toString(CursoredList)
} // class LCTester
Tuesday, 10 August 1999
Wednesday, 8 December 1999
Back to Graphs, Concluded. On to A History of Computer Science.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Project] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [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/99F/Outlines/outline.54.html
Source text last modified Wed Dec 8 09:31:30 1999.
This page generated on Wed Dec 8 09:39:47 1999 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu