CSIT115 Class 24

Sec. 13.1 Sorting and Searching

Read to pg. 780, stopping short of “Shuffling”

Note this line of code on pg. 775:

 List<String> words = new ArrayList<String>();

For our purposes, simply replace List with ArrayList here.  But to explain why List<String> can be used here, note that List<T> is an interface in the JDK and ArrayList<T> implements List<T>, so an ArrayList<T> IS-A List<T>.  But we won’t use the List type in this class.

We looked at method readBook, pg. 775, and the use of indexOf to locate a word in words in the next paragraph.

We went through the steps of Figure 13.1, pg 777, to understand how binary search works.

With an ArrayList<String> words, there are two ways to locate a certain word:

Int index = words.indexOf(word);  // return index of word, or -1 if not there

Or, if words is sorted, we can search faster with binarySearch:

Int index = Collections.binarySearch(words, word);  // return index of word, or -1 if not there

If words isn’t sorted, we can sort it with Collections.sort(words).

We looked at FindWords.java from the text’s online site. It is similar to WordChallenge, pg. 778.  Both of them read the file words.txt available on the site into an ArrayList<String>, with the help of a Scanner. Then they locate a word “target” in the list by using Collections.binarySearch(words, target). 

The book then covers sorting of arrays and ArrayLists:

Arrays.sort(strings);  // sort an array of Strings named strings

Collections.sort(list); // sort an ArrayList<String> named list

We’re stopping short of “Shuffling”, though it is nice to know that the JDK can randomize an ArrayList for us.

Also, we’re not covering the following material on Comparators.

The summary of Table 13.1 may be useful.

Now back to Chapter 9 to cover Object Inheritance.

Previously we studied Sec. 9.5 on Interfaces, and also pp 572-578 on the Object class and equals.

Now we start from Sec. 9.1

See class Employee, with hours, salary, vacationDays, and vacationForm of various colors.

Class Secretary is originally just the same as Employee except for an additional method takeDictation.

If we make Secretary a subclass of Employee, it can inherit all the Employee methods, and we only have to write down the additional method takesDictation.

Then Employee is the superclass, and Secretary is the subclass, the more specialized class.

We looked at the Employee.java and Secreatary.java in the textbook’s website, which are slightly different from the ones in the book, but follow the same ideas.

See the code for Secretary on pg. 562. Note the “extends” keyword, that makes Secretary a subclass of Employee, and thus lets it inherit all the methods of Employee.

Look at EmployeeMain on pg. 562.

We see that Employees and Secretaries can be constructed and used in the usual way. The Secretary object uses the inherited methods from Employee, plus its own method.

Overriding Methods

Secretaries have the ordinary Employee salary, hours, etc., but another subclass of Employee, Lawyer, has more vacation days than the ordinary employee.

So Lawyer, which also extends Employee, has its own getVacationDays() that provides different code than getVactionDays() of Employee, to provide 15 days of vacation instead of just 10.  This is a case of a subclass method that overrides the superclass method of the same name and parameters.  When getVacationDays() is called on an object of type Lawyer, it returns 15.  The more specific implementation is used, based on the type of the object itself.

ColoredPoint Example

Recall that a Point has state (x,y)

A ColoredPoint has state (x, y, color), for ex. Blue point at (1,2), red point at (2,1)

This can be done by inheritance from Point.java, it its final version, pg. 530.

The following code is close to that on pg. 1080:  Here a ColoredPoint has a color by its own field and an (x,y) position by its superclass fields in Point.

public class ColoredPoint extends Point {

  private Color color;

  public ColoredPoint(int x, int y, Color color) {

    super(x,y);  // call superclass constructor this way

    this.color = color;   // here we handle color

  }

  public Color getColor( ) { return color; }

}

Now using this setup, a ColoredPoint can do anything a Point can do:

   ColoredPoint cp = new ColoredPoint(1, 2, Color.BLUE);

   cp.translate(1,1);  // move Point to (2,3), still blue

and we can find out its color:

   Color c = cp.getColor();