Computer Science 252
Problem Solving with Java
Spring 2015, The College of Saint Rose
Lecture 16: Recursion
Date: Tuesday, March 17, 2015
Agenda
- Announcements
- Exam 2 comes out next Tuesday (3/24)
- you will receive a PDF by email so be sure to check that morning
- this is a take-home lab practical exam
- you will be asked to write and/or modify programs
- topics up to GUI, not recursion
- if you understand the class examples and have done the
in-class and lab exercises, you will have no problem with the
problems
- there will be 4 problems:
- "how do I draw that?" where you are given a picture
and description and have to write the program to draw it
- a custom object problem along the lines of the "fun
with flags" program you had on the first exam (except this
time you will type it in and run it)
- a program where you add animation capabilities to a program
- a program that has some inappropriate variable
declarations that you will be in charge of fixing
- due by 9 AM Thursday
- Lab 6: Furious Fowl recap
- Recap of some of the key ideas we have seen that come up frequently
- persistence of data: what needs to be an instance variable
and what should not
- custom classes: teach Java about a new kind of object - how
to create it and how it answers questions (its accessors) and
obeys commands (its mutators)
- information flow within a program
- instance variables to share information among methods of
a given object (instance of a class)
- method calls and their parameters and return values to
share information among objects
- More recursion
- more methods of a recursive structure
- non-graphical recursive methods
Lecture 16 Assignment
Due at the start of class, Thursday, March 19.
Please submit answers to these questions
in Submission
Box under "LA16" or in hard copy by the
start of our next class. We will discuss these questions at the
start of class, so no late submissions are accepted. Please be sure
that your name is clearly indicated in all submissions.
For this lecture assignment, you will be writing a Java application -
a program that is not in a WindowController class but instead has
a main method that does its work. It will be similar in
structure to the Powers example. Write your program in
Factorial.java. As in that example, you will need to create a
keyboard Scanner to read in a number from the terminal and use
System.out.println to issue a prompt and report your answer.
The problem we are going to solve is that of computing factorials.
From math, you know that the factorial of a positive integer n,
denoted n!, is the product of all the numbers from 1 up to n.
- Write a main method of your Factorial class that
prompts for and reads in a positive integer. You need not worry
about error checking the input for this assignment. (2 points)
- Write an iterative method loopFactorial that takes a single
int as its parameter and returns the factorial of that value.
Recall that an iterative method uses loop structures like for
and while loops to do its work. Also update your main
method to call the loopFactorial method on the number entered
by the user, and print out the value returned. (4 points)
- Write a recursive mathematical formulation, along the lines of
the second power formula in the notes, for computing n!. (2
points)
- Write a recursive method recFactorial that uses that
formulation to compute factorials. Also update your main
method to call the recFactorial method on the number entered
by the user, and print out the value returned. (6 points)
Terminology
Examples