CS 178 Programming with Multimedia Objects

Fall 2004

Recitation Exercise:

Prepared by: Aditya Mathur.

To be completed , as much as possible, in class on Oct 15, 2004.

Learning objectives:
  1. How to perform simple operations on arrays.
  2. How to use loops.
  3. How to write and use your own methods.
Complete the following set of instructions to develop a few simple methods. Note that we are NOT writing an applet. Instead, we are writing methods that could be used later inside a Java applet.

Global Array and other declarations:

  1. Declare maxElements as a named constant with value equal to 120.
  2. Declare an array atomicWeight to contain atomic weights of various elements. Array size: maxElements.
  3. Declare numElements as variable. It indicates the total number of elements in atomicWeight. Initialize it apporpriately.

Write the following methods.

1. findHeaviest method:

  • Write a method that returns the index of the heaviest element in the atomicWeight array.

2. exchange method:

  • Write a method that takes indices i and j as inputs and exchanges the ith and the jth elements of atomicWeight.

3. findLarger method:

  • Write a method that takes a value of type double as input and returns the count of the number of elements in atomicWeight that are strictly larger than the input value.

4. findAverage method:

  • Write a method that returns the average of all elements in atomicWeight.

5. sortWeights method

  • Write a method to sort the atomicWeight. array in the order of increasing weights.

Last update: October 14, 2004