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:


davin.50webs.com

Sample code
NumberFile(s)Notes
1 PersonTest.java 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 CitizenTest.java 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 DogTest.java 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 RunnerTest.java 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 StaticTest.java,StaticTest2.java. 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.
6 ForTest.java This tutorial gives you practice in writing for loops. A for loop has the following structure:
   for (Init-part; While-part; Increment-part)
      Body-part
      
This tutorial shows you how the different parts of the for loop affect the behaviour of the loop.
7 LoopTest.java 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 ScrabbleTest.java 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.
9 ArrayTest.java,ArrayTest2.java

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 ArrayTest.java gives you practice using arrays of the built-in types, and the file ArrayTest2.java gives you practice using arrays of references.

10 CarTest.java.

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.

11 TurtleTest.java

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.

12 InheriTest.java

Theoretical example of inheritance. The file InheriTest.java 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.

13 StarWars.java

Practical example of inheritance.

The file StarWars.java 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 AnimalTest.java Arrays, inheritance and polymorphism.

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