Recursion program | Course Hero Pro Answers

Recursion program.

Problems

Don't use plagiarized sources. Get Your Custom Essay on
Recursion program | Course Hero Pro Answers
This is a Snippet Preview, get a Complete Solution Here
Order Essay

power

Write a

public static function named

power that takes in two integers,

a and

b, and returns

a^

b, the first argument to the power of the second argument. You may

NOT use

Math.pow(), because that would be boring.

The base case in this method is when the second argument is 0:

n^0 = 1, by definition.

Hint: 3^5 = 3*(3^4)

Why is recursion not the best solution for this problem? If you do not know, ask a TA.

Create a similar method named

power2 that is functionally identical but does not use recursion. It should still NOT use

Math.pow().

fileCount

Write a

public static function named

fileCount that takes as input a directory (as a File object) and returns the total number of files in all sub-directories

Directories count as files, too.

Your method should look through all files and sub-directories in the directory that was passed in. While searching through the directory, use the

java.io.File.isDirectory() method to check if the File you are looking at is a file or directory. If it is a normal file, count it. If it is a directory, count it and use a recursive call to count its contents.

Hint:

What is the base case?

What is the recursive step?

What is the combination?

You should consult the
java.io.File API for more information.

The following directory structure has 14 files:

root_dir 1
+–dir 2
| +–file 3
+–dir 4
| +–dir 5
| | +–dir 6
| | | +–file 7
| | +–file 8
| +–file 9
+–dir 10
| +–dir 11
| | +–file 12
| +–file 13
+–file 14

hanoi

Write a

public static function named

hanoi that recursively solves the Tower of Hanoi puzzle (see the description below).

Your function should take 1 integer and 3 chars as input (in this order):

n,

src,

dest,

aux.

n is the number of disks.

src (source),

dest (destination), and

aux (auxiliary) are tower letters.

For example, to solve the 3 disk puzzle, you would call

hanoi(3,’A’,’C’,’B’);

hanoi(3,’A’,’C’,’B’); means move 3 disks from tower A to tower C using tower B

Your function should print the solution to the puzzle to standard output in the following format:

move [disk] from [tower1] to [tower2]

[disk] is the number of the disk being moved. Disks are numbered 1 to

n, 1 being the smallest and

n being the largest.

[tower1] is the letter of the tower

from which the disk is being moved.

[tower2] is the letter of the tower

to which the disk is being moved.

For example: move 1 from A to C is the first step of solving the 3 disk problem.

Print each step on its own line.

The base case is the 0-disk problem, for which nothing must be done (just return).

What is the recursive step?

What is the combination?

Hints:

Solve the puzzle by hand (
one of many online versions or
another).

Divide and Conquer.

How do you use the solution to the 1-disk problem to help you solve the 2-disk problem?

How about using the 2-disk solution to solve the 3-disk problem?

And so on.

The whole function is just 5 lines of code.

2 of these are the base case

The other 3 are the steps to use the (n-1)-disk solution to solve the n-disk problem.

About the Tower of Hanoi

There is a temple in Kashi Vishwanath which contains a large room with three time-worn posts, on which are stacked 64 golden disks. Brahmin priests, acting out the command of an ancient prophecy, have been moving these disks, in accordance with the immutable rules of the Brahma, since that time. When the last move of the puzzle is completed, the world will end. Despite the location of the legendary temple, the puzzle is most often called the Tower of Hanoi.
The objective of the puzzle is to move the entire stack of disks from the first post to the third post, obeying the following simple rules:

Only one disk may be moved at a time.

Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack.

No disk may be placed on top of a smaller disk.

mysterySeries

Figure out the pattern from the table below.

Write a

public static function named

mysterySeries that take integers

i and

j as input and returns the (

i,

j)-th mystery number (the number in the

i-th row and

j-th column).

mysterySeries(6,2) should return 15

Base Cases:

If

i < 0 or j < 0 or i < j, then A( i, j) = 0 The above 3 base cases are not sufficient, what other base case(s) do you need? Hint: Each element of the i-th row can be computed from elements in the ( i-1)-th row. A(i,j) 0 1 2 3 4 5 6 0 1 1 1 1 2 1 2 1 3 1 3 3 1 4 1 4 6 4 1 5 1 5 10 10 5 1 6 1 6 15 20 15 6 1 .awasam-promo { background-color: #9ED5EA; color: white; text-align: center; padding: 10px; } .button { background-color: #4CAF50; border: none; color: white; padding: 10px 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 10px; } .awasam-alert { color: red; } Get 15% discount on your first 3 orders with us Use the following Order Now The post Recursion program The post Recursion program Recursion program was first posted on June 16, 2020 at 12:54 pm. 2020 . . . Source link Recursion program

CourseHeroPro | CourseHeroAnswers
Calculate your paper price
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.