Study Guide #2 Answers > ====================== > > Sample end-of-chapter exercises 5.2: static methods can't refer to instance data because static methods are intended to be run without any reference to *any* objects of that class. Alternatively, static functions don't have a particular object to refer to, and if there were several objects created then the static function couldn't "decide" which instance data was wanted. > For chapter 6, look at 6.2 through 6.7 on page 374. > ====================== > Sample questions > 1. Write a loop to find the product of all the numbers in an array A > of type double. For example, if A contains [1.2, 2.0, 2.5], then your > loop should end with 6.0 in a suitable variable. double prod = 1.0; for(int i = 0; i 2. Write a function to find the product in part 1. double arrayprod(double [] v) { // I'm using 'v' as param name, not 'A' double prod = 1.0; for(int i = 0; i 3. Write a class Counter with two methods: value(), to get the current > integer value stored internally, and increment(), to increment that value. > Give your class a constructor, that explicitly initializes the counter > so that value() initially returns the value 0. class Counter { private int _x; public Counter () {_x = 0;} // constructor public int value() {return _x;} public void increment() {_x++;} } > 4. Consider the following array a: 0 1 2 3 4 5 6 7 8 > +---+---+---+---+---+---+---+---+---+ > | 3 | 4 | 2 | 5 | 7 | 1 | 0 | 8 | 6 | > +---+---+---+---+---+---+---+---+---+ > Give: > a[1] > a[ a[1] ] > a[ a[a[1]+1] ] a[1] == 4 a[a[1]] == a[4] = 7 a[a[a[1]+1]] = a[a[4+1]] = a[a[5]] = a[1] = 4 > 5. Consider the following class: > class Foo { > private int _x; > public Foo(int x) {_x = x;} > public int get() {return _x;} > public void set(int x) {_x = x}; > public void inc() {_x++;} > } (This is a very typical class, with data, ctor, accessor, mutator, and an "update" mutator) > Give the output: > > int x=7, y=2; > Foo a = new Foo(3), b = new Foo(5), c; > Foo[] v = new Foo[5]; > > x = y; > y++; > System.out.println("x=" + x + " y=" + y); x=2 y=3 incrementing y does *not* affect x; x and y are "primitive" data. > a = b; > b.set(b.get()+5); > a.inc(); > System.out.println("a.get()=" + a.get() + " b.get()=" + b.get()); a.get()=10 b.get()=10 a and b are two references to the *same object*. The "b.set(b.get()+5)" updates the object referred to by b to contain 10 instead of 5. a is an alias for this same object. > c = new Foo(0); > int i; > for (i = 0; i for (i = 0; i System.out.println("c.get()=", c.get()); > for (i = 0; i System.out.println("v[" + i + "].get()=" + v[i].get()); The array v has five components, and all five are references to the same object. That single object gets inc()'ed five times, so its final value is 5. The output is: v[0].get()=5 v[1].get()=5 v[2].get()=5 v[3].get()=5 v[4].get()=5 It helps to draw a diagram for problems of this type, indicating the references and the objects themselves as separate things. > 6. Write a function that takes two arrays of double, and multiplies each entry > of the first array by the corresponding component of the second array. > If the arrays are of unequal length, the process stops whenever the end > of the shorter array is reached. void multer(double[] a, double[] b){ int i, len; if (a.length 7. Suppose you have the array a > +---+---+---+---+---+---+---+---+---+ > | 3 | 4 | 2 | 5 | 7 | 1 | 0 | 8 | 6 | > +---+---+---+---+---+---+---+---+---+ > and you execute > j=1; > hi=5; > for (i=0; i if (a[i] } > (a) What is j at the end? what would happen if we swapped a[i] and a[j]? The loop searches from a[0] to a[hi-1], inclusive, for the smallest component; j ends up containing the index of this component. In the example above, we terminate with j==2, as a[2] = 2 is the smallest component. Normally we would swap a[0] and a[j], not a[i] and a[j]. If we do swap a[i] and a[j], we first note that i=5 at the end of the for loop. We then end up with +---+---+---+---+---+---+---+---+---+ | 3 | 4 | 1 | 5 | 7 | 2 | 0 | 8 | 6 | +---+---+---+---+---+---+---+---+---+ > (b) What would your answer be if we set hi=6 initially instead of 5? In this case we end up with j=5, as a[5]=1 is the smallest between a[0] and a[5]. > 8. The following code initializes an array of size MAX with random data > and then counts how many entries are > CUTOFF. > Rewrite it using an ArrayList. > > Random g = new Random(); > int [] a = new int[MAX] > for (int i=0; i int count = 0; > for (int i=0; iCUTOFF) count++; This was dashed off *just* a little too quickly; ArrayLists can't contain primitive data like int. Here's a version using Integer, the int wrapper class; you're *not* expected to be able to figure that out on the exam. Actually, even if I had a "simple" ArrayList problem on the exam, I would provide you with a brief summary of ArrayList methods. Random g = new Random(); ArrayList A = new ArrayList(); for (int i=0; i CUTOFF) count++; }