I have an assignment that consists of 3 parts. I already did parts 1, 2, and 3a. But I am having problems with part 3b.
Page1 of 2
CS 143 Assignment 3
Introduction to Recursion
See Canvas for due date!
For this assignment, you will implement a few recursive methods in a Java class called
RecursionIntro. For this 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, or for any other subtasks you may need. (You may reuse code from 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:
n 0 27 987654321 -8443 11121113 -11
eduodd(n) 1 36 896745230 -9552 30002 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(ð) = fibby(âð/3â) + fibby(â2ð/3â) where ð > 0 and âð¥â means the floor of ð¥ (round down).
HINT: recall Javaâs integer division behavior. Table of examples:
n 1 2 3 4 5 6 7 8 9 10 20 100
fibby(n) 2 3 5 5 7 8 8 10 13 13 23 119
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 ð ⥠ð ð¡ððð¡ and ð ⤠ððð.
However, skip a line of output if the previous row printed has the same fibby value.
Page 2 of 2
For example, if printSparseTable(4, 10); is called, you would print:
4 5
5 7
6 8
8 10
9 13
Note that even though fibby(3) == fibby(4), since we didnât print fibby(3), we still print fibby(4). But we
skip fibby(7) because it equals the previously printed fibby value. We also leave out fibby(10) because it
equals the last printed fibby(9). A helper method will probably help with this. (As an aside, âsparseâ
refers to the existence of gaps in the table and is used for matrices, etc.)
Part 3a. Largest power of two less than (10 pts)
Implement the method public static int lp2lt(int n) which calculates and returns the
largest integer power of 2 less than n. You may assume n is greater than 1. For example, if n is 10, the
largest power of 2 less than n is 8 (8 is 2 cubed). If n is 8, the answer is 4. If n is 2, 20 = 1.
Part 3b. There can be only one (20 pts)
You are holding a contest to determine the ultimate penny champion. Each participant in the contest
has decided which side of the penny they like, heads or tails, and keeps that decision to themselves. We
represent heads using the boolean value true and tails using false. The contest participants all get in
a long line (array). They start by pairing up (the first two, second two, and so forth). After the two
members reveal their pennies to each other (battle), if they are different boolean values, the first (left)
person wins, otherwise, the second (right) person wins. There are no draws/ties. The battles are single
elimination: once a participant loses, they stop playing in the contest. The remaining members then
battle it out the same way in the next round (first winner of the first round battles the second winner of
the first round, third winner of the first round battles the fourth winner, etc.). Battle rounds continue to
occur in this way until only one winner emerges. If there are an odd number of people in any round, the
last person gets a âbyeâ and automatically survives to the following round.
Write the following recursive method: public static int champion(boolean[] a)
It should return the index corresponding to the winner of the contest. You do not need to allocate any
arrays for this problem and you should not modify the input array as it contains the decisions that the
participants have made, which never change. Instead, adapt what we learned from binary search. You
will probably need a helper method. You may assume that the array is at least length 1.
Note: It can be proven that the above problem is equivalent to dividing up the line of participants into
two parts (where each part is a contiguous âpieceâ of the line), performing a totally separate
champion contest for each part, and having the winners of the two separate contests pairing up for a
final match. The length of the first part is the largest power of two less than the number of
participants, and the second part is the remaining participants. (Part 3a will help!)
For example, if there are 11 participants, you would divide into a contest of the first 8 participants
followed by a contest of the other 3. The 3 participants would have a pair battling with the other in a
bye. The winner of the faceoff between the pair winner and the bye would survive until the final battle!
See if you understand how this version of the problem results in the same sequence of battles that
would occur in the earlier description.
Abdulrahman Own alla
We provide professional writing services to help you score straight A’s by submitting custom written assignments that mirror your guidelines.
Get result-oriented writing and never worry about grades anymore. We follow the highest quality standards to make sure that you get perfect assignments.
Our writers have experience in dealing with papers of every educational level. You can surely rely on the expertise of our qualified professionals.
Your deadline is our threshold for success and we take it very seriously. We make sure you receive your papers before your predefined time.
Someone from our customer support team is always here to respond to your questions. So, hit us up if you have got any ambiguity or concern.
Sit back and relax while we help you out with writing your papers. We have an ultimate policy for keeping your personal and order-related details a secret.
We assure you that your document will be thoroughly checked for plagiarism and grammatical errors as we use highly authentic and licit sources.
Still reluctant about placing an order? Our 100% Moneyback Guarantee backs you up on rare occasions where you aren’t satisfied with the writing.
You don’t have to wait for an update for hours; you can track the progress of your order any time you want. We share the status after each step.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
From brainstorming your paper's outline to perfecting its grammar, we perform every step carefully to make your paper worthy of A grade.
Hire your preferred writer anytime. Simply specify if you want your preferred expert to write your paper and we’ll make that happen.
Get an elaborate and authentic grammar check report with your work to have the grammar goodness sealed in your document.
You can purchase this feature if you want our writers to sum up your paper in the form of a concise and well-articulated summary.
You don’t have to worry about plagiarism anymore. Get a plagiarism report to certify the uniqueness of your work.
Join us for the best experience while seeking writing assistance in your college life. A good grade is all you need to boost up your academic excellence and we are all about it.
We create perfect papers according to the guidelines.
We seamlessly edit out errors from your papers.
We thoroughly read your final draft to identify errors.
Work with ultimate peace of mind because we ensure that your academic work is our responsibility and your grades are a top concern for us!
Dedication. Quality. Commitment. Punctuality
Here is what we have achieved so far. These numbers are evidence that we go the extra mile to make your college journey successful.
We have the most intuitive and minimalistic process so that you can easily place an order. Just follow a few steps to unlock success.
We understand your guidelines first before delivering any writing service. You can discuss your writing needs and we will have them evaluated by our dedicated team.
We write your papers in a standardized way. We complete your work in such a way that it turns out to be a perfect description of your guidelines.
We promise you excellent grades and academic excellence that you always longed for. Our writers stay in touch with you via email.