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 */

  • C# Create a “Main” method that contains a 1-Dimensional ArrayList of integers. Randomly fill the ArrayList...

    C# Create a “Main” method that contains a 1-Dimensional ArrayList of integers. Randomly fill the ArrayList with at least 10 integers using a recursive method. Then create another method that calculates the sum of that ArrayList using recursion as well. Only use TWO (2) parameters, at most, in these two recursive methods. Finally, print the array and the sum of ArrayList. Sample Output: ArrayList contents: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 The sum of the ArrayList...

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

  • java code level: beginner write a method sumList() public static Integer sumList(ArrayList<Integer> list) This method calculates...

    java code level: beginner write a method sumList() public static Integer sumList(ArrayList<Integer> list) This method calculates the sum of all Integer values in a given ArrayList. For example, if ArrayList<Integer> list contains {1, 2, 3}, a call to sumList(list) should return 1+2+3, which is 6 as an Integer. Return null if the list is empty or null. Think about the base case. When should the method terminate/end? Under what condition should your method stop making recursive calls and return your...

  • (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list)...

    (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a program to test the method with three different arrays: Integers: 2, 4, and 3; Doubles: 3.4, 1.2, and -12.3; Strings: "Bob", "Alice", "Ted", and "Carol" This program is similar to the Case Study in the book: Sorting an Array of Objects The language must be written, compiled, and run on TEXTPAD. The Language is in Java.

  • 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

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