C212 Fall 1999

Exam 1: some sample solutions and grading comments

(incomplete draft)

Question 1

Sample solution (there are several approaches, some as pretty, some quite ugly by comparison):

    public static int minimum(int i, int j, int k)
    {   if (i <= j && i <= k) return i;
        else if (j <= i && j <= k) return j;
        else return k; // k < i and k < j
    }

Some of the more common error follow.

  1. Most forgot to declare the procedure static: 1 point extra credit for those who did and no penalty this time for not doing so.
  2. Some used < instead of <= with logic similar to that above. This results in k being returned when i == j < k, which is an error.
  3. Many wrote <= as a line under the < sign. Nothing off this time, but don't do it again. Also do not use =<, which is not an operator in Java or related languages.
  4. A method with non-void return type must always return a value. An else clause at the end that does nothing or just prints an error message is a type error. Also, an else clause that does nothing is always a waste of the last test. Either eliminate the last test (as in the solution above, in which case it is a good idea to put the condition at that point in a comment), or keep the test and throw an exception in the else clause.

Question 3

import java.applet.Applet;  // ; instead of , 
import java.awt.Graphics;   // ; instead of , 
import java.awt.Color; 
public class DistributedDrafter extends Applet 
{   
    public void init() 
    {   Graphics g = this.getGraphics(); 
        MousePoint mousePoint = new MousePoint(); 
        g.setColor(Color.blue);  // instead of BLUE 
        this.setBackground(Color.yellow); 
        this.addMouseMotionListener(new MouseMotionHandler(mousePoint)); 
        this.addKeyListener(new KeyHandler(g, mousePoint));  // add ; and remove the extra ) 
    } 
} 

Comments:

Overall, the class did well on this question. Almost no one lost points for poor indentation/style. Several wrote

this.addMouseMotionListener = new MouseMotionHandler(mousePoint);

This is of course incorrect because addMouseMotionListener() is  a method (inherited from Applet), not a variable that you can assign a value to.

Question 4

Errors in the code:

line 6: this keyword not defined in a static method.
line 6: cannot call an instance method m() from a static method.
line 6: cannot access the instance variable x in a static method.
line 6: wrong argument type in the method call; the second parameter should be an int.
line 6: missing ; at the end of the statement.

Comments:

This was a tricky question an most had a lot to trouble with it.  In fact no one got full  points on this question.

Question 5

"On!"
"On!"
"Light turned off: 1"
"Light turned on"
"Light turned off: 2"
"Light turned off: 1"

Grading tried to be kind:

Question 6, Dice Class [30 points]:

// Comments are always good, but since it was an exam and people were short
// on time, you were forgiven this time if you didn't include comments.  So
// you could get full marks for something like this:

public class Dice
{
    private int numberUp = 2;
    private boolean isCheating = false;

    public void rollDice()
    {   this.numberUp = (int)(Math.random() * 11.0) + 2; 
    }

    public int getValue()
    { 
        if (isCheating) return 1;
        else return this.numberUp;
    }

    public void cheat (boolean honest)
    {   isCheating = !honest ; 
    }
}

Many people wrote the cheat() method in a slightly long-winded way (no marks off for writing it like this):

public void cheat (boolean honest)
{   if (honest) isCheating = false; 
    else isCheating = true;
}

Common misakes:

The Most Common Place for Problems:

Remember: casting to an int truncates a double - that is, you lose everything after the decimal place. So 5.99 converts to 5 when cast as an int. So:take a random double x, where 0 <= x < 1. Multiply it by 11, we get 0 <= 11*x < 11. Cast it to an int, and you get one of {0, 1, 2, 3, 4, 5. 6. 7. 8. 9. 10} (Why not 11? Because the largest number we could have had was something like 10.9999999999999999, which comes out as 10 when we truncate everything after the decimal place). So we need to take the random double, multiply by 11, cast to an int, and then add 2. (We could add the 2 before casting to an int if we want).

Other issues

Some general points:

  1. Write in a style in which the distinction between upper and lower case is clear: Java is case sensitivity. This time grading was lenient about this.
  2. Unless it substantially simplifies the problem, if you make essentially the same mistake multiple times in the same question it will only be marked and points deducted the first time.
  3. Grading for indentation and other style errors was lenient this time, but see the style notes to be posted soon on the web.
  4. else { if is always bad style. If the else clause contains a single if or if/else statement, always use else if. Otherwise, start a new line either before the brace or before the if, depending on your chosen indentation style.
  5. If you provide more than one solution to a problem, only what appears to be the first or main one will be graded (unless it is crossed out).
  6. Do exactly what the problem says. For example, if your are to define a method named doit, define a method named doit, not one named something else you might prefer, like illDoIt.
  7. Avoid stray marks that might be mistaken for a misplaced semicolon or other punctuation.