In: Computer Science
**** 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
}
}
// 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.