Computing Concepts with Java Essentials
Laboratory Notebook
Chapter 11 - Arrays and Vectors


Your name:
Your email address:
Your student ID number:

Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.


Lab Objectives

To gain experience with

P1. Array Subscripts

In the text, we have frequently used the Random.nextInt function to generate random integers. For example, generator.nextInt(6) generates random numbers between 0 and 5, suitable for simulating the throw of a die. In this lab assignment, you will use an array to test whether the random generator is fair, that is, whether each possible value is generated approximately equally often.

Your program should ask the user

Make an array with nvalues elements, one for each possible value. Set each element to 0. Then keep calling the random number generator. If it returns a value v, then increment the counter belonging to v.

After all numbers have been generated, print the counters. Here is a typical program run:

How many numbers do you want to generate?
1000
What is the number of values?
10
0    78
1   101
2   118
3    97
4   103
5   102
6   112
7    91
8    94
9  104

P2. Using Arrays to Collect Objects

Arrays can hold objects, not just numbers. In the following lab program, you will generate random circle objects and store them in an array. If a circle does not intersect any of the previously stored circles, you will add it to the array. Finally, display all circles that you collected in the array. The result should look something like this: (Note that none of the circles intersect.)

Randomly generated non-overlapping circles

Use the code of the circleIntersect function that is given below to test whether two circles intersect. To randomly generate a circle, simply pick random x- and y- positions between 0 and getWidth()/getHeight() for the center and a random radius between 0 and 40. You need to compare each newly generated circle with all other circles before you can add it. If the new circle passes the test, add it to the end of the array. Note that the array will have fewer elements than the number of generated circles since you are rejecting sme of the circles. You will need to keep track of the actual number of circles in the array.

   /**
    * Test if two circles intersect
    * (distance between centers is less than sum of radii)
    * @param c1 the first circle (must be a circle, i.e width == height)
    * @param c2 the second circle (must be a circle, i.e. width == height)
    * @return true if they intersect
    */

   public static boolean circleIntersect(Ellipse2D.Double c1, Ellipse2D.Double c2)
   {  double radius1 = c1.getWidth() / 2;
      double radius2 = c2.getWidth() / 2;
      double dx = c1.getX() - c2.getX();
      double dy = c1.getY() - c2.getY();
      double distance = Math.sqrt(dx * dx + dy * dy);
      return radius1 + radius2 < distance;
   }

Using the debugger or a print statement, find out what percentage of circles was rejected.

Since you don't know the exact number of circles that need to be stored, and the upper bound--the total number of random circles--is higher than the actual requirements, it would make sense to use a Vector to store the circles instead. Rewrite the program to use a Vector to hold the circles instead. Whenever a circle passes the test, call add to add it to the end of the vector.


P3. Return Parameters and Return Values

Here is a function that receives an array of integers and creates a new array, with the entries reversed. For example, if the input array contains 1 2 3 4, the output array contains 4 3 2 1. Note that this function has an array parameter and an array return value.

/**
 * compute the reverse of an array
 * @param intValues an array of integer values
 * @return a copy of the array, with the elements reversed
*/
public static int[] reverse(int[] intValues)
{  int[] result = new int[intValues.length];

   int i;
   for(i = 0 ; i < intValues.length; i++)
       result[intValues.length - i - 1] = intValues[i];

   return result;
}

Note that this function does not change the intValues array. Rewrite this function so that it reverses the intValues array in place, without computing a new array. Supply a program that tests your function.


P4. Simple Array Algorithms

Write a method that finds the average grade from a sequence of homework scores where the lowest two scores are discarded.

double averageWithoutLowest2(int[] scores)

Test your method with a test harness.

How does your program handle duplicate grades ? For example, how would it process 95, 95, 95, 80, 78, 68, 68? What do you think it should do?


P5. Avoiding Parallel Arrays

Write a program that reads in the names and scores of students and displays the names of the students with the highest and lowest score.

A simple method of carrying out this task would be to have two parallel arrays

String[] names;
int[] scores;

However, you should avoid parallel arrays in your solution. (Hint: Come up with a class, and make an array or vector of that class.)


P6. Arrays as Object Data

You probably solved the preceding exercise by implementing a class Student with two instance variables, String name and int score. Now suppose that we want to store multiple scores for each student. That is, we want to have a class like this:

class Student
{   . . .
   private String name;
   private int[] scores;
   private int scoresSize;
   private static final int SCORES_LENGTH = 20;
}

Supply the following methods:

Now put your class to work in a program that simulates a class of 30 students. Each student takes 12 quizzes and earns a completely random score between 0 and 100 on each quiz. Find out the highest and lowest averages (with the two lowest scores discarded) among the 30 students and print out those two averages.

What were the highest and lowest average in your program run?


P7. Using 2-dimensional Arrays

Arrays store a linear arrangement of values, accessed by a single index. Two-dimensional arrays store a tabular arrangement of values, accessed by two indexes, for example matrix[i][j], where i is the row index, and j is the column index..

Crosswords are a type of puzzle which have letters in common. For example,

          d
       across
          w
          n

share the letter 'o'.

Write a program that will accept a list of words into a vector of strings, then make a 20 x 20 array of char that contains a crossword puzzle with these words.

For example, your program when given the list addle, apple, clowning, incline, plan, burr it displays it as:

   addle
   p
   p
  clowning
   e   n
       c
       l
       i
    plan
       e

That is, use the following method: Place the first word horizontally in the center. For the following words, do this: For each letter in the word, check if that letter is already in the puzzle. If so, try to place the word horizontally and vertically. A word can be placed if all of its letters fall on spaces or on matching letters. If you can't place a word, skip it.


Don't forget to send your answers when you're finished.