Exam Question 2

Well done folks. Yes, the breaks were missing, which is an interesting foible of C, C++ and Java. I'd used this in a good way:
case 'a':
case 'A':


This makes sure that the code will work for upper case and lower case command letters. However, it also means that the program continues when you don't want it to:


case 'A':
AddEntry();
case 'd':
case 'D':
DeleteEntry();

Once the program has performed the AddEntry it carries on and does the delete, which is not what you want. You have to explicitly end the case clause:

case 'A':
AddEntry();
break;
case 'd':
case 'D':
DeleteEntry();
break;


C# will not let you do this, which is nice. In C# you have to explicitly chain the cases together. Of courese, I'd probably convert the command to upper case before the switch if I was really writing this code. It makes the code smaller, faster and clearer.
The missing break is one of the most common typo based programming errors. Two others that I've seen are:
for ( count = 0 ; count < limit ; count++ ) ;
{
// something in here which happens when you dont expect it
}

and
if ( x = 5 ) {
// at least Java won't let you do this one
}

Beginning programmers think that their work is done when the program compiles. Ha! If you like a good read and want to know about this kind of thing you should read Code Complete. In fact, if you haven't read this book I don't think you are a programmer at all!
And so to question 2. A couple of classes which won't compile. They are in the right source files, so what is going on here?

public class Person {
String name ;
public Person ( String inName ) {
name = inName ;
}
}
public class SecurityGuard extends Person{
int securityLevel;
public SecurityGuard ( int inSecurityLevel) {
securityLevel = inSecurityLevel ;
}
}

I've just discovered that Steve McConnell is writing another editiion of Code Complete. Wahay! Incidently, he has also written some of the best books ever on project management. Take a look at Rapid Development if you want to find out how to really do it!