Question

In: Computer Science

**** All these methods should be implemented using RECURSIVE solutions (no looping statements) // Java //...

**** All these methods should be implemented using RECURSIVE solutions (no looping statements)

// Java

// This method takes an integer array as well as an integer (the starting
// index) and returns the sum of the squares of the elements in the array.

// This method uses recursion.
public int sumSquaresRec(int[] A, int pos)
{
// TODO: implement this method
      
return -1; // replace this statement with your own return
}   

// This method takes a character stack and converts all lower case letters
// to upper case ones.
public void upperStackRec(CharStack s)
{
// TODO: implement this method
}
  
// This method reads a string and returns the string in the reversed order.
public String reverseStringRec(String s)
{
// TODO: implement this method
      
return "dummy string"; // replace this statement with your own return
}
  
// This method takes as parameters a reference to the head of a linked list, a
// position specified by n, and a key. It returns the number of occurrences
// of the key in the linked list beginning at the n-th node. If n = 0, it means
// you should search in the entire linked list. If n = 1, then you should skip
// the first node in the list.
public int numOccurrencesRec(LNode node, int n, int key)
{
// TODO: implement this method
      
return -1; // replace this statement with your own return   
}
}

Solutions

Expert Solution


    // Java

    // This method takes an integer array as well as an integer (the starting
    // index) and returns the sum of the squares of the elements in the array.
    // This method uses recursion.
    public int sumSquaresRec(int[] A, int pos) {
        if(pos >= A.length) {
            return 0;
        }

        return A[0] * A[0] + sumSquaresRec(A, pos+1);
    }

    // This method takes a character stack and converts all lower case letters
    // to upper case ones.
    public void upperStackRec(CharStack s) {
        if(!s.isEmpty()) {
            char c = Character.toUpperCase(s.pop());
            upperStackRec(s);
            s.push(c);
        }
    }

    // This method reads a string and returns the string in the reversed order.
    public String reverseStringRec(String s) {
        if(s.length() <= 1) {
            return s;
        }

        return reverseStringRec(s.substring(1)) + s.charAt(0);
    }

    // This method takes as parameters a reference to the head of a linked list, a
    // position specified by n, and a key. It returns the number of occurrences
    // of the key in the linked list beginning at the n-th node. If n = 0, it means
    // you should search in the entire linked list. If n = 1, then you should skip
    // the first node in the list.
    public int numOccurrencesRec(LNode node, int n, int key) {
        int count = 0;
        if(node == null) {
            return count;
        }
        if(n <= 0 && node.value == key) {
            count++;
        }
        return count + numOccurrencesRec(node.next, n-1, key);
    }

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using...
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, NOT LOOPS. You may use JDK String Methods like substring() and length(), but do not use the JDK methods to avoid coding algorithms assigned. For example, don’t use String.revers(). public boolean recursiveContains(char c, String s) { if (s.length() == 0) return false; if (s.charAt(s.length() - 1) == c) return true; else return recursiveContains(c, s.substring(0, s.length() - 1)); } public boolean recursiveAllCharactersSame(String s) return...
Write a RECURSIVE algorithm (different from the ones your provided in 3) implemented in Java a...
Write a RECURSIVE algorithm (different from the ones your provided in 3) implemented in Java a in a complete Java program to reverse a stack of characters using recursion. You are not allowed to use loop constructs like while, for..etc, and you can only use the following functions on Stack S shown below (15pts) Provide an explanation of the running time. You will need to implement your own stack if needed isEmpty(S) push(S) pop(S) Make your own assumption and your...
Write a java program with 3 recursive methods that reverse the order of a string. The...
Write a java program with 3 recursive methods that reverse the order of a string. The first recursive method should reverse the order starting from the left selecting the leftmost character as the head and the remaining part of the string as its tail., the second starting from the right selecting the rightmost character as the head and the remaining part of the string on its left as the tail, and the last a recursive method starting from the middle...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java program to reverse a stack of characters. Make your own assumption and your own design on the methods signature, inputs, outputs, and the full main program.
• 2. Get all the information from the user using methods Java • B. if the...
• 2. Get all the information from the user using methods Java • B. if the inputs are not given in the proper format the program should prompt user to give the proper input (eg. Name cannot be numbers, age cannot be String)
(In Java Using recursive techniques) When buying a house or condo (using a mortgage), or when...
(In Java Using recursive techniques) When buying a house or condo (using a mortgage), or when taking out a loan, you’ll likely wind up with some form of a fixed monthly payment. Mortgages and loans work this way: You take out a loan from the bank or lender for a specified amount of money at a specified annual interest rate with a specified monthly payment amount At the beginning of every month, interest is added to the amount you owe...
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Recursion java: 1. Write a recursive algorithm to add all the elements of an array of...
Recursion java: 1. Write a recursive algorithm to add all the elements of an array of n elements 2. Write a recursive algorithm to get the minimum element of an array of n elements 3. Write a recursive algorithm to add the corresponding elements of two arrays (A and B) of n elements. Store the results in a third array C .4. Write a recursive algorithm to get the maximum element of a binary tree 5. Write a recursive algorithm...
java Write a recursive program to reverse a positive integer. . Your method should take a...
java Write a recursive program to reverse a positive integer. . Your method should take a non negative integer as a parameter and return the reverse of the number as an integer. e.g. if you pass 12345, your method should return 54321.
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT