For this problem, you'll be implementing a menu of options
similar to those in previous assignments. No classes other than
CS5App will be needed. Each menu option will
require its own method, however, and all of these methods
must be recursive.
The menu of options
Your program should offer the following menu (in a while loop) to the user:
Each menu option will ask for one or more inputs, pass them to
a recursive method, and then print out an appropriate result.
Each option is described in more detail below. A suitable
interface is shown in the example run.
The Recursive Roster:
(1) factorial
(2) power
(3) harmonic series
(4) palindrome checker
(5) hanoi solver
(6) min mover
(7) list sorter
(9) quit
Which option would you like?
The recursive methods
All of these should be in the CS5App class:
public static double factorial(int n)
It should return the factorial of the input n as a double.
If the input is zero or negative, this method should return 1.0.
public static double power(double b, int n)
It should return the value of b to the n power.
If n is zero or negative, this method should return 1.0.
Remember that taking a power is NOT repeated squaring -- it's
repeated multiplication by the base, b.
public static double harmonic(int n)
It should return the value of the first n terms of the harmonic
sequence, i.e., the sum of the reciprocals of the first n
positive integers. For example,
harmonic(1) = 1/1 = 1.0
harmonic(2) = 1/1 + 1/2 = 1.5
harmonic(5) = 1/1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.28333
If n is one or less, this method should return 1.0.
Watch out for type casting here -- remember that in java, 1/2 is
equal to 0, but 1.0/2 is equal to 0.5!
public static boolean isPalindrome(String s)
It should return true if s is a palindrome, i.e.,
if s is the same either forwards or backwards. It should
return false otherwise. Note that any word of
one letter is a palindrome and any word
of zero letters is also a palindrome. After all, one-letter and zero-letter
words do read the same forwards and backwards.isPalindrome quite easily
with the reverse method from the class worksheet, the intent of
this assignment is to write each method recursively (do not use that function
here!).substring when
writing the palindrome function, although this is by no means required.
(Instead, you could change the function signature by adding some additional
index arguments, similar to what was done in class on the worksheet with
reverse). substring takes in two integers as input and
returns the piece of this that lies between those two
indices. Here are a couple of examples of how substring
works. Note that the substring is taken from the first index and continues up
to but not including the second index:
String s = "hamburger";
String t = "";
t = s.substring(4,8); // t is now the String "urge"
t = s.substring(0,1); // t is now the String "h"
t = s.substring(1,s.length()); // t is now the String "amburger"
public static void solve_hanoi(int height, int fromPole, int withPole, int toPole)
It should print the series of moves needed to
solve the Towers of Hanoi puzzle for height disks (it is because
the answer is being printed to the console that the return type is
void). From main you'll want to assume the source pole (the
one originally containing the disks) is pole 1 and that the
destination pole (where they end up) is 3. Pole 2 is the
"temporary pole".
public static void moveMinToLeft(double[] A, int L, int U)
This method does not return anything, but when called
the minimum element of the array A that was anywhere
between index L and index U inclusive,
should be swapped around so that it now is located at
index L. (These stand for "lower" and "upper"
indices.) Other elements may be swapped around as well,
but no elements of A should be deleted or added.
Note that for this moveMinToLeft option and the sort one below, your code (in main somewhere) will have to ask the user for, first, the number of elements to have in the array, and then the elements in the array, and THEN it should ask for the low and high indices that will be used for L and U when calling moveMinToLeft. (You can assume the user always enters "valid" values for L and H.) It will also need to print the array after the method is called. See the example below for a guide to input and output.
public static void sort(double[] A, int L, int U)
It does not return anything, but after sort is called,
the elements between index L and U,
inclusive, in the input array A should be sorted in ascending
order. Hint: Use the above moveMinToLeft
method, as well as recursion.
Sample Run
Here is an example run -- the text
the user types is in blue. Remember that all of the prompting
for inputs and printing of outputs should be done in main:
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Choose a recursive function to test:
1 factorial
2 power
3 harmonic series
4 palindrome checker
5 hanoi solver
6 min mover
7 list sorter
9 quit
Choose an option: 1
Type a nonegative integer to take the factorial of: 6
The factorial of 6 is 720.0
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >
Choose an option: 2
Type a base (a double) and a power (a nonnegative int): 5.5 3
5.5 to the 3 power is 166.375
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >
Choose an option: 3
Type a positive number of terms: 1000
The sum of 1000 terms is 7.485470860550343
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >
Choose an option: 3
Type a positive number of terms: 5
The sum of 5 terms is 2.283333333333333
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >
Choose an option: 4
Type a possible palindrome: racecar
racecar is a palindrome
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >
Choose an option: 4
Type a possible palindrome: racecars
racecars is NOT a palindrome
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >
Choose an option: 5
Enter tower height 3
Solution is:
1->3
1->2
3->2
1->3
2->1
2->3
1->3
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >
Choose an option: 6
How many numbers would you like to handle? 5
Type the 5 numbers:
#0: 67.8
#1: 54.01
#2: 1.10
#3: 100
#4: -65
Type the low index (L): 1
Type the low index (U): 3
The new list is
67.8
1.1
54.01
100.0
-65.0
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >
Choose an option: 7
How many numbers would you like to sort? 5
Type the 5 numbers:
67.8
54.01
1.10
100
-65
Type the low index (L): 0
Type the low index (U): 4
The sorted list from 0 to 4 (inclusive) is
-65.0
1.1
54.01
67.8
100.0
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >
Choose an option: 9
Submission Be sure to submit your CS5App.java
file under homework 11,
problem 1.
Extra Credit There is no Extra Credit this week.
The final project (linked here) is a large program that implements a movie database. (The full description will be handed out in class during week 14.) Because it is by far the largest software project in CS 5, this problem intends to be sure that everyone has at least a skeleton prepared in advance. (Trying to write the movie database a few hours before the deadline would not be a good idea!)
Thus, for this problem, you will need to write all of the classes and data members needed for the movie database. In addition, you will need to write stubs (empty methods) for all of the functions -- with the correct signature and return value for each; you do not need to actually implement them until week 14. You should also comment them this week (that way you'll have a sense of what each does!)
What do I return?
You may return anything of the correct type, but you do
need to return something in order for your code to compile (which
we will check!). So, for example, you could return 0 for an int,
0.0 for a double, and true for a
boolean. For objects, the value you should
return is null, which is a reference that doesn't
refer to anything! Though it's not too useful in and of
itself, it is a legal value for any variable of some class type.
Thus, the stub for your "getFilmDB" method in the Director
class could look like this:
Finally, your code should compile and run. By run I mean it needs to implement
an initial question (ask the user for the capacity of the database) and then
provide a main menu of user options (as well as accept their input). Of course,
aside from the quit option, individual options do not have to work until week
14.
public FilmDB getFilmDB()
{
return null;
}
Commenting: Your code should have the usual comments for each method
(this will help considerably when writing the actual methods for the final
assignment!). That is, they should have a brief note of purpose and should
explain their inputs and outputs. Here's a complete example for the above
getFilmDB method in the Director class:
/* method: getFilmDB, used to obtain all of a director's films
*
* inputs: none
* return: an object of class FilmDB holding "this" director's films
*/
public FilmDB getFilmDB()
{
return null;
}
Classes: Here is a list of each class, along with their data and method members. A short description for each is here so that you'll be able to write comments on what each method does. You may include more methods (either this week or in week 14), but you should have at least these:
private static FilmDB myFilms private static DirectorDB myDirectors public static void main(String[] args) public static void printMenu() public static void saveDB(String fileName) saves the film database to a filepublic static void readDB(String fileName) loads a film and director database from a filepublic static void readFilm() private String title private int year private String rating private double review private Director dir public Film(String title, int year, String rating, double review, Director dir) public String getTitle() public int getYear() public String getRating() public double getReview() public void save() saves all of a film's data to filepublic void display() prints all of a film's dataprivate Film[] films private int count public FilmDB(int capacity) public boolean isFull() public int getCapacity() returns the length of the films array public int getCount() returns the number of films in the film databasepublic void addFilm(Film f) adds a film to this film databasepublic Film findFilmByName(String fulltitle) returns null or the film already present by that titlepublic void saveAllFilms() public void displayAllFilms() public void displayFilmsByTitle(String titlepiece) public void displayFilmsByYear(int year) public void displayFilmsByRating(String rating) public void displayFilmsByReview(double minreview) private String fname private String lname private FilmDB filmDB public Director(String fname, String lname, int capacity) public String getFullName() public String getFirstName() public String getLastName() public FilmDB getFilmDB() private Director[] dirs private int count public DirectorDB(int capacity) public boolean isFull() public int getCapacity() returns the length of the dirs array public int getCount() returns the number of dirs in this director DBpublic void addDirector(Director dir) adds a director to this director DBpublic void displayAllDirectors() public Director findDirectorByName(String lname, String fname) returns null or the director already present with the input name
Sample Run:
Welcome to the bottomless CS 5 film database!
Please choose from the following list:
0 Display all directors
1 Display all films
2 Display all films by Title
3 Display films by Director
4 Display films by Year
5 Display films by Rating (G, PG, ...)
6 Display films by Review (0-10)
7 Add a new film
8 Save database to a file
9 Load database from a file
42 Quit
Enter a choice: 42
Until next time, the gallery is closed.
Submission For this problem, submit your CS5App.java file
under Homework 11, Problem 2.