In: Computer Science
// 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
}
}
//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){
// This is a base when the position is outside the scope of array, return 0
if(pos >= A.length) return 0;
//take the square of the current element, and proceed to the next element
return (A[pos] * A[pos]) + 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){
List<Character> temp_list = new ArrayList<>();
//iterating through the stack popping characters,making them upper case and adding them to a list
while(!s.isEmpty()){
char ch = (char)s.pop();
ch = Character.toUpperCase(ch);
temp_list.add(ch);
}
// iterate from the end of the list as the stack follows FIFO ordering
for(int i=temp_list.size() - 1; i>=0; i--){
s.push(temp_list.get(i));
}
}
// This method reads a string and returns the string in the reversed order.
public String reverseStringRec(String s)
{
StringBuilder sb = new StringBuilder("");
//iterate from the end of the string and keep adding characters to the stringbuilder at the back
for(int i=s.length() - 1; i >= 0; i--){
sb.append(s.charAt(i));
}
//convert to string and return
return sb.toString(); // 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)
{
// the current position of index is 0, key count is 0
int currentIndex=0, keyCount=0;
//while the current index of the list is not equal to the specified index, the pointer moves to the next position
// replace next with the feild provided in the LNode class
while(currentIndex != n){
node = node.next;
currentIndex++;
}
// The current position will be the desired position to start count the key count
while(node!=null){
// If current node has the same key value increase key count
// replace data with the feild provided in the LNode class
if(node.data == key){
keyCount++;
}
node = node.next;
}
return keyCount;
}