CS111 PreLab 13

This prelab covers simple object diagrams with arrays and writing simple methods that work with arrays. You should try to do the object diagram before lab. The second part of the prelab (writing methods) will not be covered in lab and the answers are provided below. You can study the questions and solutions on your own time. The lab assignment will cover harder object diagrams with arrays (similar to that on the problem set) and also go over a basic implementation of the FigureStack class using arrays (i.e. the FigureStackArray class mentioned in the problem set). You can read the problem set to see what we'll be doing in lab. Your homework this week involves implementing a smarter implementation of the FigureStack class also using arrays. Understanding this week's lab assignment will help a lot for doing the homework. Solutions for the FigureStackArray will be handed out at the end of lab.

SEQs will be conducted in the last 20 minutes of lab. SEQs are Wellesley's official way of evaluating courses and instructors. We will be doing SEQs for the entire CS111 course in lab. That means you will be filling out five forms in lab:

The evaluation forms are very short. You answer a few questions about each course (lecture/lab) and only two questions about each instructor. I will not be in the room when you fill out the forms. I will also need a student volunteer to stay till the end of class and collect the forms and bring them to Green Hall. Please consider volunteering to do this.

Simple Object Diagrams with Arrays

Consider the following code which we'll say is in a class called IntArrayOps:
public static int [] copy(int [] a) {
  // Returns a copy of the given array. 
  int [] result = new int [a.length];
    for (int i = 0; i < a.length; i++) {
      result[i] = a[i];
  }
  return result;
}

public static int [] postpend(int [] a, int n) {
  // Returns a new array which has the integers of the original array
  // followed by the new integer.
  int [] result = new int [a.length + 1];
  for (int i = 0; i < a.length; i++) {
    result[i] = a[i];
  }
  result[a.length] = n;
  return result;
}

public static int[] increment (int [] a, int n) {
  // Returns a new array which has n added to each integer of a.
  int [] result = new int [a.length];
  for (int i=0; i < a.length; i++) {
    result[i] = a[i] + n;
  }
  return result;
}

public static void incrementX (int [] a, int n) {
  // Modifies the array by adding n to each integer in a.
  for (int i=0; i < a.length; i++) {
    a[i] = a[i]+n;
  }
}
Draw the Object Diagram for the following set of actions. Follow the guidelines in problem set 10. Assume that the methods above are available to us.
// An array with no slots.
int[] intArray1 = new int[0];

// One way to initialize an array.
int[] intArray2 = new int[4];
intArray2[0] = 48;
intArray2[1] = 84;

// Another way to initialize an array.
int[] intArray3 = {-4, 8, 93};

intArray1 = copy(intArray3);
intArray1[2] = 33;
int[] intArray4 = postpend(intArray2,23);
intArray4[3] = intArray3[2];
int[] intArray5 = increment(intArray3,-5);
incrementX(intArray1,2);
intArray2 = intArray3;
intArray3 = new int[1];


Methods that manipulate arrays

Flesh out the following methods involving integer arrays:
public static int sum (int [] a)
// Returns the sum of the integers in the array.
// Ex. sum({1,2,3,4}) ==> 10

public static int minimum (int [] a)
// Returns the minimum integer in the array.
// If there are no elements in the array, returns Integer.MAX_VALUE.
// Ex. minimum({-10,30,0,-4}) ==> -10

public static double average (int [] a)
// Returns the average of the integers in the array.
// Ex. average({28,82,48,28}) ==> 46.5

public static void multiplyBy (int [] a, int n)
// Modifies the current array so that each integer has been multiplied by n.
// Ex. multiplyBy({28,-4,3,10},-3) ==> {-84,12,-9,-30} (same array)

public static int [] prepend (int [] a, int n)
// Returns a new array starting with the integer n followed
// by the integers in the array a.
// Ex. prepend({38,28,94},-38) ==> {-38,38,28,94}

public static int [] resize (int [] a, int n)
// Returns a new array of the size given with the elements of the
// old array if they are within the new size given.
// Assumes ne is greater than or equal to zero
// Ex. resize({10,40},5) ==> {10,40,?,?,?}
// Ex. resize({30,28,40,28,48},2) ==> {30,28}

public static int [] repeat (int [] a, int n)
// Returns a new array which has the sequence of integers in a
// repeated n times. Assumes n is greater than or equal to zero.
// Ex. repeat({10,20,30},3) ==> {10,20,30,10,20,30,10,20,30}

public static int [] difference (int [] a1, int [] a2)
// Returns a new array which has the difference between corresponding
// integers in a1 and a2. Ie. result[0] = a1[0]-a2[0].
// Assumes that a1 and a2 are the same length.
// Ex. difference({30,28,93},{-49,88,38}) ==> {-19,-60,55}

Answers to methods above

Answers are given here so that you can check your work.
public static int sum (int [] a) {
  int sum = 0; // declare local variable and initialize it
  for (int i=0; i < a.length; i++) {
    sum+=a[i]; // add the integer in that position of the array to the sum
  }
  return sum;
}

public static int minimum (int [] a) {
  int minimum = Integer.MAX_VALUE;
  for (int i=0; i < a.length; i++) {
    minimum = Math.min(minimum, a[i]);
  }
  return minimum;
}

public static double average (int [] a)
  return ((double)sum(a))/a.length;
}

public static void multiplyBy (int [] a, int n)
  for (int i=0; i < a.length; i++) {
    a[i] = a[i]*n;
  }
}

public static int [] prepend (int [] a, int n)
  int[] result = new int[a.length+1];
  result[0] = n; // new integer goes in first slot
  // copy rest of integers into rest of slots
  for (int i=0; i < a.length; i++) {
    result[i+1] = a[i];
  }
  return result;
}

public static int [] resize (int [] a, int n)
  int[] result = new int[n]; // new int array of size n
  int maxItems = Math.min(a.length,n); // which is smaller, the new or old array?
  for (int i=0; i < maxItems; i++) {
    result[i] = a[i];
  }
  return result;
}

public static int [] repeat (int [] a, int n)
  int [] result = new int[a.length*n];
  for (int times=0; times < n; times++) {
    for (int i=0; i < a.length; i++) {
      result[(a.length*times)+i] = a[i];
    }
  }
  return result;
}

public static int [] difference (int [] a1, int [] a2)
  int [] result = new int[a1.length];
  for (int i=0; i < a.length; i++) {
    result[i] = a1[i] - a2[i];
  }
  return result;
}