Question

IN JAVA!! THANK YOU Assignment Overview You are to include both of these recursive assignments in...

IN JAVA!! THANK YOU

Assignment Overview

You are to include both of these recursive assignments in your code submission. Remember to well-document your code and submit your code and output as text file. 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 Main) to test these recursive methods.

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:

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 method to display how one solider crosses the river

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

// Java Code

public class RecursiveMethods {

   public static String getSoldierString(int s){
       if(s == 1){
           return "soldier";
       }else{
           return "soldiers";
       }
   }
  
   public static void crossRiver(int sLeft, int sRight){
       String sLeftSoldier, sRightSoldier;
      
       if(sLeft == 0){
           return;
       }
      
       sLeftSoldier = getSoldierString(sLeft);
       sRightSoldier = getSoldierString(sRight);
       System.out.println("--------------------------------------------------");
      
       System.out.println(sLeft + " " + sLeftSoldier + " ----> "+ sRight + " " + sRightSoldier+", 2 boys");
       System.out.println(sLeft + " " + sLeftSoldier + ",1 boy <---- "+ sRight + " " + sRightSoldier + ", 1 boy");
       sRight = sRight + 1;
       sLeft = sLeft - 1 ;
       sLeftSoldier = getSoldierString(sLeft);
       sRightSoldier = getSoldierString(sRight);
       System.out.println(sLeft + " "+ sLeftSoldier + ",1 boy ----> "+ sRight +" " + sRightSoldier + ", 1 boy");
       System.out.println(sLeft + " "+ sLeftSoldier + ",2 boys <---- "+ sRight + " " + sRightSoldier);
      
      
       crossRiver(sLeft,sRight);
      
   }
  
   public static String integerToBinary(int n){
       if(n == 0){
           return "0";
       }else if(n == 1){
           return "1";
       }else{
           String rem;
           if(n%2 == 0){
               rem = "0";
           }else {
               rem = "1";
           }
           return integerToBinary(n/2) + rem;
       }
      
   }
  
  
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       int n = 2;
       System.out.println("\nRiver crossing for "+n +" soldiers below : ");
       crossRiver(n,0);
       n = 3;
       System.out.println("\nRiver crossing for "+n +" soldiers below : ");
       crossRiver(n,0);
       n= 4;
       System.out.println("\nRiver crossing for "+n +" soldiers below : ");
       crossRiver(n,0);
       System.out.println("--------------------------------------------------");
       System.out.println();
       n = 1;
       System.out.println("Binary representation of " + n + " is "+ integerToBinary(n));
       n = 11;
       System.out.println("Binary representation of " + n + " is "+ integerToBinary(n));
       n = 47;
       System.out.println("Binary representation of " + n + " is "+ integerToBinary(n));
       n = 483648;
       System.out.println("Binary representation of " + n + " is "+ integerToBinary(n));
      
   }

}

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

// output for the above

Add a comment
Know the answer?
Add Answer to:
IN JAVA!! THANK YOU Assignment Overview You are to include both of these recursive assignments in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this assignment, you will write a Java program(s) to print the binary representation of a...

    In this assignment, you will write a Java program(s) to print the binary representation of a positive integer inserted from command line.  You must finish your assignment in 2 different ways: Using a recursive method Using an iterative method     Your main method must be: public static void main(String[] args) {      int input;         input = Integer.parseInt(args[0]);     print_recursion(input);     print_binary(input); }     You must implement a class and test your program by different input values, including 0. Comment your program properly Example of test...

  • Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement...

    Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement two recursive methods and write JUnit tests for each one. You may write all three methods in the same class file. You are required to write Javadoc-style documentation for all of your methods, including the test methods. Procedure 1) Write method!! a recursive method to compare two Strings using alphabetical order as the natural order (case insensitive, DO NOT use the String class built-in...

  • 10. Recursive Append Download the file AppendRec.java. Write your code inside the appendNTimes method and use...

    10. Recursive Append Download the file AppendRec.java. Write your code inside the appendNTimes method and use the main method to test your code. Submit the file AppendRec.java. There is no need to delete the main method, the autograder will only grade appendNTimes. The method appendNTimes is recursive and takes two arguments, a string and an integer. It returns the original string appended to the original string n times. The method signature is as follows public static String appendNTimes ( String...

  • Assignment Overview In Part 1 of this assignment, you will write a main program and several...

    Assignment Overview In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of baseball player data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on. Purpose This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of...

  • Recursion Exercises These exercises provide practice with recursion in Java. Objectives Module: To write recursive solutions...

    Recursion Exercises These exercises provide practice with recursion in Java. Objectives Module: To write recursive solutions for basic problems that require iteration. To identify and address base cases in a recursive method. Reminders during development Each of your solutions to the problems below should be in their own method. If a method header is provided for a problem, then your solution should have that exact method header. Any changes to the method header will receive a zero for that problem....

  • You will implement and test several short recursive methods below. With the proper use of recursi...

    this can be done in one class or two separate classes,in java thanks! You will implement and test several short recursive methods below. With the proper use of recursion, none of these methods should require more than a dozen lines of code. Test all these in the same class. Keep adding methods and testing until all are working 5. A Fractal Pattern Examine this pattern of stars and blanks, and write a recursive method that can generate patterns such as...

  • Hi, I have Java programming problem: Please solve using Java. Thanks. Best Regards. In the army,...

    Hi, I have Java programming problem: Please solve using Java. Thanks. Best Regards. In the army, each soldier has an assigned rank. A soldier of rank X has to report to (any) soldier of rank X + 1. Many soldiers can report to the same superior. Write a function: class Solution { public int solution(int[] ranks); } that, given an array ranks consisting of soldiers' ranks, returns the number of soldiers who can report to some superior. Examples: 1. Given...

  • Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link cl...

    Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link class by writing methods described below. Do not use loops, or create any more methods (other than those specified), class or instance variables. public class Link private Link next; /null if this is the last link private int value; public Link(Link n, int v) nextn valuev; Do not use loops, or create any more methods (other than those specified), class or...

  • Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application...

    Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT