Question
PROBLEM #1: The year:1777 The setting:General Washingtons camp somewhere in the colonies. The British have been shelling the Revolutionary forces with a large cannon from
PROBLEM #1:
The year:1777
The setting:General Washingtons camp somewhere in the colonies.
The British have been shelling the Revolutionary forces with a large cannon from within their camp. You have been assigned a dangerous reconnaissance mission -- to infiltrate the enemy camp and determine the amount of ammunition available for that cannon.
Fortunately for you, the British (being relatively neat and orderly) have stacked the cannonballs into a single pyramid-shaped stack. At the top is a single cannonball resting on a square of four cannonballs, which is itself resting on a square of nine cannonballs, which is itself resting on a square of sixteen cannonballs, and so forth. Given the danger in your situation, you only have a chance to count the number of layers before you escape back to your own encampment.
Your assignment, upon your return, is to write a method which will return the number of cannonballs in the stack, given the number of layers. Good luck!
Requirements:
Come up with a recurrence relation (mathematical expression of the recursive function) for the method. Do this before you start coding.
Put the recurrence relation (including the base case) in the comments for this method.
Write a recursive static Java method to solve the problem
Write a main method which will prompt the spy to type in the number of layers, call the method and display the results.
Make sure the method is never called with a negative number.
Allow the user to enter several values (loop). Use a sentinel to stop the program.
No credit will be awarded for a non-recursive solution.
PROBLEM #2:
The spy also counted the number of troops. To protect the information, the spy encoded it. To decode the number, print it out in reverse order.
Requirements:
This problem "does something" instead of returns a value so there is no recurrence relation. However, you will still have to figure out how to solve this problem recursively. Do this before you start coding.
Write a recursive static Java method to solve the problem
Write a main method which will prompt the spy to type in the number of troops and call the method. The method will display the results.
Make sure the method is never called with a negative number.
Allow the user to enter several values (loop). Use a sentinel to stop the program.
No credit will be awarded for a non-recursive solution.
Other requirements
The program should display your name
Use good programming style with consistent and correct indenting
Grading
Requirements (approximate points) | Possible Points |
Problem #1 - Cannonballs The method correctly calculates the number of cannonballs in a stack of N layers using recursion (15 pts) A recurrence relation (including base case) is included in the comments (5 pts) |
20 pts |
Problem #2 Reverse number The method correctly displays a number in reverse order using recursion (15 pts) |
15 pts |
main() main() prompts for the number of layers the stack of cannonballs and calls the cannonball method main() displays the number of cannonballs in the stack main() does not permit negative numbers to be passed to the cannonballs method main() includes a sentinel controlled loop which allows the user to call the cannonballs method repeatedly main() prompts for the number of troops and calls the reverse method main() does not permit negative numbers to be passed to the reverse method main() includes a sentinel controlled loop which allows the user to call the reverse method repeatedly Program displays your name (1 pt)
|
5 pts |
Comments For each method, comments include the purpose of the method and the names, type and meaning of each argument |
2 pts |
Style Program blocks are indented consistently and correctly Opening and closing curly braces are aligned consistently Variable names are meaningful Proper capitalization is used |
3 pts |
Submission All .java files must be zipped together and uploaded to D2L | -10% |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started