Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this assignment, you will implement a few recursive methods in a Java class called RecursionIntro. Fothis assignment, there are some rules spelled out below:

image text in transcribedimage text in transcribed

For this assignment, you will implement a few recursive methods in a Java class called RecursionIntro. Fothis assignment, there are some rules spelled out below: You may not make use of any Java classes other than incidental use of Strings, calls to System.out.println, and accessing an existing array passed to your method. The tester program will verify that your program contains no imports, no use of "new", and the only uses of. (dot) are within calls to System.out.println, accessing array.length, or followed by a digit (double literals) You may not use for or while. Any looping must be accomplished using recursion. The tester program will check for no "for"'s or "while"s and may be triggered by the word in a comment. You may not declare static or non-static member fields- only local or parameter variables allowed You may implement additional helper methods to use the "recursive driver" technique as we did with the find method in class. Part 1. Even digits up, odd digits down (30 pts) Implement the method public static long eduodd(long n). eduodd(n) returns a value which increases each of the even decimal digits of n by one and decreases each of the odd digits of n by one Leading one digits will disappear. The sign of eduodd(n) should be the same as n, unless n is negative and eduodd(n) is zero as seen in the last example below Please review the written practice recursion problems as seen in class. Table of examples 27 36 987654321 -8443 896745230-9552 11121113 30002 eduodd(n) 1 0 Part 2a. Recursive definition (20 pts) Implement the method public static int fibby (int n). fibby is mathematically defined for nonnegative values in the following way fibby(0)1 fibby(n) = fibby(In/4]) + fibby([3n/4) where n > 0 and 1x1 means the floor of x (round down) HINT: recall Java's integer division behavior. Table of examples 10 20 24 4 100 fibby(n) 2 6 Part 2b. Sparse table generation (20 pts) Notice that for many values i, fibby(i) fibby(i+1). Implement the method public static void printsparsetable(int start, int end). Output using System.out.println all consecutive values of n and fibby(n) (just the two numeric values separated by a space) where n start and n S end However, skip a line of output if the previous row printed has the same fibby value For example, if you call printsparsetable(5, 10); you would print 5 6 6 8 8 11 Note that even though fibby(4)fibby(5), since we didn't print fibby(4), we still print fibby(5). But we skip fibby(7)because it equals the previously printed fibby value. A helper method will probably help with this Part 3. Recursive approximate median (30 pts) Consider an array of integers. You wish to find an approximate median. You have an idea: split the array (or a range of the array) into three pieces, find the approximate median of each piece, and then return the actual median of the three approximate medians (if you put the three approximate medians in sorted order, the one in the middle). There are a few details to consider. If we are trying to find the approximate median of a range consisting of one element, that element itself is its own median. If the range contains two elements, take the mean (average) which may be a fractional value. Otherwise, the range contains at least three elements and we can split it into three pieces. The length of the range may be divisible by 3, have a remainder of 1 when divided by 3, or a remainder of 2 when divided by 3 each piece should be n/3 elements If a remainder of 1: the first piece should consist of the first In/3] elements, the last piece should consist of the last In/3] elements, and the middle piece is the remaining [n/31 elements. [n/3] rounds up. (For example, if there were 4 elements, the first piece is the first element, the last piece is the last element, and the middle two elements constitute the middle piece.) If a remainder of 2: the first piece should consist of the first [n/31 elements, the last piece should consist of the last [n/31 elements, and the middle piece is the remaining In/3] elements. (For example, if there were 8 elements, the first piece is the first 3 elements, the last piece is the last 3 and the middle two elements constitute the middle piece.) Implement the method public static double median3(int[] a) that follows the above strategy. Do not create any new arrays; you will need one or more helper methods that look at portions/ranges/pieces of the existing array. Do not modify the array you are given. Examples, where the square braces show the elements in each recursive call's range int[] a-1); median3 (a); I returns 1.0 int[] b- 11, 2); median3 (b); // return 1.5 int[] c -3, -10, 100); median3 (c); // returns 3 int[] d- 11, 2, -5, 10, 100, 6}; median3 (d); // median of [1, 2], [-5, 10], [100, 6]: returns 2.5 int[] e-11, 2, -10, 7, 20, -3, 100, 6}; median3 (e); II [[1], [2], [-10]], [7, 20], [[-3], [100], [6]]: returns 6 int] f-11, 2,-20, 10, 7, 20, -3, 100, 6, 92); median3(f) //[1], [2], [-20]], [[-10], [7,20], [-31], [[10], [6], [92]]: 1.0 For this assignment, you will implement a few recursive methods in a Java class called RecursionIntro. Fothis assignment, there are some rules spelled out below: You may not make use of any Java classes other than incidental use of Strings, calls to System.out.println, and accessing an existing array passed to your method. The tester program will verify that your program contains no imports, no use of "new", and the only uses of. (dot) are within calls to System.out.println, accessing array.length, or followed by a digit (double literals) You may not use for or while. Any looping must be accomplished using recursion. The tester program will check for no "for"'s or "while"s and may be triggered by the word in a comment. You may not declare static or non-static member fields- only local or parameter variables allowed You may implement additional helper methods to use the "recursive driver" technique as we did with the find method in class. Part 1. Even digits up, odd digits down (30 pts) Implement the method public static long eduodd(long n). eduodd(n) returns a value which increases each of the even decimal digits of n by one and decreases each of the odd digits of n by one Leading one digits will disappear. The sign of eduodd(n) should be the same as n, unless n is negative and eduodd(n) is zero as seen in the last example below Please review the written practice recursion problems as seen in class. Table of examples 27 36 987654321 -8443 896745230-9552 11121113 30002 eduodd(n) 1 0 Part 2a. Recursive definition (20 pts) Implement the method public static int fibby (int n). fibby is mathematically defined for nonnegative values in the following way fibby(0)1 fibby(n) = fibby(In/4]) + fibby([3n/4) where n > 0 and 1x1 means the floor of x (round down) HINT: recall Java's integer division behavior. Table of examples 10 20 24 4 100 fibby(n) 2 6 Part 2b. Sparse table generation (20 pts) Notice that for many values i, fibby(i) fibby(i+1). Implement the method public static void printsparsetable(int start, int end). Output using System.out.println all consecutive values of n and fibby(n) (just the two numeric values separated by a space) where n start and n S end However, skip a line of output if the previous row printed has the same fibby value For example, if you call printsparsetable(5, 10); you would print 5 6 6 8 8 11 Note that even though fibby(4)fibby(5), since we didn't print fibby(4), we still print fibby(5). But we skip fibby(7)because it equals the previously printed fibby value. A helper method will probably help with this Part 3. Recursive approximate median (30 pts) Consider an array of integers. You wish to find an approximate median. You have an idea: split the array (or a range of the array) into three pieces, find the approximate median of each piece, and then return the actual median of the three approximate medians (if you put the three approximate medians in sorted order, the one in the middle). There are a few details to consider. If we are trying to find the approximate median of a range consisting of one element, that element itself is its own median. If the range contains two elements, take the mean (average) which may be a fractional value. Otherwise, the range contains at least three elements and we can split it into three pieces. The length of the range may be divisible by 3, have a remainder of 1 when divided by 3, or a remainder of 2 when divided by 3 each piece should be n/3 elements If a remainder of 1: the first piece should consist of the first In/3] elements, the last piece should consist of the last In/3] elements, and the middle piece is the remaining [n/31 elements. [n/3] rounds up. (For example, if there were 4 elements, the first piece is the first element, the last piece is the last element, and the middle two elements constitute the middle piece.) If a remainder of 2: the first piece should consist of the first [n/31 elements, the last piece should consist of the last [n/31 elements, and the middle piece is the remaining In/3] elements. (For example, if there were 8 elements, the first piece is the first 3 elements, the last piece is the last 3 and the middle two elements constitute the middle piece.) Implement the method public static double median3(int[] a) that follows the above strategy. Do not create any new arrays; you will need one or more helper methods that look at portions/ranges/pieces of the existing array. Do not modify the array you are given. Examples, where the square braces show the elements in each recursive call's range int[] a-1); median3 (a); I returns 1.0 int[] b- 11, 2); median3 (b); // return 1.5 int[] c -3, -10, 100); median3 (c); // returns 3 int[] d- 11, 2, -5, 10, 100, 6}; median3 (d); // median of [1, 2], [-5, 10], [100, 6]: returns 2.5 int[] e-11, 2, -10, 7, 20, -3, 100, 6}; median3 (e); II [[1], [2], [-10]], [7, 20], [[-3], [100], [6]]: returns 6 int] f-11, 2,-20, 10, 7, 20, -3, 100, 6, 92); median3(f) //[1], [2], [-20]], [[-10], [7,20], [-31], [[10], [6], [92]]: 1.0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions