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.
- Most forgot to declare the procedure static: 1 point
extra credit for those who did and no penalty this
time for not doing so.
- Some used < instead of <= with logic similar to
that above. This results in k being returned when i == j
< k, which is an error.
- 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.
- 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.
- Note that this question was about compilation
errors, not about writing sensible or meaningful code.
That is, you were only supposed to find only things
that are illegal in Java, not which statements get
executed and what would be the result.
- Many didn't seem to know that false is a Java
keyword which represents the false boolean value, and
hence is perfectly legal in an if conditional.
- Some people didn't like the {} after else, some
didn't like the fact that x and y are
not used in the body of the method m(), and some
didn't like the class name C. Again, these are
all absolutely legal.
- Several students pointed out the lack of a main() method
as an error. It is fine to have class which does not have
a main() method. You cannot execute a class as
an application (e.g., with % java C) if it
doesn't have a main() method, but you can
compile it fine (and have it used by other classes).
Question 5
"On!"
"On!"
"Light turned off: 1"
"Light turned on"
"Light turned off: 2"
"Light turned off: 1"
Grading tried to be kind:
- 2 points off for not realising the constructors print
things (ie, no "On!" outputs, or for getting
the wrong number of constructors (ie, 1 or 3
"On!" outputs).
- 2 points off for getting the last line wrong
("Lights turned off: 3" was a favorite
response... which is bad, because the object that is
being turned off at the end has never been turned off
before!)
- various other ways to go wrong cost various amounts of
points.
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:
- Not having return values for methods (eg
public
rollDice()).
- Not realising that the static method random() should be
used by giving the class name that the method is from,
then the method's name.
- Declaring that methods like rollDice(), or fields like
isCheating, are static. EACH Dice object should have its
own numberUp, and should run its own mathods, so these
things should NOT be static.
- Confusing the duties of the rollDice() method (which
changes the value of numberUp) with the getValue() method
(which returns the value of numberUp). rollDice() should
be void, getValue should return an int.
The Most Common Place for Problems:
- Not knowing how to get a random number between 2 and 12.
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
- No import statements were needed, since the java.lang
package is always automatically imported. But if you had
"import java.lang.Math;" that's fine.
- Ideally you should have made sure that the numberUp
started at some sensible value (eg the default of 2,
above), since otherwise we start with 0 up on the dice
and that's a bit silly. You could either write a
constructor, or set default values when the fields are
declared. But since it was possible to return a 1 (which
is also a little odd for a 2-12 dice roll) we were very
kind and didn't deduct marks for this.
- You didn't need a main() method because this was not
an application. If you wrote one that tested out
your class by creating a Dice object and using it, that's
okay (since having a main method that tests your class
works is a good idea) but if you wrote one that was buggy
that could cost you marks.
- Fields should be private. Methods should be public.
- Some people used the java.util.Random class to create a
Random number. That was okay if you did it right, but in
that case you had to create an object of that class to
act as your random number generator.
- A couple of people generated the number from 2 to 12 by
generating two random numbers from 1 to 6 and adding
them. That's a nice touch, since the real game of craps
works that way, and we didn't insist on a uniform
distribution.
Some general points:
- 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.
- 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.
- Grading for indentation and other style errors was
lenient this time, but see the style notes to be posted
soon on the web.
- 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.
- 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).
- 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.
- Avoid stray marks that might be mistaken for a misplaced
semicolon or other punctuation.