Monday, May 9, 2011

Designing the Spring Class, Part 2

First up is code documentation for the Spring class. Here's what I came up with:

public class Spring

{

// FIELDS

// Array for the watch hands. Future TODO Use a list so the watch can take any number of hands.

Hand[] hand;

// Main spring of watch.

int mainSpring = 0;

// CONSTRUCTORS

/*

Initializes the hand array and declares and initializes the hands that belong to it.

*/

public Spring ()

{

hand = new Hand[4]; // Initialize the array

hand[0] = new Hand(60, 1); // second hand

hand[1] = new Hand(3600, 60); // minute hand

hand[2] = new Hand(43200, 3600); // hour hand

hand[3] = new Hand(86400, 43200); // day hand

}

// PROPERTIES

// METHODS

/*

Displays all the hands, in order: am/pm, hour, minute, second.

Displayed on a single line. Space serves as separator.

*/

public void display ()

{

Console.Write(hand[3].Display + " ");

Console.Write(hand[2].Display + " ");

Console.Write(hand[1].Display + " ");

Console.Write(hand[0].Display + " ");

}

/*

Simulates wining the clock by increasing the value of mainSpring.

Ignores input after spring reaches 100000.

Returns current value of spring.

*/

public int wind()

{

if (mainSpring [LESSTHAN] 100000) // Maximum tension the spring can take.

{

mainSpring++;

}

return mainSpring;

}

/*

Causes each of the hands to tick once while unwinding the spring one tick.

Returns false if mainSpring is not wound or if any of the hands return a false.

Returns true if everything works.

*/

public bool tick()

{

if (mainSpring [LESSTHAN] 1) return false;

bool temp = true; // Local variable to keep track of results

foreach (Hand x in hand) // For each hand in the array. All take ticks

{ // Does the tick as a side-effect

temp = (x.tick() | temp); // Allows a single false to return a false value.

}

mainSpring--;

return temp;

}

#if DEBUG

// TESTING CODE

#endif

// OVERRIDES

}

Next, I'm going to go back to the blog, where I wrote down what I meant to do with this project.

Well, I planned on including a Set method that would allow the mainSpring to set the clock. Let's do a simple one.

Here it is:

public bool set(int setValue)

{

// Get all hands into a known state.

hand[0] = new Hand(60, 1); // second hand

hand[1] = new Hand(3600, 60); // minute hand

hand[2] = new Hand(43200, 3600); // hour hand

hand[3] = new Hand(86400, 43200); // day hand

while (setValue [GREATERTHAN] 0) {

if (!(tickLight())) return false; // calls correct number of ticks.

setValue--; // false if a tick fails.

}

return true; // true on success.

}

I had to create a new private method that did a tick without decrementing the mainSpring field:

private bool tickLight()

{

bool temp = true; // Local variable to keep track of results

foreach (Hand x in hand) // For each hand in the array. All take ticks

{ // Does the tick as a side-effect

temp = (x.tick() | temp); // Allows a single false to return a false value.

}

return temp;


}

And now I have to do some testing.

I was having a problem testing the display method – the output wasn't comparable to anything. It wasn't working like a string or an int.

I should be able to redirect the output of the Display command to an object and serialize that object for later use. I think I did one of the tests in the earlier project htat way.

But that didn't work here. I do have another way. Let's try this:

# Testing Spring set method

#

$testSpring = new Spring

$testSpring2 = new Spring


$temp = $testSpring.set(99)

$temp = $testSpring.Display()


for ($a = 1; $a -lt 100; $a++)

{

$temp = $testSpring2.wind()

$temp = $testSpring2.tick()

}


"Expected chaff:"

if (!($testSpring.Display() -eq $testSpring.Display())) {"FAIL: Set value should equal 99."}



#

# END Testing Spring set method

This is a little bit klugey.

The fact is, my display method is a little klugey. It relies on a side-effect of the Display method instead of returning a value.

But it is sufficient for now. I'll include a TODO note for future development.

The other thing that planned for back on April 29, 2011, was an entry point so that this project could be compiled outside of my PowerShell setup:

public class Watch

{

static void Main(string[] args)

{

Spring x = new Spring();

}

}

I'm just going to verify that everything still runs and call it a day.

No comments:

Post a Comment