Trouble Learning Java?
Why Not Try:
Free On-line Java Tutorials
COSC121 Logo

Sample code

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 study aid.

When you have finished the tutorials, check out my Website:

Sample code
1 Contains a simple class Person for representing the concept of a human being, and a driver class PersonTest. Key concepts:
  • Defining and calling a method.
  • Defining and accessing a property.
  • Defining and calling a constructor.
2 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 CitizenTest. Key concepts:
  • 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.
3 An example involving three classes Flea, Dog and DogOwner to represent the idea that a dog has a flea and a dog-owner has a dog. The class DogTest is the driver class. Key concepts:
  • A property of a class can itself be a reference to another class, setting up a relationship between the two classes.
4 Two classes SportsShoe and Runner for modelling runners and their shoes, together with a driver class called RunnerTest. Key concepts:
  • Static methods versus non-static methods.
  • Overloading two methods.
  • Methods for swapping properties of two objects.
5, This tutorial attempts to convey the meaning of the static keyword by showing firstly an example where static is used and then secondly an example which is the same example with static removed.
6 This tutorial gives you practice in writing for loops. A for loop has the following structure:
   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.
7 Java's three different looping constructs: for, do and while. This tutorial takes you through the process of rewriting a for loop into a do loop and a while loop.
8 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: int, long, short, byte, float, double, char and boolean.

The file gives you practice using arrays of the built-in types, and the file gives you practice using arrays of references.


Advanced tutorial involving two classes Car, and Driver for representing automobiles and their owners, respectively. Also the class CarTest is 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 Turtle objects, with the up field serving to connect one Turtle object to another. If a Turtle object has a non-null 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 contains four classes: Animal, Bird and Eagle and a tester class InheriTest. Class Bird inherits from Animal and class Eagle inherits from Bird.

The main thing that this tutorial shows is the idea that inheritance is a non-symmetrical relationship. For example: the Bird class inherits from the Animal 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 this.


Practical example of inheritance.

The file is comprised of three classes: XWing, TieFighter and StarWars. The first two represent spacecraft from the two sides of the Star Wars films. The class StarWars 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.

14 Arrays, inheritance and polymorphism.

Last modified: Thu Jul 21 08:56:00 NZST 2016
© Copyright 1998-2016 Davin Pearson.