Computer Science 252
Problem Solving with Java
Fall 2015, The College of Saint Rose
Lecture 15: Introduction to Recursion
Date: Thursday, October 22, 2015
Today's paper from 26 years ago. After Marty saved the day, of course.
Agenda
- Announcements
- Lecture 14 assignment recap
- Lab 9: Bouncy GUI Ball out
- Recursive structures and methods
- In-class Exercise 15 - (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.
Lecture 15 Assignment
Due at the start of class, Tuesday, October 27.
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. 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
- recursive
- recursive data structure
Examples
Links