Thursday, December 10, 2009

Surprise Milestone!!

Sample Program 1: Documentation for a factorial function.

/**
* Returns the result of computing factorial on the argument.
* <p>
* This function is borrowed from test-advanced.164. It tests
* to make sure the while-loop works.
*
* @param d the integer to compute factorial on
* @return the result of d!
*/
def fact2(d) {
def r = 1
while (1 < d) {
r = r * d
d = d - 1
}
r
}


Sample Program 2: Documentation for a fibonacci function.

/**
* Returns the nth Fibonacci number.
* <p>
* This function is borrowed from test-fibonacci.164.
*
* @param n the index in the Fibonacci sequence
* @return the nth number in the Fibonacci sequence
*/
def fib(n) {
if (n < 2) {
n
} else {
fib(n - 1) + fib(n - 2)
}
}

/**
* Prints the first n numbers of the Fibonacci sequence.
* <p>
* This function is borrowed from test-fibonacci.164.
*
* @param n how many numbers in the Fibonacci sequence to print
* @see fib
*/
def printfib(n) {
def i = 0
while (i < n) {
print fib(i)
i = i + 1
}
}

printfib(input())


Sample Program 3: Documentation for a Shape Object hierarchy and Shape-list generation.

/**
* A Shape prototype. Represents an abstract polygon.
* <p>
* Shape is the parent class of Circle and Rectangle.
*
* @author Joel
* @version 1.0, 12/06/09
* @method draw
* @see Circle
* @see Rectangle
*/
def Shape = {}
Shape.__index = Shape

/**
* Prints the Shape.
*
* @see Shape
*/
Shape.draw = lambda(self) { print "I'm a shape." }

/**
* A Circle prototype. Represents a polygon with no vertices.
* <p>
* Inherits from the Shape prototype.
*
* @author Joel
* @version 1.0, 12/06/09
* @method draw
* @see Shape
*/
def Circle = {}
setmetatable(Circle, Shape)
Circle.__index = Circle

/**
* Prints the Circle.
*
* @see Circle
*/
Circle.draw = lambda(self) { print "Let's draw a circle!" }

/**
* A Rectangle prototype. Represents a polygon with four vertices
* and four right angles.
* <p>
* Inherits from the Shape prototype, and is the parent class
* of Square.
*
* @author Joel
* @version 1.0, 12/06/09
* @method draw
* @see Shape
*/
def Rectangle = {}
setmetatable(Rectangle, Shape)
Rectangle.__index = Rectangle

/**
* Prints the Rectangle.
*
* @see Rectangle
*/
Rectangle.draw = lambda(self) { print "I have four sides." }


/**
* A Square prototype. Represents a polygon with four vertices,
* four right angles, and four sides of equal length.
* <p>
* Inherits from the Rectangle prototype.
*
* @author Joel
* @version 1.0, 12/06/09
* @method draw
* @see Rectangle
*/
def Square = {}
setmetatable(Square, Rectangle)
Square.__index = Square

/**
* Prints the Square.
*
* @see Square
*/
Square.draw = lambda(self) { print "I'm a special Rectangle that's, well, square." }

/**
* Returns an instance of the argument prototype.
*
* @param parent the prototype to create an instance of
* @return the instance
*/
def makeObject(parent) {
def o = {}
setmetatable(o, parent)
o
}

/**
* Returns a list of user-specified shapes.
* <p>
* This function receives input from the user.
* <p>
* Typing "Shape" adds a Shape instance to the list.
* <p>
* Typing "Circle" adds a Circle instance to the list.
* <p>
* Typing "Rectangle" adds a Rectangle instance to the list.
* <p>
* Typing "Square" adds a Square instance to the list.
* <p>
* Typing 0 cancels user input and returns the list.
* <p>
* Typing anything besides 0 will allow the user to continue adding
* shapes to the list.
*
* @return a list of Shape or Shape-subclass instances
*/
def makeList() {
print "Let's make a list."
def list = {}
def i = 0
def continue = 1
while (continue) {
print "Do you want to add another element to the list? Enter 0 for no; anything else is considered yes."
def result = input()
if (result == 0) {
continue = 0
} else {
print "Please enter the next element to add ('Shape', 'Circle', 'Rectangle', or 'Square')."
def elem = input()
if (elem == "Shape") {
list[i] = makeObject(Shape)
i = i + 1
}
if (elem == "Circle") {
list[i] = makeObject(Circle)
i = i + 1
}
if (elem == "Rectangle") {
list[i] = makeObject(Rectangle)
i = i + 1
}
if (elem == "Square") {
list[i] = makeObject(Square)
i = i + 1
}
}
}
list
}

# No 164docs needed here since we're not defining classes or functions
def objects = makeList()
print "Let's do some drawing."
for o in objects {
o:draw()
}


:)

No comments:

Post a Comment