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

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...
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
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.
The Problem Below are a series of problems you need to solve using recursive methods BY...
The Problem Below are a series of problems you need to solve using recursive methods BY using java . You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. (15 points for main method) DescArrayCheck (15 points) Write a recursive method that checks whether an...
Considering all your findings using the different methods of analysis on the financial statements for Target...
Considering all your findings using the different methods of analysis on the financial statements for Target and Amazon, which company would you more likely invest in? Your response to this question should be 3 paragraphs: One paragraph summarizing key points of analysis on Target One paragraph summarizing key points of analysis on Amazon One paragraph using these results to support and rationalize your investment decision
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the...
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the object with the largest or smallest measure from an array of Measurable objects.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT