Computer Science – Java

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

Don't use plagiarized sources. Get Your Custom Essay on
Computer Science – Java
Just from $13/Page
Order Essay

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

What Will You Get?

We provide professional writing services to help you score straight A’s by submitting custom written assignments that mirror your guidelines.

Premium Quality

Get result-oriented writing and never worry about grades anymore. We follow the highest quality standards to make sure that you get perfect assignments.

Experienced Writers

Our writers have experience in dealing with papers of every educational level. You can surely rely on the expertise of our qualified professionals.

On-Time Delivery

Your deadline is our threshold for success and we take it very seriously. We make sure you receive your papers before your predefined time.

24/7 Customer Support

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.

Complete Confidentiality

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.

Authentic Sources

We assure you that your document will be thoroughly checked for plagiarism and grammatical errors as we use highly authentic and licit sources.

Moneyback Guarantee

Still reluctant about placing an order? Our 100% Moneyback Guarantee backs you up on rare occasions where you aren’t satisfied with the writing.

Order Tracking

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.

image

Areas of Expertise

Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.

Areas of Expertise

Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.

image

Trusted Partner of 9650+ Students for Writing

From brainstorming your paper's outline to perfecting its grammar, we perform every step carefully to make your paper worthy of A grade.

Preferred Writer

Hire your preferred writer anytime. Simply specify if you want your preferred expert to write your paper and we’ll make that happen.

Grammar Check Report

Get an elaborate and authentic grammar check report with your work to have the grammar goodness sealed in your document.

One Page Summary

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.

Plagiarism Report

You don’t have to worry about plagiarism anymore. Get a plagiarism report to certify the uniqueness of your work.

Free Features $66FREE

  • Most Qualified Writer $10FREE
  • Plagiarism Scan Report $10FREE
  • Unlimited Revisions $08FREE
  • Paper Formatting $05FREE
  • Cover Page $05FREE
  • Referencing & Bibliography $10FREE
  • Dedicated User Area $08FREE
  • 24/7 Order Tracking $05FREE
  • Periodic Email Alerts $05FREE
image

Our Services

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.

  • On-time Delivery
  • 24/7 Order Tracking
  • Access to Authentic Sources
Academic Writing

We create perfect papers according to the guidelines.

Professional Editing

We seamlessly edit out errors from your papers.

Thorough Proofreading

We thoroughly read your final draft to identify errors.

image

Delegate Your Challenging Writing Tasks to Experienced Professionals

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!

Check Out Our Sample Work

Dedication. Quality. Commitment. Punctuality

Categories
All samples
Essay (any type)
Essay (any type)
The Value of a Nursing Degree
Undergrad. (yrs 3-4)
Nursing
2
View this sample

It May Not Be Much, but It’s Honest Work!

Here is what we have achieved so far. These numbers are evidence that we go the extra mile to make your college journey successful.

0+

Happy Clients

0+

Words Written This Week

0+

Ongoing Orders

0%

Customer Satisfaction Rate
image

Process as Fine as Brewed Coffee

We have the most intuitive and minimalistic process so that you can easily place an order. Just follow a few steps to unlock success.

See How We Helped 9000+ Students Achieve Success

image

We Analyze Your Problem and Offer Customized Writing

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.

  • Clear elicitation of your requirements.
  • Customized writing as per your needs.

We Mirror Your Guidelines to Deliver Quality Services

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.

  • Proactive analysis of your writing.
  • Active communication to understand requirements.
image
image

We Handle Your Writing Tasks to Ensure Excellent Grades

We promise you excellent grades and academic excellence that you always longed for. Our writers stay in touch with you via email.

  • Thorough research and analysis for every order.
  • Deliverance of reliable writing service to improve your grades.
Place an Order Start Chat Now
image

Order your essay today and save 30% with the discount code Happy