Question
Essential Features of Program Documentation 1. An initial comment for the program that includes a. Statement of purpose b. Author and date c. Description of
Essential Features of Program Documentation
1. An initial comment for the program that includes
a. Statement of purpose
b. Author and date
c. Description of the program's input and output
d. Description of how to use the program
e. Assumptions such as the type of data expected
f. Statement of exceptions, that is, what could go wrong
g.Brief description of the major classes
2. Initial comments in each class that state its purpose and describe the data contained in the class (constants and variables)
3. Initial comments in each method that state its purpose. precondi- tions, postconditions, and methods called
4. Comments in the body of each method to explain important fea- tures or subtle logic
SOLVE THE FOLLOWING
All of the recursions programs below must be included in a single, menu driven, program that presents a list of the program options, then proceeds to run each of the individual sub-programs with appropriate input request.
a) Replace AT recursively: Given a string supplied by the user, compute recursively (no loops) a new string where all appearances of "at" have been replaced by "@" using the method header: public static String changeAt(String str) Examples: changeAt("Thecatsatonthehat") outputs " Thec@s@ontheh@ " changeAt("atattattt") outputs "@@t@tt" changePi("tattle") outputs "t@tle"
b) Shaking Hands A group of n people gather for a meeting and they each shake the hand of every other person attending the meeting. When only two people attend, the number of handshakes is one; when a third person attends, they must shake the hands of the two original people bringing the count of handshakes to 3. The fourth person will need to shake the hands of three people increasing the total to 6. Write a method that asks for the number of people attending the meeting and reports the total number of handshakes. The method header is as follows: public static int countHandshakes(int numOfAttendees) Examples: countHandshakes(1) outputs 0 countHandshakes(2) outputs 1 countHandshakes(3) outputs 3 NOTE: There is only one base case. c) Count the stars: Consider the pyramid of stars shown at the right. If the user indicates that the last row will contain 4 stars, then the number of stars in the pyramid will be 10 one for the first row, two for the second row, three in the third row and four in the final row. If the pyramid has 6 stars in the last row, the total will be 21. Create a recursive method that will provide the total number of stars in the pyramid when the number of stars in the last row is supplied. The method header will be: public static int countTheStars(int numInLastRow)
Examples: countTheStars(0) outputs 0 countTheStars(1) outputs 1
countTheStars(2) outputs 3
d) Print the Pyramid This question is related to the previous question. Ask the user how many stars will be in the bottom row of the pyramid. Then print the pyramid along with a statement that shows the count of how many stars are in the pyramid (using the countTheStars() method). Note the shape of the pyramid with the stars clearly centered and a single space between each star. The method header will be: public static void printStarPyramid(int numInLastRow) Examples: This is an example of when the last row is 5 This is an example of when the last row is 3
e) Count the Files in a Directory Create a method which asks they user for the letter of a drive, and then using that letter, counts the number of files in that drive. The File object can be used to connect to a directory. For example, new File(f:\\) would create a new File object connected to the F drive (assuming it exists). Once you have this object, three methods which would be useful are: a) listFiles() which returns a File[] array representing all the files and directories in the calling File object; b) isDirectory() which returns true if the file is a directory; and c) isFile() which returns true if the file is a normal file (ie. not a directory). Create a recursive method which will: a) Use the method header: public static int countFiles(String directoryName) b) Use a loop in the recursive method to process every file in the directory. (this is one of those exceptions where a loop is used within the recursive method) . This is done after calling the listFiles() method. c) Within the loop, check each File object in the array returned by listFiles(), and if that file is an actual file (not a directory) count it. If the File object is a directory, recursively count the files in that directory and add the result to the count. d) Return the count. It is also useful to know that the listFiles() method will return null if there are no files in the File object (ie. isFile() is true or it is an empty directory.)
f) [20] The Job Offer You just received a programming job offer from a local start-up company. It is a guaranteed one-year position (52 weeks) but it has a peculiar wage offer. They have been having a difficult time with employees leaving before they complete the year, so they have decided to pay less at the beginning and more at the end. The company pays its employees every week. For the first pay-period, you will be paid one cent. For the second pay-period, you will be paid 2 cents; 4 cents for week 3; 8 cents for week 4 after that and the pay keeps doubling every week until the end of the contract. At the end of the first pay period you will have been paid 1 cent; at the end of the second pay period, 3 cents (2 + 1 cents) total has been paid; at the end of the next pay period, the total will be 7 cents (4 + 3 + 1) and so on. Write a program that implements two recursive methods: a) given the week number, the first recursive method will report the amount to be paid for that week (i.e. week 10 [end of 10th pay period] pays $5.12); and b) the second method will report the total amount paid to date given the week number (i.e. by the end of the sixteenth week [16th pay period], $655.35 has been paid in total).
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