Here are some Java tricks and pointers that might help you with the homework.
- If you are a C++ programmer with little or no Java experience, do
not make the mistake of thinking that "Java is C++ without pointers,"
or "Java is C++ with everything as a class," etc. Many of these
simplifications are wrong and will lead you astray. The first
statement, "Java is C++ without pointers," is especially hazardous to
your health. In Java, unlike C++, all objects and arrays are created
on the heap and are not in the stack. The stack only contains
primitive types and references to objects. When you declare Bozo x
= new Bozo();, you are creating an object on the heap and
declaring that x is a reference to it. When you pass x
around, assigning it to different variables and passing it to
functions, it is passed by value. So if I say Bozo y = x;, y refers
to the same Bozo as x does. But the following method has no effect:
public void swap(Bozo x, Bozo y) {
Bozo temp;
temp = x;
x = y;
y = temp;
}
- If you want to copy arrays quickly, use System.arraycopy().
- Vectors can save a lot of the bookkeeping hassle required to use arrays
correctly, and they are very efficient.
- Some Java classes support the clone() method, but you must be
careful before using it. A clone() usually creates a shallow copy of
an object. So, if you clone() a Vector, you get two different Vectors
with independent internal data structures (i.e. you can delete
elements from one Vector and they will remain in the other Vector),
but the objects the Vectors contain are not duplicated - they both refer
(at least initially) to the same set of objects.
- Use the SolutionDisplay class to display your answers graphically and
save yourself the hassle of learning AWT or Swing (Java's display system)
unless you want to.