Reading:
This chapter contains conceptually complex material that is central to the course, so it is worth spending time going through it carefully. Do these exercises as you finish reading each section and if things don’t make sense, ask questions, post to the discussion forum, send emails.
As with previous chapters, the Processing-specific material is collected in the example sections; the main sections are general, Java-based in nature.
Focus here on Sections 9.2.1-9.2.2.
Focus here on Sections 9.3.1-9.3.2.
Implementing Class Attributes
private
and wrap them in a Coordinate
class following
the model of the temperature class detailed in the text.
Implementing Class Behaviors
There are quite a few sub-sections here. On the first round, the most important are those on the
toString()
method, the constructors (default and explicit-value) and the accessors. Start by understanding those, and then
move to the other more detailed discussions of the Temperature class behaviors.
toString()
method for your coordinate class. It should print a coordinate
x, y as:
(x, y)
And this program should print out the following:public class CoordinateConsole { public static void main(String[] args) { Coordinate c1 = new Coordinate(); // Construct a default coordinate. System.out.println(c1); // Print out that new coordinate. } }
(0.0, 0.0)
And this program should print out the following:public class CoordinateConsole { public static void main(String[] args) { Coordinate c2 = new Coordinate(1, 2); System.out.println(c2); } }
(1.0, 2.0)
And this program should print out the following:public class CoordinateConsole { public static void main(String[] args) { Coordinate c2 = new Coordinate(1, 2); System.out.println(c2.getX()); System.out.println(c2.getY()); } }
1.0 2.0
This section discusses the interaction of multiple class objects. For example, consider the case of adding two coordinate objects together where we would view the result at the sum of the vectors represented by the coordinates. For example, adding the coordinates (1, 2) and (3, 4) would result in a new coordinate (4, 6).
myX
and myY
, the following method implements the coordinate summation method:
Given this method, a programmer could write:public Coordinate add(Coordinate otherCoordinate) { return new Coordinate(myX + otherCoordinate.getX(), myY + otherCoordinate.getY()); }
And this program should print out the following:public class CoordinateConsole { public static void main(String[] args) { Coordinate c2 = new Coordinate(1, 2); Coordinate c3 = new Coordinate(3, 4); System.out.println(c1.add(c2)); } }
(4.0, 6.0)
Play with this example for a bit to make sure that you understand the implementation. Pay attention to a couple issues:
add()
method must receive an “other” coordinate and add “itself”
to that other coordinate. In addition, it does not modify either itself or the other coordinate; instead, it
constructs a new coordinate and returns that as a result.
c3 + c4
, the programmer is required to write c3.add(c4)
.
This is a common feature in object-oriented languages.
This chapter focuses on the Processing-based example. Go through it only if you’re interested in classes in Processing.
Just read this short section and look forward to Chapter 11!