CS
1401 Introduction to Computer Science
Fall 2014, Lab 9
Motivation: to practice classes and objects.
Background. While computers have been originally designed
for a serious task of processing data, nowadays a large amount of
computer resources is spent on playing computer games. One of the
many things that attracts people to computer games is their
ability to compute realistic images of 3D scenes.
Many graphics algorithms behind these games use 3D vectors. Let
us therefore practice simple operations with vectors.
Assignment for Lab 9. Define a class of vectors. Let us
start with an implementation in which each instance of this class
has three realvalued fields: the three coordinates x_{1},
x_{2}, and x_{3} of the vector. Define a
constructor, appropriate setmethods (modifiers) and getmethods,
operations on vectors (see below), and a method that prints the
vector as a triple (x_{1}, x_{2}, x_{3}).
Use the main method to test your class. For example, show how you
can use your methods to normalize a given vector, i.e., to divide
it by its length.
The following operations should be defined for vectors:

multiplication of a vector by a real number a: a * (x_{1},
x_{2}, x_{3}) is defined as (a * x_{1}, a
* x_{2}, a * x_{3});
 division of a vector by a real number a: (x_{1},
x_{2}, x_{3}) / a is defined as (x_{1} /
a, x_{2} / a, x_{3} / a);
 the length of a
vector; the length is defined as x= √ (x_{1})^{2} +
(x_{2})^{2} + (x_{3})^{2};
 a method for checking whether the vector is a zero vector,
i.e., whether all three of its components are equal to 0;
 the
sum of two vectors; it is defined componentwise: (x_{1},
x_{2}, x_{3}) + (y_{1}, y_{2},
y_{3}) = (x_{1} + y_{1}, x_{2} +
y_{2}, x_{3} + y_{3});
 the difference
between two vectors; it is also defined componentwise:
(x_{1}, x_{2}, x_{3}) −
(y_{1}, y_{2}, y_{3}) = (x_{1}
− y_{1}, x_{2} − y_{2},
x_{3} − y_{3});
 the dot product between
the two vectors: (x, y) = x_{1} * y_{1} +
x_{2} * y_{2} + x_{3} *
y_{3}.
For extra credit:
 write down an alternative
implementation, in which a vector is represented by an array; by
calling the corresponding methods in the main program, show that
the results do not change if you replace the original numberbased
implementation with the arraybased one;
 implement vector
product  and any other additional operations with vectors.
When it is due. The program is due at the beginning of the
second lab section on the week of November 10, i.e.:
 on
Wednesday November 12 for those who attend MondayWednesday labs,
and
 on Thursday November 13 for those who attend
TuesdayThursday labs.