[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]
COMP2100/2500
Lecture 4: Java RevisionSummary
A brief overview of object-oriented programming in Java, aimed at students who have completed first year successfully. Hopefully this will also be of some assistance to students who haven't used Java before.
Aims
After attending this lecture, you should:
Be familiar with elementary object-oriented programming concepts such as classes, objects, fields & methods, inheritance.
Be aware of basic Java syntax, the different parts of a class, how to call a method on an object, assignment, object creation, conditionals, loops.
Understand some basics of interaction with the computing environment: files and directories, editing, compiling and running programs (both in DrJava and using an editor and the command line), looking up the Java API documentation.
Object-oriented programming
Classes and objects
Every object is an instance of a class.
An abstract class is not completely defined; it has no creation clause and can have no instances. An interface is similar, but it can't have any instance fields or concrete methods.
Fields & methods:
Fields store data
Methods perform computation
Clients and suppliers: the has a relationship, via object references
Inheritance: the is a kind of relationship
Compilation and execution
javac class-name...
java class-name
Java syntax
How to call a method on an object:
object.method();
this.method(); or equivalently just method();
If the command takes arguments, then object.method(arguments);
For methods that are functions (returning a result), a feature call is an expression not a complete command. It can be used in any place where an expression is appropriate:
x = object.method();
System.out.println(object.method());
If the method returns an object reference, then you can chain the dots together: object.method().method();
Assignment instructions: variable = expression;
Object Creation
The creation operator: new, e.g. Scanner s = new Scanner(System.in);
Conditionals
if (condition) { instructions to carry out if condition1 is True } else { instructions to carry out if condition1 is False }Loops:
While loops
while (continuation condition) { instructions }For example:
while (s.hasNextInt()) { System.out.println(s.nextInt()); }For loops
for (initialisation; continuation condition; increment instruction) { instructions }For example:
for (i=0; i<a.length; i++) { System.out.println("a[" + i + "] = " + a[i]); }
Example
First an example class Counter (which must be in a file Counter.java), implementing a “tally counter” abstract data type. A tally counter is like a little gizmo that a farmer might use to count sheep passing through a gate. It has a button to click to increment the count, and another button to click to reset it to zero.
class Counter { private int count; public Counter() { this.reset(); } public void reset() { this.count = 0; } public void increment() { this.count++; } public int getCount() { return this.count; } }Now an example client class TestCounter (in a file TestCounter.java, which will create a Counter object and make use of its various capabilities.
import java.util.Scanner; class TestCounter { public static void main(String[] args) { Scanner input = new Scanner(System.in); Counter counter = new Counter(); boolean finished = false; System.out.println("Welcome to the Counter Tester"); while (!finished && input.hasNextLine()) { String s = input.nextLine().trim(); if (s.equals("q")) { finished = true; } else if (s.equals("i")) { counter.increment(); } else if (s.equals("r")) { counter.reset(); } else if (s.equals("p")) { System.out.println(counter.getCount()); } else { System.out.println("Unknown command: \'" + s + "\'"); } } System.out.println("Goodbye"); } }I will do a live demonstration of working on this system using the DrJava environment and also Emacs plus the command line. I will show how to:
add some extra methods to class Counter including overloading the constructor;
add a main() method to class Counter for self-testing, thus eliminating the need for “test harness” in a separate class like TestCounter;
edit, compile and run the program in DrJava; and
edit, compile and run the program using Emacs and the command line.
More Information
Some useful DrJava links:
The DrJava web site, which includes a comprehensive documentation section and an FAQ.
The instructions for COMP1100 Lab 2. (Not all of it, but there are a couple of relevant sections.)
A local copy of the DrJava user documentation. Use this instead of downloading it from the DrJava site and costing the university money.
For more detailed and definitive information on the material covered in this lecture, consult your first-year textbooks, lecture notes, labs and assignment.
There is much more information about Java available from the Java Page. You may also find the websites for COMP1100 and COMP1110 helpful.
[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]
Copyright © 2005, Ian Barnes, The Australian National University
Version 2005.4, Monday, 28 February 2005, 12:14:02 +1100
Feedback & Queries to
comp2100@cs.anu.edu.au