Technical University of Denmark DTU
02115   Java Programming
Autumn 2011

Week plan #1

For the week   29. August - 2. September


Welcome

Welcome to our Java Programming course.
Our first job is to find the most appropriate weighting of material from the two course textbooks depending on the preknowledge of Java among the course participants.

Usually we have a mix of student qualifications of Java. So it's impossible at the lectures to find a track suited for all of you. But the section After the lectures of the week plans will give useful references to both textbooks.


Introduction

Before covering projects from BK - the textbook 'Objects First with Java' by Barnes and Kölling) - I will use an example from PS (the textbook 'Java Precisely' by Peter Sestoft) to introduce objects and classes and the idea of cooperating classes.

An object of some class contains data. A class defines the data of the corresponding objects, mechanisms to create objects of that class and the possible operations to be performed on these objects.

An object oriented solution to a problem will often involve a number of cooperating classes. To illustrate that I will sketch how to define a polygon in a two-dimensional (x, y)-coordinate plane based on a representation as a sequence of points. That requires two classes, a Polygon class and a Point class.

A Polygon-object can then be considered as a sequence of Point-objects representing the nodes of the polygon. The Polygon-class then has to define the sequence of Point-objects, a mechanism to construct a polygon and the operations to be performed on a polygon. By now we will not present a Java class declaration of the Polygon-class but turn to the PS Example 12 (page 11) where two Point-objects - p1 and p2 - holding a (x, y)-pair of coordinates are constructed (such two Point-objects could actually define an edge of a polygon):

    Point p1 = new Point(10, 20); Point p2 = new Point(30, 40);
    System.out.println("p1 is " + p1);      // Prints: p1 is (10, 20) 
    System.out.println("p2 is " + p2);      // Prints: p2 is (30, 40)
    p2.move(7, 7);
    System.out.println("p2 is " + p2);      // Prints: p2 is (37, 47)
First, the objects refered to by p1 and p2, respectively, are created. Then they are printed out. The textual representation of a Point-object is defined by a method of class Point, see below. At last the p2-object is subject to a move operation where both actual coordinates 30 and 40 are incremented by 7 and afterwards it is printed out.

The corresponding class Point, PS Example 24 (page 21), looks like this:

    class Point {
      int x, y;

      Point(int x, int y) { this.x = x; this.y = y; }

      void move(int dx, int dy) { x += dx; y += dy; }

      public String toString() { return "(" + x + ", " + y + ")"; }
    }
and defines the following parts: The declaration of class Point is a simple class declaration, PS page 20, having the form

        class C
              class-body

with a class-body holding a field-declaration, a constructor-declaration, and two method-declarations enclosed in a { }-pair.


How could the class Polygon cooperate with the class Point when we introduce operations on a polygon ?

If we e.g. would like to move a polygon, class Polygon should be supplied with a move-method. A textual representation of the nodes of a polygon could be achieved by puting a toString-method into the class Polygon. Both methods should cooperate with the corresponding methods of class Point in a simple way: Go through the sequence of Point-objects representing the polygon and apply to each Point-object the actual method of class Point, that is move or toString respectively.

In another context it could be useful to present a drawing of a polygon. This job should be done by some methods of a new class, which just asks the Polygon-class to "hand over" the sequence of points representing the polygon to be drawn. In order to do that the Polygon-class must be supplied with an appropriate getPoints-method to be used in the new class. The drawing might then be done by drawing straight line segments between each pair of nabouring points of the sequence.


Preparations for the week

It's important to get a good start. To achieve that we expect you to
  1. Install Java and BlueJ at your home computer
  2. Get the Project files from the BK textbook
  3. Get the Example programs from the PS textbook
  4. Work with the sections of BK as refered below
The simplest way to do the first three points above is to visit the links at the course homepage stated as emphasized text and follow the instructions given. If you don't have Java available at home, then as the very first install the latest Java version using the links at the BlueJ homepage.

If you have not yet bought the textbooks then just continue with the section Exercises at the PC's and work with the Project files from Chapter 1 of the BK textbook.


Do the BK textbook covering as a minimum the first two and a half pages of 'Preface to the instructor' (pages xix - xxi), the 'Guided Tour' (pages xxvii and xxviii), and the Chapters 1, 2 and 3 to the following extent:

  1. Chapter 1, 1.1 - 1.8; pages 3-10:
  2. Chapter 2, 2.1; pages 18-19:
  3. Read Chapter 3, 3.1 - 3.5; pages 56-59:


The lectures

We will cover selected parts of the textbooks, primarily the following parts of BK
  - Chapter 1, pages 3-15
  - Chapter 2, pages 18-51
  - Chapter 3, pages 56-83


Exercises at the PC's

Do a logon and get the Projects files from the BK textbook and the Example programs from the PS textbook as mentioned on this week plan in the section Preparations for the week above.

You get access to the BlueJ environment by choosing a xterm window and then typing
      bluej &
in reply of the prompt.

In the BlueJ environment the project files from the BK textbook are accessible from the menu bar by choosing the file catalog where you previously stored the project files.

Exercises are taken mostly from the BK textbook:




After the lectures

The lectures has not covered all details of the chapters 1 - 3 of BK. Work through the missing parts and do the exercises missed at the exercise class, refer to the section above, with the supplements mentioned below as a minimum.

At the end of this week you are supposed to be familiar with the following parts of the BK textbook:


In the PS textbook read the following sections:
Jens Thyge Kristensen, Email: jtk@imm.dtu.dk

Newest edition:   18. August (The Java representation of the Examples 12 and 24 has been added)
Previous editions:
    - 17. August
    -  8. April (just the heading and footing)