5. (Recursion.java) Write a java program that has three static recursive methods and a main method....
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
5. (Recursion.java) Write a java program that has three static recursive methods and a main method. In your main method (5 marks) prompt for user input and display the results of each of your recursive methods as shown in the sample output below. a) Write a method that uses recursion to compute the value of a", where a and n are both arguments to the method. If n= 0, the method should return 1 as a = 1. If n = 1, the method should return a as a¹ = a. If n is any other number... that's for you to determine but remember, a" = axa"-¹. b) Write a method that uses recursion to return the reverse of a String that is passed to the method as an argument. Hint: For base cases, consider a string that has 1 or fewer characters... how much work is there to reverse them? Otherwise a reversed string is the last letter of the original string plus the reverse of the rest. Try it out on paper first. c) Write a recursive method that determines the number of digits in an integer, n. Hint: If n<10, there is one digit. Otherwise, it has one more digit than n / 10. For these questions and with recursion in general, the trick is to determine 1) the base case(s) that will cause the function to simply return and end the recursion and 2) the recursive case(s) that will cause the function to call itself with a smaller version of the same problem. All input and output should take place in your main method. *** Don't forget to "eat the new line" if you use your scanner between numbers and strings. java Recursion Enter two numbers, base then exponent: 20 Result: 1 Enter a string to reverse: apples Result: selppa Enter a number: 123 Number of digits: 3 java Recursion Enter two numbers, base then exponent: 100 1 Result: 100 Enter a string to reverse: Result: Z Enter a number: 6 Number of digits: 1 java Recursion Enter two numbers, base then exponent: 2 12 Result: 4096 Enter a string to reverse: i luv cosc! Result: csoc vul i Enter a number: 1234567890 Number of digits: 10 5. (Recursion.java) Write a java program that has three static recursive methods and a main method. In your main method (5 marks) prompt for user input and display the results of each of your recursive methods as shown in the sample output below. a) Write a method that uses recursion to compute the value of a", where a and n are both arguments to the method. If n= 0, the method should return 1 as a = 1. If n = 1, the method should return a as a¹ = a. If n is any other number... that's for you to determine but remember, a" = axa"-¹. b) Write a method that uses recursion to return the reverse of a String that is passed to the method as an argument. Hint: For base cases, consider a string that has 1 or fewer characters... how much work is there to reverse them? Otherwise a reversed string is the last letter of the original string plus the reverse of the rest. Try it out on paper first. c) Write a recursive method that determines the number of digits in an integer, n. Hint: If n<10, there is one digit. Otherwise, it has one more digit than n / 10. For these questions and with recursion in general, the trick is to determine 1) the base case(s) that will cause the function to simply return and end the recursion and 2) the recursive case(s) that will cause the function to call itself with a smaller version of the same problem. All input and output should take place in your main method. *** Don't forget to "eat the new line" if you use your scanner between numbers and strings. java Recursion Enter two numbers, base then exponent: 20 Result: 1 Enter a string to reverse: apples Result: selppa Enter a number: 123 Number of digits: 3 java Recursion Enter two numbers, base then exponent: 100 1 Result: 100 Enter a string to reverse: Result: Z Enter a number: 6 Number of digits: 1 java Recursion Enter two numbers, base then exponent: 2 12 Result: 4096 Enter a string to reverse: i luv cosc! Result: csoc vul i Enter a number: 1234567890 Number of digits: 10
Expert Answer:
Answer rating: 100% (QA)
Here is a Java program that meets the requirements provided This program contains three recursive me... View the full answer
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Posted Date:
Students also viewed these programming questions
-
Portray in words what transforms you would have to make to your execution to some degree (a) to accomplish this and remark on the benefits and detriments of this thought.You are approached to compose...
-
Briefly describe ASCII and Unicode and draw attention to any relationship between them. [3 marks] (b) Briefly explain what a Reader is in the context of reading characters from data. [3 marks] A...
-
The MLC Corporation produces electronic microwave ovens and uses a perpetual inventory system. Its job costing records yield the following information. Job No. 1 m 2 3 4 *5 6 7 Date Finished Sold...
-
Ross Company implemented a quality improvement program and tracked the following for the five years: By cost category as a percentage of sales for the same period of time: Required: 1. Prepare a bar...
-
a. Write an application for the Shady Rest Hotel; the program determines the price of a room. Ask the user to choose 1 for a queen bed, 2 for a king, or 3 for a king and a pullout couch. The output...
-
This case stems from a rocky relationship between two Texans. Lawrence Shipley was the president of Shipley Do-Nut Flour & Supply Co., and Andrea Vasquez was a substitute school teacher in the...
-
Using Microsoft Project, set up the network and determine the critical path for Phase 1 of the project. The project workweek will be 5 days (MF). Given the coming 2010 Winter Olympics in Vancouver...
-
A square insulating plate, 2 meters by 2 meters, lies flat on the floor. A total charge Q = 8 10-8 C is distributed uniformly on the sheet. A pith ball (a small non-conducting ball made of a light...
-
Which theory focuses on the notion that people think that what they see on television and in other media represents the mainstream of what happens in our culture? Multiple choice question. Social...
-
This industry dates back thousands of years, is visible in TV news, is global in nature, and has annual sales of $100 billion. Yet participants do not even agree on how to label it, and most...
-
Reconstruct the confidence interval for the mean home prices given in question 18, but this time construct a 99 % confidence interval. What happens to the size of the confidence interval? Question 18...
-
A quality control engineer believes that the life of light bulbs for his company is normally distributed with a standard deviation of 100 h. A random sample of 10 light bulbs gives the following...
-
Find the following F 1;2; values. (a) v = 8, V = 10, and x = .01 (b) v = 3, V = 11, and x = .005 (c) v = 12, v2 = 9, and x = .05 (d) v = 24, v2 = 19, and x = .025 vi V2
-
Reviewing his records, a grocery store manager finds that the amount of money spent shopping on Friday evenings is normally distributed with a standard deviation of $22. A random sample of 5...
-
Given the area formed by the are of a circle AB and two lines AC and BC, with the dimensions shown. Find the radius R of the circular arc AB in feet, and the interior angle ACB (in degrees, minutes,...
-
Digital Fruit is financed solely by common stock and has outstanding 25 million shares with a market price of $10 a share. It now announces that it intends to issue $160 million of debt and to use...
-
Explain two usages of the super keyword, and state some of the advantages of each type of usage.
-
The Java compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces ({ and }). This behavior can lead to what is referred to as the...
-
Research the Koch Snowflake online then, using the techniques you learned in Section 18.9, implement an application that draws the Koch Snowflake. 18.9.1 Koch Curve Fractal As an example, let's look...
-
How are risk and return each defined? Do you think there is a relationship between them?
-
Describe the components of a business plan. How does each of these components help the following people to make decisions about a business? (a) An investor. (b) A creditor. (c) A manager or owner.
-
Define relevant range. Why is it important?
Study smarter with the SolutionInn App