The following tutorials were written by Davin Pearson as
supplementary material to the University of Canterbury COSC121 course teaching the
Java language. At the top of each tutorial is a hyperlink to a
model answer, but don't look at the answer until you have completed
all the questions of the tutorial!
DISCLAIMER: These tutorials were not developed by TEAM COSC and
are independent any COSC course. They are solely intended as a
When you have finished the tutorials, check out my Website:
Contains a simple class
Person for representing the
concept of a human being, and a driver class
- Defining and calling a method.
- Defining and accessing a property.
- Defining and calling a constructor.
Contains a class
Citizen for storing a person's
financial information including their salary, savings
and loan (if they have one). Tax is paid by the citizen
and a class called
Government is used to keep track
of the total tax paid. The classes are driven by
- How to pass arguments into methods.
- How to use a method's return value.
- Ignoring the return value of a method.
- Overloading two constructors.
- Accessing a static property.
- Calling a static method.
An example involving three classes
DogOwner to represent the idea that a dog has a flea
and a dog-owner has a dog. The class
DogTest is the
- A property of a class can itself be a
reference to another class, setting up a
relationship between the two classes.
Runner for modelling
runners and their shoes, together with a driver class
- Static methods versus non-static methods.
- Overloading two methods.
- Methods for swapping properties of two objects.
This tutorial attempts to convey the meaning of the
static keyword by showing firstly an example
StaticTest.java where static is used and then
secondly an example
StaticTest2.java which is the
same example with static removed.
This tutorial gives you practice in writing
for loops. A
for loop has the following
for (Init-part; While-part; Increment-part)
This tutorial shows you how the different parts of the
for loop affect the behaviour of the loop.
Java's three different looping constructs:
while. This tutorial takes you through
the process of rewriting a
for loop into a
loop and a
|String manipulation. Useful methods to remember are
length() for getting a string's length, and
charAt(NNN) for inspecting the character at location
NNN in a string.
Arrays. There are two kinds of arrays: arrays of
references, and arrays of the built-in types. The
built-in types are:
ArrayTest.java gives you practice using
arrays of the built-in types, and the file
ArrayTest2.java gives you practice using arrays of
Advanced tutorial involving two classes
Driver for representing automobiles and their
owners, respectively. Also the class
used for testing the other two classes.
WARNING: Difficult tutorial.
Linked lists. Dr Seuss' story Yertle the Turtle
describes how a turtle called Yertle sits at the top of
a pile of other turtles. In this example, the pile of
turtles is represented by a linked list of
objects, with the
up field serving to connect one
Turtle object to another. If a
has a non-
up field, then this represents a
turtle that is sitting on top of the current one. The
last turtle in the linked list is the turtle that is at
the top of the pile.
WARNING: Difficult tutorial.
Theoretical example of inheritance. The file
InheriTest.java contains four classes:
Eagle and a tester class
Bird inherits from
Eagle inherits from
The main thing that this tutorial shows is the idea that
inheritance is a non-symmetrical relationship. For
Bird class inherits from the
class, which corresponds to the idea that "every bird is
an animal". The reverse, "every animal is a bird" is
plainly not true! Inheritance forces you to recognize
Practical example of inheritance.
StarWars.java is comprised of three
The first two represent spacecraft from the two sides of
the Star Wars films. The class
is the driver class and contains code for executing
a battle between the X-Wings and the Tie Fighters.
The tutorial shows how inheritance can
be used to reduce the amount of duplication of code.
|Arrays, inheritance and polymorphism.