Lab 9: Thursday August 25th

Mathematics Fun : vectors and matrices

Use the following class to represent a vector:

class Vector  {

   int[] v;

   Vector( int size ) {
      this.v = new int[size];
   }

}

Make an instance of a 5 element vector.

Currently, our Vector contains all 0s. In fact, all of our vectors will always contain 0. This isn't mathematically very interesting.

Add a method to Vector that initializes all of the elements of v to a specified number.

Still, our vectors aren't very mathematically interesting. So let's provide the ability to have arbitrary values throughout our vector. Consider the following partial classes:
abstract class InitList {
    // To initialize givenV with the elements of this InitList
    abstract Vector initializeV( Vector  givenV );
}

class Empty extends InitList {
}

class Larger extends InitList {
   int first;
   InitList rest;

   Larger( int first, InitList rest ) {
      this.first = first;
      this.rest = rest;
   }
}


Finish implementing these classes. You may assume that the length of InitList is always the same as the length of the int array in givenV.
You will need to add a method to Vector. One possible header is void initPosition( int pos, int val)

Now our vector is mathematically interesting. Add a method to Vector to scale the vector by a given value (i.e. multiply all of the elements of the vector by a given number).

Write a method to add two vectors, producing a new vector (you may assume that both vectors are always the same size).

Now develop a data design and class representation for a matrix. A matrix might either be an int[][] or a Vector[].

Develop methods in the Matrix class to add to matrices and to scale a matrix by a constant.

If there is time, develop a means to initialize the matrix, as we did for the Vector.

Consider how you would develop a method to multiply a matrix by a vector.

Note: Java has provided a means to initialize an array directly: the syntax 'int[] myArray = {1,2,3,4,5};' will create an array of these numbers and associate it to the name myArray. Do not use this syntax for this lab, but you may use it in the future.