Question

In: Computer Science

// Java // This method takes an integer array as well as an integer (the starting...

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

//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;    
}

Related Solutions

Create a Java program with a method that searches an integer array for a specified integer...
Create a Java program with a method that searches an integer array for a specified integer value **(see help with starting the method header below). If the array contains the specified integer, the method should return its index in the array. If not, the method should throw an Exception stating "Element not found in array" and end gracefully. Test the method in main with an array that you make and with user input for the "needle". starting header ** public...
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
Both in Java Question 1: Write a method, bunnyEars that takes in a integer for a...
Both in Java Question 1: Write a method, bunnyEars that takes in a integer for a number of bunnies and return another integer for the total number of ears that the group of bunnies has. (Assume ear bunny has exactly 2 ears).. Write a method countX, that when given a string counts the number of lowercase 'x' chars in the string. countX("xxhixx") → 4 countX("xhixhix") → 3 countX("hi") → 0 Write a method copies that, when given a string and...
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
In java we will need to create a method that takes the array {1,2,3,4,5} and returns...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns the head reference to a linkedList. We will also need to display the linked list in out main method.
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array...
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array of single        words which are then are edited based on the command        Possible Commands:        remove: for the sent words, remove those from the text        replace: replace the word in an even element of words with a word in an odd element of words        add: add the word given word in the index indicated after...
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer...
Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer values squared and reversed. Example: if the LinkedList has (9, 5,4,6), then the returned list will have (36, 16,25,81). What is the time-complexity of your code? You must use listIterator for full credit. public LinkedList getReverseSquaredList (LinkedList list) { }
1 Write a Java method that takes an integer, n, as input and returns a reference...
1 Write a Java method that takes an integer, n, as input and returns a reference to an array of n random doubles between 100.0 and 200.0. Just write the method. 2. You have a class A: public class A { int i, double d; public A(double d) { this.d=d; this.i=10; } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT