Question: Need help writing this practice program in Java code. All the methods are static and to be included in a class named Recursives, so that

Need help writing this practice program in Java code.

All the methods are static and to be included in a class named Recursives, so that a test program can directly call these methods via the class name without creating objects of the class. In addition, write a test program (within a Main Class) to test these recursive methods. Include comments.

Step 1: Divide and Conquer

A detachment of n soldiers must cross a wide and deep river with no bridge in sight. They noticed two boys playing in a rowboat by the shore. The boat is so tiny, however, that it can only hold two boys or one soldier. How can the soldiers get across the river and leave the boys in joint possession of the boat? Write a recursive method to solve the problem with this header:

1 public static void crossRiver(int n)

//Your output should look like this for two soldiers:

--------------------------------------------------

2 soldiers ----> 0 soldiers, 2 boys

2 soldiers, 1 boy <---- 0 soldiers, 1 boy

1 soldier, 1 boy ----> 1 soldier, 1 boy

1 soldier, 2 boys <---- 1 soldier

--------------------------------------------------

1 soldier ----> 1 soldier, 2 boys

1 soldier, 1 boy <---- 1 soldier, 1 boy

0 soldiers, 1 boy ----> 2 soldiers, 1 boy

0 soldiers, 2 boys <---- 2 soldiers

--------------------------------------------------

//Test your implementation with n = 2,3,4,5 soldiers.

Hint: To show the process, you need to write another ethod to display how one soldier crosses the river.

Step 2: Binary Conversion

Write a method that takes a positive integer n as a parameter and returns its binary representation as String. Write a recursive method to solve the problem with this header:

1 public static String integerToBinary(int n)

Hint: repeatedly divide 2 into n and read the remainders backwards. Test your implementation with n = 1; 11; 47; 483648.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!