Spring 2016, The College of Saint Rose

Lecture 16: Recursion
Date: Thursday, March 24, 2016

Agenda

• Announcements
• Exam 2 Lab Practical is now done. Exam 3 will be back to a written exam, scheduled for April 19.
• Lab 9: Bouncy GUI Ball out
• Lab 10: Final Project out
• Recursive structures and methods
• In-class Exercise 16 - (10 lecture assignment points) due before the end of class.

Augment the NestedSquaresDrag example to include the following additional functionality:

• All of the FramedRects in the NestedSquares object become red if the mouse moves over the object, and become black again when the mouse is moved off.
• The NestedSquares object should be hidden when the mouse moves outside the canvas, and shown again when it moves back inside.

Please demonstrate your program or submit an archive of your project folder by email before you leave class. Email submissions should use a meaningful subject line, clearly indicating the course number and assignment name.

Due at the start of class, Tuesday, March 29.

Please submit answers to these questions either as a hard copy (typeset or handwritten are OK) or by email to terescoj AT strose.edu by the start of class. Please use a clear subject line when submitting by email (e.g., CSC 252 Lecture 16 Assignment, Mary Smith). We will discuss these questions at the start of class, so no late submissions are accepted.

In preparation for the non-graphical recursive methods we'll be working with, let's refresh your memory of how to write methods that compute and return things.

For example, if you were asked to write a method named averageOfThree that takes three double parameters and returns their average, your method might look like this:

```    public static double averageOfThree(double a, double b, double c) {

return (a + b + c)/3;
}
```

If asked to write a method named countTheCaps that takes a String as its only parameter, and returns the number of capital letters that occur in the String, your method might look like this:

```    public static int countTheCaps(String s) {

int capCount = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if ((c >= 'A') && (c <= 'Z')) {
capCount++;
}
}
return capCount;
}
```

Write a program Methods.java, a Java Application, not a WindowController, with the following functionality:

• A method named area that takes two parameters, a width and a height of a rectangle, and returns its area. (3 points)
• A method named perimeter that takes two parameters, a width and a height of a rectangle, and returns its perimeter. (3 points)
• A method named howManyTwos that takes one parameter, a positive integer, and returns the number of times that number is evenly divisible by 2. Another way to say this is how many times 2 appears in the number's prime factorization. (3 points)
• A main method that tries all three of these out. You should create a keyboard Scanner, prompt your user for input values, and print out what the methods return for those values. (3 points)

Please note that all of your methods will need the static qualifier before their return type to be able to be called easily from main (which must always be a static method).

Terminology

• base case
• recursive case

Examples