CIS 121 February 4, 2000
You will find a statement of Programming Assignment 2 on both
the web page for this course, and the general web page for CIS 120 and 121.
Recall that this assignment is due in one week. I strongly encourage you
to follow the suggested list of stages. In order to test a method, I
suggest creating a class called test, which has a main method. Use this
class to implement a task, until you get it working. This class shouldn't be
used when you write your actual class. It should only be used to test.
You may use code from Module 7 of the CIS 120 notes to sort the array of words.
In your assignment, you will need to perform a search. Since you are asked
to sort the array, the most efficient search is a binary search. You may
use the code from Module 7 of the CIS 120 notes if you want. Recall the
idea of a binary search.
Initialize a low variable to the beginning of the array.
Initialize a high variable to the end of the array.
loop:
Average the two and examine the element in the middle.
If the word we are searching for is in the middle, then we are done.
If the word we are searching for is alphabetically after the one in the middle,
then the low variable becomes one position after the middle.
If the word we are searching for is alphabetically before the one in the middle,
then the high variable becomes one position below the middle.
Go back to loop until we find the word we are searching for.
- Use a class from the JDK if it exists
- Buy a prewritten package
- Create your own class using inheritance
- Write your own
The Extends clause
The Extends Clause allows us to use inheritance. When we define a class
we can use the extend clause to create subclasses. For example, in the
definition
public class B extends A {
There are different ways of stating the relationship between A and B.
- Class A is called the super class of class B.
- Class A is called the parent class of class B.
- Class B is said to inherit from class A.
- A is called the base class from which B is derived.
Even though we don't state it explicitly, we use inheritance whenever
we create an object. In the JDK, there is a class called Object. This
class is the base class of every class we define without an extends clause.
In class definitions where we use the extends class, the Object class
is at the top of the class hierarchy.
An example of inheritance is the use of toString().
When we inherit from a class, we inherit its methods. We are not
required to keep the same implementation of the method. If we change the
implementation, then we are overriding the implementation. We are allowed to change
the implementation of the method, but we must have the same signature.
Whenever we call a method, the rule is that the interpreter begins
by looking at the object from which the method is called. If it does
not find an implementation of that method, then it goes up the hierarchy
until it finds one.
Examples of inheritance.
Example
Polymorphism is the concept of method calls taking on different forms.
A method is called a Concrete Method if and only if it has
an implementation. A class is called a Concrete Class if and only
if all of its methods are concrete.
A method is called an Abstract Method if and only if it has no
implementation. A class is called an Abstract Class if and only
if at least one of its methods is abstract.
Abstract Classes are classes in which at
least one of its methods is abstract. We can't create objects of Abstract
Classes.