CS 10: Introduction to Computer Programming

In-Class Examination 2: Solutions

(Although one solution is given for each question, it is not necessarily the only, or best, solution.)
Open-Book, 75 minutes, Spring 1997

General Instructions



Question  Value  Score 
1  10 
2  15 
3  10 
4  15 
5  15 
6  10 
7  10 
8  15
Total  100 


1. Write a boolean method, called xyz, that returns true if and only if its int argument is divisible by 3, 5, 7, and 11.

    Solution:
 
        boolean xyz(int n) {
            return n % 3 == 0 && n % 5 == 0 && n % 7 == 0 && n % 11 == 0;
       }

2. Write a method, called magic, that has 3 boolean arguments and behaves according to the truth table below.

arg1 arg2 arg3 returns
true true true false
true true false true
true false true true
true false false true
false true true false
false true false true
false false true false
false false false false
3. What is the output of the applet below (approximate the String positions, based on the coordinates given)? 

4. Write a Java expression that evaluates to a "random" int value in the set {17, 18, 19, ..., 100}.  If it cannot be done, say "It cannot be done." 5. Convert the following if statement to equivalent Java statements without using boolean operators.  If it cannot be done, say "It cannot be done."


6. Write a method or pair of methods called magic that returns true, if its argument is a boolean and false if its argument is a double.  If it cannot be done, say "It cannot be done." 


Your resulting applet would produce the output: 

7. Write a method whose argument is an int array, and that returns an int that is the sum of the elements up to the first element whose value is 0.  If the array has no element whose value is 0, then the method returns the sum of the elements of the entire array. 8. You have the following applet: 

import java.awt.*;
import java.applet.*;
public class Temp extends Applet {
        int potSize = 20;                   // size of color squares
        int paletteX = 0, paletteY = 30;    // origin of palette
        Color pots[][] = new Color[4][4], active = Color.black;

        public void init() {
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 4; j++)
                    pots[i][j] = new Color(i*64, 0, j*64);
        }

        public void paint(Graphics g) {
            // paint palette
            for (int i = 0; i < 4; i++)
            for (int j = 0; j < 4; j++) {
                g.setColor(pots[i][j]);
                g.fillRect(paletteX + j*potSize, paletteY + i*potSize, potSize, potSize);
            }
            // paint active color
            g.setColor(active);
            g.fillRect(0, 0, potSize, potSize);
        }

        public boolean mouseUp(Event event, int x, int y) {
            if (x >= paletteX && x <= paletteX + 4*potSize &&
                y >= paletteY && y <= paletteY + 4*potSize)
                active = select(x, y);
            repaint();
            return true;
        }

 public Color select(int x, int y) {

            //your code goes here
        }
    }



Write a method, public Color select(int x, int y), that takes the cursor position (i.e., 2 int arguments), and returns the Color of the square that has been clicked.

Solution: