Exam 2
Computer Science 125
Dr. St. John
Boise State University
Monday, 9 November 1998

1.
TRUE or FALSE:
(a)
A constructor is used to initialize objects when they are created.

(b)
A class may have at most one method with the same name.

(c)
When creating an array, you do not need to specify the number of elements you want the array to hold.

(d)
Array subscripts must be integers.

(e)
Arrays cannot contain strings as subscripts.

(f)
If a program has passed all tests in a test suite, it has no more bugs.

(g)
Functions declared as static operate on objects and are called instance methods.

(h)
Every Java program can be rewritten to avoid import statements.

(i)
You may no more than one public class per file.

(j)
Stacks have ``first in, first out'' or FIFO access.

2.
What are the values of s and n after the following loops:
(a)
int s = 1;
int n = 1;
while (s < 10) 
{ 
    s = s + n; 
    n++; 
}
(b)
int s = 1;
int n = 1;
do { 
    s = s + n; 
    n++; 
} while ( s < 2*n );

3.
Consider the following class.
(a)
Mark the constructors with a ``C'', the accessor functions with a ``A'', and the mutator functions with a ``M'':
class A
{
    public A() { n = 0; }

    public A(int a) { n = a; }

    public void f() { n++; }

    public void g() { f(); n = 2 * n; f(); }

    public void h() { return n; }

    public void k() { System.out.println(n); }

    private int n;
}

(b)
Add a method c() to the class above that divides n by 2 if n is even and replaces it with 3 * n + 1 if n is odd.

4.
Write a function:
public boolean isPrime(int n)
that returns true if n is a prime number and false otherwise.

5.
For each of the following sets of values, write code that fills an array v with the values:
(a)
10 20 30 40 50 60 70 80 90 100
(b)
3 5 3 5 3 5 3 5 3 5

6.
What is the output from the following programs:
(a)
import ccj.*;
public class mystery1 extends GraphicsApplet
{  public void run()
    {   Point[] pp = new Point[6];
        for ( int i = 0 ; i < 6 ; i++ )
        {   double t = i * (2*Math.PI/6);
            pp[i] = new Point(5*Math.cos(t),5*Math.sin(t));
        }       
        for ( int i = 0 ; i < n ; i++ )
            new Line(pp[i], pp[(i+1)%6]).draw();
    }
}

(b)
import ccj.*;
public class mystery2 extends GraphicsApplet
{   public void run()
    {   Point[] pp = new Point[8];
        for ( int i = 0 ; i < 8 ; i++ )
        {   double t = i * (2*Math.PI/8);
            pp[i] = new Point(5*Math.cos(t),5*Math.sin(t));
        }       
        new Circle(new Point(0,0),5).draw();
        for ( int i = 0 ; i < 8 ; i++ )
            new Line(pp[i], new Point(0,0)).draw();
    }
}

7.
(a)
What is the difference between a virus and a Trojan horse? Give examples.
(b)
What were Social Security Numbers (SSN) originally designed for? Why are SSN used as ``passwords'' for consumer databases such as credit bureaus? Why is this a bad choice?
(c)
Data generated by a computer program is used to design a bridge. The bridge collapses during the last week of construction killing two people. Who is responsible? Why?

8.
Modify the following program to allow the user to change the colors that are drawn. Add at least 2 choices of colors and buttons for each choice. For example, if you choose the colors red and black, you should have buttons that when pressed, change the color that is drawn to red or black, respectively. Your applet should also have a clear button like OneColorDraw.
import java.applet.*;   
import java.awt.*;     

public class OneColorDraw extends Applet 
{
    private int last_x = 0;     
    private int last_y = 0;    
    private Button clear_button;
    private Color current_color = Color.black;  

    
    public void init() 
    {
        clear_button = new Button("Clear");
        clear_button.setForeground(Color.black);
        clear_button.setBackground(Color.lightGray);
        this.add(clear_button);           






    }

    public boolean mouseDrag(Event event, int x, int y)
    {
        Graphics g = this.getGraphics(); 
        g.setColor(current_color);      
        g.drawLine(last_x, last_y, x, y);
        last_x = x;     
        last_y = y;    
        return true;  
    }


    // (continued on the next page -->)
    
    public boolean action(Event event, Object arg) 
    {

        if (event.target == clear_button) {
            Graphics g = this.getGraphics();
            Rectangle r = this.bounds();
            g.setColor(this.getBackground());
            g.fillRect(r.x, r.y, r.width, r.height);
            return true;
        }
        else return super.action(event, arg);

    }


}

9.
Fill in the missing function definitions in the PuzzlePiece class:
import ccj.*;
public class PuzzlePiece 
{   
    private Point ul;
    int label;
    /**
     * Initialize the label to 0 and the corner to (0,0).
     */
    public PuzzlePiece()
    {




    }
    /**
     * Initialize the label to l and the corner to (0,0).
     * @param l the label for the piece
     */
    public PuzzlePiece(int l)
    {




    }
    /**
     * Initialize the label to l and the corner to (x,y).
     * @param l the label for the piece
     * @param x the x-coordinate of the corner.
     * @param y the y-coordinate of the corner.
     */
    public PuzzlePiece(int l, double x, double y)
    {





    }

    // (continued on the next page -->)
    /**
     * Move the piece by dx units in the x direction and dy units in the y 
     * direction.
     * @param dx change in x-coordinate
     * @param dy change in y-coordinate
     */
    public void move(int dx, int dy)
    {










    }

    /**
     * Draw the puzzle piece.  The label is drawn, surrounded by
     * a square whose upper left corner is the point stored in
     * PuzzlePiece.
     */
    public void draw()
    {
        Point ll = new Point(ul.getX(), ul.getY()+2);
        Point lr = new Point(ul.getX()+2, ul.getY()+2);
        Point ur = new Point(ul.getX()+2, ul.getY());
        new Line( ul, ll).draw();
        new Line( ul, ur).draw();
        new Line( ll, lr).draw();
        new Line( ur, lr).draw();
        Point mp = new Point(ul.getX()+1, ul.getY()+1);
        Message m = new Message(mp, "" + label);
        m.draw();
    }
}

10.
Write a complete program that counts the number of lines in a file. For example,
$ java Count sample.txt
counts the number of lines in the file sample.txt.