# Arrays...

• Goal of this Homework

• Relevant Material: Chapter 7 in book

• Preparation

Save the following files in your project directory (preferably, make a directory called "hw6" in your "cs170" directory, and save these files there (right click and select save target as)

You can also get the files using the command:

```    cp  /home/cheung/teaching/web/170/Projects/hw6/Handouts/*   ~/cs170/hw6
```

In the remainder of the homework description, I will assume that you have saved these file in your ~/cs170/hw6 directory.

• If you use BlueJ

• If you are using BlueJ for the lab, you must open hw6 as "Non BlueJ"

• Vectors

• A vector in Mathematics is a column (or row) of numbers

• For example: (2, 3) is a two-dimensional vector.

Graphically, it can be represented by an arrow:

 ``` | 4 + | | 3 + + (2, 3) | / | / 2 + / | / | / 1 + / | / |/ 0 +----+----+----+----+----+---- 0 1 2 3 4 5 The point (0,0) is called the "origin" ```

The length of this vector can be computed as (using Pythagoras' formula):

```      __________         _____
\  /               \  /
\/ 2*2 + 3*3    =  \/  13      = approximately 3.605551275
```

• Another example of a vector is: (2, 4, 6). This is a three-dimensional vector (because it has 3 coordinates)

This is very difficult to draw - conceptually, this is an arrow whose tail end point is on the origin and the tip ends at: 2 units to the east, 4 units to the north and 6 unit up (above the ground)

• Storing vectors in arrays

• The array can store a sequence of numbers

• For example, to store the vector (2, 4, 6), we can do the following:

 ``` double[] A; A = new double[3]; A[0] = 2; A[1] = 4; A[2] = 6; ```

• So the array is ideal for storing vectors !!!

• Homework Assignment

In this assignment, we will write a Vector class that you can use to represent three-dimensional vectors (i.e., a vector has 3 indices).

• Write in the program file "Vector.java", the following methods:

• A constructor Vector() that takes 3 double values and constructs a vector of length 3. The first value is the coordinate in the x-coordinate, the second value is the coordinate in the y-coordinate and the third value is the coordinate in the z-coordinate.

The representation must use a one-dimensional array of doubles - and the size of the array is 3. (Look inside the file Vector.java - I have already defined the array variable; you need to initialize it !).

• A method norm() that returns the norm of a vector.

The norm of a vector (x, y, z) is:

```	Math.sqrt( x*x + y*y + z*z )
```

For example: if the vector is (2, 4, 6), then its norm is:

```        Math.sqrt( 2*2 + 4*4 + 6*6 ) = Math.sqrt(56) = 7.483314774
```

• Turnin

• Turn in the homework with the following command:
```   /home/cs170001/turnin   ~/cs170/hw6/Vector.java  hw6

```