ANU The Australian National University



____________________________________________________

[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]

____________________________________________________

COMP2100/2500
Lecture 4: Java Revision

Summary

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:


Object-oriented programming


Java syntax


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:


More Information

Some useful DrJava links:

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