Example Solution:
Modulus Operator and Control Flow Assignment
(Console version)
- Which version of C#/ Visual Studio is required for this assignment?
-
The provided files are (obviously) C#, and (less obviously:) ) they were built using
the Visual Studio 2005 product. If you have an older version of Visual Studio (or
are using Mono), you probaby won't be able to open the Solution (.SLN) file. If
this is the case, in order to build this, you'll need to add all the .CS files to
a new project, and then everything should compile & run just fine.
- Other than the System.Random, and System.Console classes, this program uses no other
classes, so this *should* be good to go on VS 2003, and Mono ; it hasn't been tested
on those systems, though.
- Refactoring / function decomposition
-
The user-interface input validation can serve as a good example about how bad it
is to duplicate code - verbose, easy to miss typos, and inelegant. Wouldn't you
rather figure it out once, and then not worry about it anymore? Don't you wish you
had functions/methods?
If they have seen methods, it might be a nice add-on for the project to make the
student refactor this code, so that there are a couple of routines that can take
a (string) prompt, and min & max value, and then call instead of the vast amounts
of nearly-identical code that's all in Main.
If you do go that route, it might be interesting to have them discuss / think about
how there's a trade-off around specific messages & generalized, reusable code, since
they'll probably have to write a validation routine that won't be able to explain
that value X is reserved. But again, all of this is beyond the scope of the standard
assignment
- Student solution - redundant code
-
Especially since the students have seen a large amount of code duplication in the
provided example, they may duplicate a lot of code trying to offer the user three
separate operators to be quizzed on. You should be aware of this, and either avoid
the problem entirely (for example, remove the non-modulus operators, so that there
is only 1 operator) , accept it (if you want to focus the assignment on just the
operators), or else head it off early by talking about it in-class, so that they
know to look for (and try to factor) out common code
- Possible Additional Topic: Random Number Generation
- It's easy enough to omit the lines that randomly generate the two operands, if you
want the students to also learn Random number generation.