Question

java - A recursive method findR() to search for a number in list (ArrayList) - A...

java

- A recursive method findR() to search for a number in list (ArrayList)

- A recursive algorithm to multiply two positive integers m and n using repeated addition.

with output

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

Thanks for the quesiton, here are the two recursive methods in implemented in Java with sample out.

==================================================================================

import java.util.ArrayList;

public class Recursive {

// method takes in the arraylist, the number to search and the current index = 0

    public static void findR(ArrayList<Integer> numbers, int searchNumber, int startingIndex) {

        if (startingIndex == numbers.size()) {
            System.out.println("Number :" + searchNumber + " does not exist");
            return;
        } else {
            if (numbers.get(startingIndex) == searchNumber) {

                System.out.println("Number :" + searchNumber + " exist in the list");
                return;
           } else {
                findR(numbers, searchNumber, startingIndex + 1);
            }
        }

    }

// method recursively multiplies two number using addition


    public static int multiplyByAddition(int x, int y) {


        if (y == 0) {
            return 0;
        } else {

           return x + multiplyByAddition(x, y - 1);
        }

    }


    public static void main(String[] args) {

        ArrayList<Integer> numbers = new ArrayList<>();
        for (int i = 1; i <= 10; i++) numbers.add(i);

        findR(numbers, 8, 0);
        findR(numbers, 18, 0);

        System.out.println("multiplyByAddition(6,7) = " + multiplyByAddition(6, 7));


    }
}

==============================================================================

Add a comment
Know the answer?
Add Answer to:
java - A recursive method findR() to search for a number in list (ArrayList) - A...
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
  • Write an implementation of the following Java method: /* search(ArrayList, int key) -> int method is...

    Write an implementation of the following Java method: /* search(ArrayList, int key) -> int method is passed an array list of integers and an integer key method searches for the key in the array list and returns: the index of the key if it is in the array list -1 otherwise */

  • (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value...

    (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0.          public static Integer max(ArrayList<Integer> list) b. Write a method that returns the sum of all numbers in an ArrayList: public static Integer sum(ArrayList<Integer> list) c. Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes...

  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • 4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array...

    4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...

  • Write a recursive method in java to find GCD of two integers using Euclid's method. Integers...

    Write a recursive method in java to find GCD of two integers using Euclid's method. Integers can be positive or negative. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“The GCD of 24 and 54 is “+r.findGCD(24,54)); //6                 }                 public int findGCD(int num1, int num2){                                 return -1;                 } }

  • (Recursive Binary Search) Write a recursive method recursiveBinarySearch to perform a binary search of an array....

    (Recursive Binary Search) Write a recursive method recursiveBinarySearch to perform a binary search of an array. The method should receive the search key, starting index and ending index as arguments. If the search key is found, return its index in the array. If the search key is not found, return –1. (NOTE: Complete the recursiveBinarySearch method in the BinaryArray class). java

  • *JAVA* Assume we already have an ArrayList of Integers called list. Write code that will add...

    *JAVA* Assume we already have an ArrayList of Integers called list. Write code that will add 1 to each element of the list. For example, if the list originally contained 2, 4, -3, then it should contain 3, 5, -2 after the lines of code execute. Just write the necessary lines of code (not a whole method or program). Assume we already created the ArrayList and filled it with Integers.

  • 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...

  • (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int...

    (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search function...

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