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.

    Solution:

    public boolean in(int a[][][][], int x)
    {
        for (int i = 0; i < a.length; i++)
        for (int j = 0; j < a[i].length; j++)
        for (int k = 0; k < a[i][j].length; k++)
        for (int l = 0; l < a[i][j][k].length; l++)
            if (a[i][j][k][l] == x)
                return true;
        return false;
    }



2.

3.

    Solution:

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;
    }
}


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

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

    public double getImag() { return imag; }

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

        imag = real*c.imag + imag*c.real;
        real = temp;
        return this;
    }
    public double sizeSquared()
    {
        return real*real + imag*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.

    Solution:
public boolean implies(boolean x, boolean y) { return !x || y; }

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.

    Solution:
(int) (5*Math.random()) - 2

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

    Solution:

    public void stringProduct(String a[], String b[], String c[])
    {
        for (int i = 0; i < a.length; i++)
        for (int j = 0; j < b.length; j++)
        for (int k = 0; k < c.length; k++)
            System.out.println(a[i] + b[j] + c[k]);
    }


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;
    }
}



        Solution:
     
      import java.awt.*;

      public class Box extends Bug
      {
          private Color color;

          public Box(int fieldSize)
          {
              super(fieldSize);
              int i = (int) (2*Math.random());
              color = Color.red;
              if (i == 1)
                  color = Color.blue;
          }

          public void draw(Graphics g)
          {
              g.setColor(color);
              g.fillRect(x, y, e, e);
          }
      }

      import java.awt.*;

      public class Circle extends Bug
      {
          private Color color;

          public Circle(int fieldSize)
          {
              super(fieldSize);
              int i = (int) (2*Math.random());
              color = Color.red;
              if (i == 1)
                  color = Color.blue;
          }

          public void draw(Graphics g)
          {
              g.setColor(color);
              g.fillOval(x, y, e, e);
          }
      }


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.

    Solution:

    Complex dotproduct(Complex a[], Complex b[])
    {
        int k = (int) Math.min(a.length, b.length);
        Complex product, sum = new Complex(0,0);

        for (int i = 0; i < k; i++)
        {
            product = new Complex(a[i]);  // copy construct a[i]
            product.multiply(b[i]);       // product = a[i]*b[i]
            sum.add(product);             // sum += product
        }
        return sum;
    }