## 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 3-D scenes.

Many graphics algorithms behind these games use 3-D 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 real-valued fields: the three coordinates x1, x2, and x3 of the vector. Define a constructor, appropriate set-methods (modifiers) and get-methods, operations on vectors (see below), and a method that prints the vector as a triple (x1, x2, x3). 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 * (x1, x2, x3) is defined as (a * x1, a * x2, a * x3);
• division of a vector by a real number a: (x1, x2, x3) / a is defined as (x1 / a, x2 / a, x3 / a);
• the length of a vector; the length is defined as |x|= √ (x1)2 + (x2)2 + (x3)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 component-wise: (x1, x2, x3) + (y1, y2, y3) = (x1 + y1, x2 + y2, x3 + y3);
• the difference between two vectors; it is also defined component-wise: (x1, x2, x3) − (y1, y2, y3) = (x1 − y1, x2 − y2, x3 − y3);
• the dot product between the two vectors: (x, y) = x1 * y1 + x2 * y2 + x3 * y3.

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 number-based implementation with the array-based 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 Monday-Wednesday labs, and
• on Thursday November 13 for those who attend Tuesday-Thursday labs.