This symbol indicates something that can be used either on the computer as practice or as an assignment.
This symbol indicates something that can be used either as a homework exercise or test question.
It is best not to print this page. The applets will either not show up or mess up the printing, depending on what browser you are using. The class listings that you can pop up from the applets also can't be printed from the windows. You can get a printable version of this web page and can download the classes. All the Java classes used in this resource are found there. You can download any that you want.
field | a variable that is part of a class |
method | a function that is part of a class |
constructor | a special method that is called when an object is created |
superclass | the class that is inherited from (the parent class) |
subclass | the class the does the inheriting (the child class) |
extends | in Java the keyword extends means that a class will inherit from another class |
overload | a method is overloaded if there are two or more methods with the same name in a class. Each overloaded method has a different set of parameters. That's how you can tell which one will get called. |
override | a method is overridden if there is a method in the subclass that has the same name and the same set of parameters. The superclass method is then NOT inherited. |
When you move the cursor into these applets you'll notice the pointer turns into a hand. This is to let you know that each time you press the mouse button on the white part of the applet you'll go to the next step in the example.
|
Create an applet that displays a blue Car near the left edge of the applet.
Change the size of the car to 100 x 50. Display a pink Boat near the right
edge of the applet.
Hint: If you are unfamiliar with using Applets here is a template you can use to help you get started. |
An AsideDon't Panic if this doesn't make sense. |
If you've been looking at the source code in the previous examples you may have wondered at some strange symbols appearing in the comments in the listing. In some places in the comments you will see some HTML tags. In others you will see the @ symbol. These symbols are used by a program that comes with Sun's Java package. The program is called javadoc. It makes nice external documentation in HTML format. It helps to illustrate inheritance heirarchies and what exactly gets inherited and from where. I've used it for several of my classes. Check out the javadocs for this resource. Sun provides documentation like this for the whole language. I find the more Java I learn the more I depend on their documentation to see what methods are available and how to use them. It's on the net at the Java website. You can download this information to use offline at Franck Allimant's website. This is not as necessary as it once was but sometimes it is nice to have the info offline (and may also be quicker to find the class you want. |
|
Create an applet that displays an orange Ferry near the middle of the applet.
Place two buttons in the applet. The buttons will say Larger and Smaller.
If the user clicks on the larger button increase the ferry's width and
height by 5. If the user clicks on the smaller button then decrease by 5.
Hint: If you are unfamiliar with using Buttons in Applets here is a template that handles the Buttons for you. |
|
Create a new class called SuperFerry. It will extend Ferry. A
SuperFerry is like a Ferry except that:
|
Consider the following applet. It will illustrate another interesting facet of inheritance. It will display the vehicle of your choice. When you press the button labelled Move & Change Colour the vehicle you've selected will get moved to a random location and cycle throught the colours red, green and blue. The interesting thing is that the variable used to move and change colour is of type Vehicle. We can use that one variable regardless of what type of Vehicle we are using. Try out the applet and we'll get to more details following it.
At the beginning of the applet we have the statement:
Vehicle v;Here is the code that creates the vehicle when you press a button (in the method actionPerformed):
if (e.getSource() == vehicleButton) v = new Vehicle(30,50,60,40,this); else if (e.getSource() == carButton) v = new Car(30,50,60,40,this); else if (e.getSource() == boatButton) v = new Boat(30,50,60,40,this); else if (e.getSource() == ferryButton) v = new Ferry(30,50,60,40,this); v.setVisible(true);If Java is so particular about types, how can we get away with this? Well, it's not like trying to assign a double to a char for instance. These types are related to each other because they are all in the same inheritance heirarchy. The rule is that any superclass variable can hold a reference to itself or any subclasses. So a variable of type Vehicle can hold a reference to any of Vehicle, Car, Boat, Ferry or SuperFerry.
Answer |
a) What type of references can a Car hold? b) What type of references can a Boat hold? |
So why not use the Mega-Superclass Object as the type for all our references? Since it is the superclass of all objects we can assign any object to it. What would have happened in the previous applet if we had declared v to be of type Object instead of Vehicle? As it turns out not much. Although we can create the Vehicles we can't use them. The only methods we can use are ones that are defined in Object, not Vehicle or its subclasses. Object doesn't have methods like move or setVisible. When v is defined as type Vehicle we can use any methods that are defined in Vehicle or its superclasses (in this case Object is the only superclass). This concept is so snazzy it has an equally snazzy name. It is called polymorphism.
Answer |
Suppose we had the following statement: Vehicle v = new SuperFerry(this);
State which of the following statements would be valid. For those that are invalid state why.
|
Programming question #4 (all 3 parts) is something I usually use as assignment after we've played with the vehicles for awhile. It usually takes a couple of weeks for the students to work on. They try to come up with a creative way to use the Face, HappyFace and Bunny classes to demonstrate that all the methods work. Part of the mark is based on creativity and originality. Often they decide to make their own subclasses on top of what is required.
|
You are going to create several different kinds of Faces. The base class will be called Face.
It will be very similar to the Vehicle class we were looking at. A Face looks like
this: Here is a list of the fields:
Write an applet that demonstrates that you have successfully implemented all these methods. |
|
You are now going to make a different kind of Face. This class will be called HappyFace.
It will be very similar to the Face class. It will have a smile as well as eyes. It looks like this: Its methods will be a little different as well. Here is a list of those methods:
Write an applet that demonstrates that you have successfully implemented all these methods. |
|
You are now going to make another subclass of Face. This class will be called Bunny.
It will be very similar to the Face class. It will have bunny ears and a small mouth as well as eyes.
It looks like this:
It will have some additional fields. They are:
Its methods will be a little different as well. Here is a list of those methods:
Write an applet that demonstrates that you have successfully implemented all these methods. |
Answer |
Consider a class that has the following form:
public class MyDate { int year, month, day; /** Only the method signatures are given **/ public MyDate() // a method to intialize a date to Jan. 1, 1900 //set all fields. 1=Jan.,2=Feb.,... public void setDate(int yr, int mn, int dy) // returns a string in the form July 5, 2001 public String toString() // allows the entry of a date from the keyboard public void enterDate() } Answer the following questions:
|
Answer |
Consider the following class:
public class Sequence { double a; public Sequence() {a = 0;} public Sequence(double t1) {a = t1;} public void setT1(double t1) {a = t1;} public double t(int n) {return ((n - 1) + a)* n;} public String toString() {return ""+a+", "+t(2)+", "+t(3)+", ...";} } Answer the following questions:
|
This concludes the first part of this resource. You are now ready to go to Part 2. It discusses inheritance in the context of the Java language, particularly with respect to GUIs and Swing. Students tend to really go for this stuff.