Time to get started.
Today, I'm going to work on the Hand class. That is, I'm going to start writing and testing that class. Part of my methodology, here, is to be able to design each class independently of the rest of the system, so – except for some assumptions I make on the way, I won't need to think about the Spring class.
First, I just need to copy the stub file and add the name of the class:
Add-Type @'
#define DEBUG
public class Hand
Next, a constructor.
There's only one class for the second hand, minute hand, hour hand, and day hand. I'm going to use a constructor with two parameters: maximum state and increment.
Maximum state: the length of a cycle (60 ticks for a second hand, 3600 ticks for a minute hand).
increment: how many ticks makes up a unit(1 tick for a second, 60 for a minute, 3600 for an hour)
// FIELDS
int maxState;
int increment;
int state = 0;
// CONSTRUCTORS
public Hand (int xState, int xIncrement)
{
maxState = xState;
increment = xIncrement;
}
I can't compile, yet, because I get a warning-as-error that state is given a value that is never used. Never mind, though. I have an idea of the code that I need to write to get this off the ground:
public int Display
{
get
{
return state / increment;
}
}
// PROPERTIES
// METHODS
public bool tick()
{
state++;
if (state > maxState)
{
state = 0;
}
return true;
}
I don't know whether this works. I wrote the code before I worked out the specifics – ON PURPOSE. Some testing will let me check my logic.
So, here's the basic, as expected, use of the constructor:
$testHandConstructor = new Hand(60, 1)
$testHandConstructor = new Hand(60, 1) # second hand
$testHandConstructor = new Hand(3600, 60) # minute hand
$testHandConstructor = new Hand(86400, 3600) # hour hand
$testHandConstructor = new Hand(172800,43200 ) # am/pm hand
(Note to me: Check these figures.)
And it occurs to me that there could be some bad results from unexpected inputs. In particular, the second parameter is used as a divisor. If it is 0 –
Here's some C# code to avoid the worst of what could go wrong:
if (xIncrement < 1) { xIncrement = 1;} // validation to avoid negative numbers and divide by 0 errors.
if (xState < xIncrement) { xState = xIncrement;} // validation to avoid cases where increment is larger than the possible size of the hand.
And testing code:
$testHandConstructor = new Hand(0, 0)
$testHandConstructor = new Hand(60, -1)
So, before I sign off for the day, I'm going to create stubs for the Display property and the tick method.
And I'll pick it up there tomorrow.
No comments:
Post a Comment