Tuesday, April 12, 2011

Code Dump 2 of 3

Here's the Stick class:

public class Stick {

// FIELDS

// Value of a stick (true or false).

private bool stickValue = true;

// Location of a stick.

private int location = 0;

// CONSTRUCTORS

/*

Sets the value of a stick location to the specified value.

The default value of a stick is the invalid value 0.

*/

public Stick (int temp)

{

location = temp;

}

public Stick (bool temp)

{

stickValue = temp;

}

// PROPERTIES

/*

Integer location of a stick.

*/

public int Location

{

get {

return location;

}

set {

if (value < 0) throw new Exception();

location = value;

}

}

/*

The value of an individual stick, either true or false.

*/

public bool Value

{

get {

return stickValue;

}

}

// METHODS

// TESTING CODE



// OVERRIDES

public override int GetHashCode()

{

if (stickValue) return 1;

return 0;

}

public override bool Equals(System.Object obj)

{

if (obj != null) { // Needs to fail for null objects.

if (obj.GetType().Equals(this.GetType())) { // Check types. Might be redundant on next.

if (ReferenceEquals(this, obj)) { return true;} // Check reference equals.

}

}

return base.Equals(obj); // This is a Best Practice - if your equals fails, the base equals should be returned.

}

}

No comments:

Post a Comment