Lab assignment: Recursion.

The topic of the lab today is recursion. A recursive method is a method that cllas itself. The behavior of recursive methods is the same as the behavior of a loop inside which the method is calls. In fact every recursive program has an equivalent non-recursive program that can be implemented using loops. In this lab you need to implement recursive methods to move a Circle horizontally on the Canvas. The method responsible for moving the circle must contain a call to itself. You can get started with the Canvas, Circle and Lab9 classes provided in the links below:

Lab9.java
Canvas.java
Square.java

You need to add your code to the class Lab9.java to implement the three stages listed below. Do not forget to print out your names and UIN in the class Lab9.java, which contains the main. The main already creates a Circle on the upper-left corner of the canvas.

Stage 1:
Write a RECURSIVE method to move circle to the upper-right corner. The method can use setPosition to move the circle to the right of 1 pixel and MUST call itself. Remember the method must implement some control on the position of the circle otherwise you might end up with an infinite recursion.

Stage 2:
Write a RECURSIVE method to move circle back to the upper-left corner. The method can use setPosition to move the circle to the right of 1 pixel and MUST call itself. Remember the method must implement some control on the position of the circle otherwise you might end up with an infinite recursion. This method is very similar to the method from stage 1.

Stage 3:
Using the methods of the first two stages have the ball move right and left and right and left.
The circle "ping-pongs" back and forth, each time getting shorter, as if there are walls on the sides moving in on the circle.

The turnin command for this assignment is turnin -c cs102 -p lab9 Lab9.java
If you write code on other files (not necessary) turnin the other files as well.

SOLUTION:

Lab9.java
Circle.java
Canvas.java