Computer Science 252
Problem Solving with Java
Fall 2015, The College of Saint Rose
Lecture 17: Recursive Methods and Recursive Structures as Collections
Date: Thursday, October 29, 2015
Agenda
- Announcements
- Lab practical exam a week from Tuesday - see details under "course news"
- Non-graphical recursive methods
- Recursive structures as a collection
- Lab 11: Recursion Practice out, not due until a week
from Monday
Lecture 17 Assignment
Due at the start of class, Tuesday, November 3.
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
17 Assignment , Joe Student). We will discuss these
questions at the start of class, so no late submissions are
accepted.
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)
Examples