CS 10: Computer Programming

Final Examination

Closed-Book, 180 minutes, Spring 1999


General Instructions



1. Write a method that receives 2 arguments: and returns true if there is an element in the array whose value equals the 2nd argument, and returns false otherwise.

2.

3. 4. Remove all unnecessary uses of this from the Complex class below.

public class Complex
{
    private double real,        // the real part
                   imag;        // the imaginary

    public Complex(double r, double i)
    {
        this.real = r;
        this.imag = i;
    }
    public Complex(Complex c)
    {
        this.real = c.real;
        this.imag = c.imag;
    }
    public double getReal() { return this.real; }

    public double getImag() { return this.imag; }

    public Complex add(Complex c)
    {
        this.real += c.real;
        this.imag += c.imag;
        return this;
    }
    public Complex multiply(Complex c)
    {
        double temp = this.real*c.real - this.imag*c.imag;

        this.imag = this.real*c.imag + this.imag*c.real;
        this.real = temp;
        return this;
    }
    public double sizeSquared()
    {
        return this.real*this.real + this.imag*this.imag;
    }
}


5. Write a method, implies, that takes 2 boolean arguments, and returns true if either the first argument is false or the second argument is true, and returns false otherwise.

6. Write a Java expression whose value is a member of the following set, randomly selected each time it is executed: {-2, -1, 0, 1, 2}. That is, the first time the expression is evaluated, it might evaluate to 0; the second time, it might evaluate to -2, etc.

7. Write a method:

void stringProduct(String a[], String b[], String c[])
that uses System.out.println to produce n output lines, where
n = the number of elements in a X the number of elements in b X the number of elements in c.
For example, if a = { "0", "1" }, b = { "a", "b" }, and c = { "Lysander", "Spooner", "Jr" }, then the method would output the following 12 strings:
0aLysander
0aSpooner
0aJr
0bLysander
0bSpooner
0bJr
1aLysander
1aSpooner
1aJr
1bLysander
1bSpooner
1bJr
8. 9. Given the class definition below for Bug, define 2 subclasses, Box and Circle:
  1. Box: its draw method draws a filled in box of its color, and whose upper lefthand corner is (x, y),  and whose edge length is e. Boxes are constructed to be a fixed color, randomly chosen between red and blue.
  2. Circle: its draw method draws a filled in circle of its color, inscribed in a square whose upper lefthand corner is (x, y), and whose edge length is e. Circles are constructed to be a fixed color, randomly chosen between green and black.

import java.awt.*;

public class Bug {
    final static int MINSIZE = 20,
                     MAXSIZE = 40,
                     MOVEMAX = 10;
    // instance variables
    protected int x, y,               // initial upper left corner
                  e,                  // edge length
                  dx, dy;             // motion vector

    public Bug(int fieldSize) {
        x  = (int) (Math.random()*(fieldSize - MAXSIZE));
        y  = (int) (Math.random()*(fieldSize - MAXSIZE));
        e  = (int) (Math.random()*(MAXSIZE - MINSIZE + 1)) + MINSIZE;
        dx = (int) (Math.random()*(2*MOVEMAX + 1)) - MOVEMAX - 1;
        dy = (int) (Math.random()*(2*MOVEMAX + 1)) - MOVEMAX - 1;
    }
    public void setDx(int D) { dx = D; }

    public void setDy(int D) { dy = D; }

    public void draw(Graphics g) { }

    public void move() {
        x += dx;
        y += dy;
    }
}


10. Define the dot product of 2 vectors, a = (a0, a1, a2, a3, ..., an) and b = (b0, b1, b2, b3, ..., bm) as a0*b0 +  a1*b1 + a2*b2 + ... + aK*bK, where K is equal to the smaller of n and m.  Using the Complex class defined in question 4, define a method, dotproduct, that takes 2 arrays of Complex as an arguments, and returns their dot product without altering either argument array.