02115 Java Programming | ||
Autumn 2011 |
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.
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:
int x, y;
of the
coordinate pair, an example of a field-declaration,
holds the data of a Point
-object,
Point(int x, int y) { ... }
, which is
a constructor-declaration used to construct
Point
-objects holding
(x, y)
-pair of coordinates given by the
parameter values
int x, int y
(and stored in the corresponding
fields identified using the prefix this.
in
order to distinguish them from the parameters), and
move
and toString
, respectively.toString
method converts the two integers
x
and y
of the coordinate pair
into a textual representation - as e.g. (10, 20)
-
used when printing a Point
-object given by the
data stored in the fields x
and y
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.
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:
The functionality of the clock display is then achieved by methods which combines the use of methods applied to the two number displays.
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:
class
LabClass
:Polygon
class (take ideas from the LabClass
) and a
Point
class as that of Example 24 of
PS. Think of making a user friendly constructor
which allows you to create polygons having an arbitrary
number of vertices. A file holding the coordinates might
be useful (only relevant for the experienced student).
At the end of this week you are supposed to be familiar with the following parts of the BK textbook:
s1
and s2
are expressions of type
String
and v
is of any type, then ...
String
library class
than the substring
method introduced in BK
on page 49.
main()
method of a class is an example of a
static member of a class and will be introduced later in the
course. Skim Example 6 at page 9 leaving out the details
inside the { }
bracketsa = 0; b = 1/a;
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)