Computational Methods Junior Honours : Checkpoint 3
Object assignment syntax
· Java assigns instances of objects
(classes) differently from basic types like “int” and “double”.
int i, j;
i=2;
j=1;
i=j;
j=0;
// Results in i=1,
j=0.
· However with classes, (see
ComplexTester.java) the behaviour is less obvious. Assigning classes with an
An
equals sign assigns by reference. This means that after A=B, the variables “A”
and “B” refer to the same instance.
A
unique instance can only be obtained using “A = new Complex” in some form.
This
former has unintuitive side effects illustrated in ComplexTester, and should be
avoided.
The
latter should always be used unless you know what you are doing.
public static void
GotchaObjectAssignment() throws IOException {
Complex A = new Complex(2,0);
Complex B = new Complex(1,0);
A =
B;
B.set(0,1);
System.out.println("A "+A);
System.out.println("B "+B);
};
Yields A (0.0,1.0)
B (0.0,1.0)
public static void
CorrectObjectAssignment() throws IOException {
Complex
A = new Complex(2,0);
Complex B = new Complex(1,0);
A =
new Complex(B);
B.set(0,1);
System.out.println("A "+A);
System.out.println("B "+B);
};
Yields A (1.0,0.0)
B (0.0,1.0)
· The reason for this is that Java
“objects” such as “A” or “B” underneath are really “pointers” that refer to a
bit of memory storing the object data
· A “new” chunk of memory is obtained
whenever an object is assigned with a “new” constructor call, such as “A = new
Complex();”
· If “A=B” is used, then both “A” and “B” refer to the same
chunk of memory, and modifying “B” also modifies “A” because they now really
refer to the same thing
· This is counter-intuitive and
should be avoided like the plague.
Starting point: Complex class
Complex.java defines a public class for
complex variables.
ComplexTester.java
contains an example test code that uses the complex class
javac
Complex.java ComplexTester.java
java
ComplexTester
- Understand
how the output is generated.
- EXPLAIN
the two object assignment examples in ComplexTester to your demonstrator,
- EXPLAIN
why copy constructors should be used for object assignment.
Your task: Vector class
- Create a
file called Vector3d.java in a similar style to Complex.java. Write a
class Vector3d with
1. Coordinates
x,y,z
- getter
and setter methods - these are standard Java style conventions that are
required for some of the optional assignments to work.
This is a good habit to get into because lots of
Java tools assume this naming convention is used and break if you do not use
it.
- double
getX()
- double
getY()
- double
getZ(),
- void
setX(double)
- void
setY(double)
- void
setZ(double)
3. A
default constructor Vector3d()
that assigns the coordinate to zero
4. A
constructor Vector3d(xx,yy,zz) that
initialises the coordinate to (xx,yy,zz)
5. norm2 &
norm
methods
6. toString()
method (this enable Java to automatically print a Vector3d in methods like println)
- static
methods to perform add,sub,cross,dot operations on two Vector3d's:
8. a
static method for mult and divide by double
- It
is probably best get Vector3d to compile (javac Vector3d.java) before moving
on to writing your test code below.
- Create a
source code file called Vector3dTester.java (similar to
ComplexTester.java) . This code should
- Read
from a file VectorTestInput the data for two Vector3d's.
2. Print
to the screen the two Vector3d's, their magnitude, sum, dot product, cross
product and the angle between them.
- Compile
this code (javac
Vector3dTester.java)
- Run
this code (java
Vector3dTester) for a few values of input vector. Check by hand that
it is correct.
- Harder: Choose
appropriate vectors A, B, C, and make your program print output that
verifies the following vector product identies
·
|A.B| = |A| |B| cos q
·
|AxB| = |A| |B| sin q
·
A x B = - B x A
·
A x (B+C) = ( A x B ) + ( A x C)
·
A x ( B x C ) = (A.C) B –
(A.B) C
·
Verify that A.(BxC), as computed by your
program, gives the volume a cuboid
Note
that you will reuse
the Vector3d class later in the course, so make sure it works well!
In order to work with later examples, the fields of your Vector3 class should
be called x, y and z,
So that MyVector3.x, MyVector3.y and MyVector3.z (would be the components,
assuming MyVector3 is an instance of the Vector3 class).
- Improving
the file reader:
1. Read
this link
and use StreamTokenizer::ttype to parse the input file in a way that detects
errors
2. Use
StreamTokenizer::whitespaceChars(c,c) to flag '(' ',' and ')' as ignored to
allow the input file to contain "(x,y,z)" format.