assignment, be sure to put all the classes in one file • You must take the “public” access modifier off all classes, except for the class that matches the name of the file
did not forget to include a class, you need to save only this one file in one folder and try to compile the file • It is easy to forget a class (such as an exception class) • See FractionCalculatorInOneFile.java
order, this makes searching easier • Think how hard it would be to find a phone number if a phonebook was not in alphabetical order! This is why searching and sorting is an important topic in computer science
item in sorted data collection 2.If middle item is not the correct item, examine either 1st half or 2nd half of sorted data collection for the item 3.Stop when find item, or find that the item does not exist
is much faster than sequential search with a O(n), we want to use binary search to find something in our data collection • However, the data collection must be sorted in order to use binary search • This is why sorting data is important in computer science
array of objects, which involves rearranging the pointers to the objects, but not changing the storage location of the objects themselves • Swapping pointers (addresses) between reference variables takes little time (as opposed to copying all the data from one object to another)
we sort a collection of data, the result will always be in ascending order (from smallest to largest) • To avoid confusion, “in order” always means in ascending order (NOT descending order)
implements interface java.lang.Comparable • Class such as Integer and String already implement this interface • We need to implement interface Comparable in our Name and Fraction classes to sort objects of these classes
• Only contains one method prototype: int compareTo(Object o); • See interface Comparable.java and classes Fraction.java, Name.java, ThreeNames.java, and client class TestComparable.java on the class web page for examples
compareTo() is implemented as follows: • “Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
another, which is done by putting the class name in parentheses • For example, you can cast an integer into a float using (double) int x = 5; //x=5 double y = (double) x; //y=5.0
object of a superclass into an object of its subclass • For example, we can cast an Object to a Fraction, because Fraction is a subclass of Object • We must cast in order to use the methods of the subclass
is cast to a Fraction, so we can use the methods of class Fraction, as parameter o2 should be an object of class Fraction public int compareTo(Object o2){ Fraction f2 = (Fraction)o2; int n1 = numerator * f2.getDenominator();
try to cast to the wrong class, which will throw the exception ClassCastException • For example, TestComparable.java program throws this exception, because a Name object is cast to a Fraction object in the compareTo() method in the Fraction class
have a compareTo() method declaration, but the TestComparable.java program calls the compareTo() method on the two ThreeNames objects? • This is because compareTo() is inherited from superclass Name
why does the compareTo() method inherited from superclass Name still work? • Because the toString() method inside compareTo() calls the ThreeNames’s toString() method • This is polymorphism at work!
This contains code for the 5 sorting algorithms that we will cover • Although each sorting method can sort an array of any class that implements interface Comparable, the main method only sorts Integers that are entered on the command line
This algorithm has nested for loops • Outer for loop swaps all elements to the correct position • Inner for loop finds the index of the smallest element
does not depend on initial order of elements • If the array is already in order, the algorithm still searches for the smallest element, and “swaps” the element with itself, so O(n2) • If the array is NOT in order, then still have O(n2)
O(n), if already sorted • Worse case: O(n2), so does depend on initial order of elements • This algorithm has nested for loops • Outer for loop repeats N-1 times • Inner for loop swaps adjacent elements that are out of order
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture