Lecture and Lab 8
Selection Sort
In class design. To be included in this week's homework.
In-class exercise
Do
this exercise.
Inheritance: extending existing classes
- How complex a class definition may be? Let's look at the
class
Point
in the
java.awt package (what you import in the beginning of any Java program
that uses graphics).
How does it compare with the class myPoint defined by you during
last week's lab?
- What if we wanted to take advantage of all the methods
implemented in the java.awt.Point class, but also include
other desirable features (such as shape and color, as in
last week's lab)? The solution provided by Java is
class inheritance.
public class myPoint extends Point
{
int shape;
int color;
...
}
Notice that:
int x;
int y;
no longer appear explicitly as class variables of myPoint. But
they are present, and are inherited from the Point class.
-
Example
of the new class myPoint and how to use it.
- Overriding methods. A class which extends another
class inherits the parent's methods. But it can also
override them. This means, defining a method with exactly
the same name as a method from the parent.
For example, the java.awt class Point has a method to test
when two points are equal. This is done by comparing the
corresponding x and y coordinates. We can override this method in
the class myPoint and have equals also test for
equality of the shape and color of a point.
boolean equals(myPoint p)
{
if (x == p.x && y == p.y && shape == p.shape && color == p.color)
return true;
else return false;
}
More on Inheritance and Extending Objects
- Use of super in constructor of a class which extends another class.
- When you are using paint, mousePressed, actionPerformed, etc., you are overriding some
method from the class that you are extending.
Overview of new Hwk 4
Discussion of new Homework 4.
- Insertion Sort. Applet, from Dominique Thiebaut's web page.
Input: an array A of integers, of size n
Output: in the same array A, the numbers in increasing
order.
Algorithm:
The algorithm works in n-1 iterations by scanning the
array from left to right. At iteration i,
the ith element is considered and inserted in the part of the array
that has already been sorted (from 1 to i-1).
For this, the
algorithm first searches the array from 1 through
i-1 to find where the ith element would fit, then
shifts to the right those elements larger than it to make room
for its insertion in the array.
- Developing the program by stepwise refinement.
We will develop the program in stages, by stepwise refinement,
first on paper, as pseudo-code, then implement it. The
pseudo-code development should roughly follow the same structure
as the one I gave you for Selection Sort, starting with a general
description in English, then refining it to include functions. All
parameters to the functions should be clearly identified as IN,
OUT or IN/OUT.
In the stepwise refinement of your program for insertion sort,
you should have
separate functions for searching and for shifting,
invoked when inserting the current element.
The main program (applet)
should be similar to the one for Selection
Sort:
get input via mouse clicks, sort, then print and draw the result.