Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello Cheggsperts! Couple of Java related questions. AGAIN, PLEASE ANSWER THE FO

ID: 3692301 • Letter: H

Question

Hello Cheggsperts! Couple of Java related questions.
AGAIN, PLEASE ANSWER THE FOLLOWING IN JAVA!!!!! Thank you!

Q1:

Let public static void reverse(Object [] arr) be a method that reverses an array and Person be a class like below:

public class Person {
String name= " ";
public void setName(String name) { this.name = name; } ;
public String getName( ) { return name; } ;
}

The code BELOW is in the main. Draw a box and arrow diagram showing only the final state (after this code is executed) of the people and names array variables and the array elements.

Finally, what is the output of the code?????

Code:

int len = 3;
Person[] people = new Person[len];
String[] names = new String[] {"Peter", "Paul", "Mary"};
Person blank = new Person();
for (int i=0; i<len; i++) {
people[i] = blank;
people[i].setName(names[i]);
}

reverse(people);
for (int i=0; i<len; i++) {
System.out.println(people[i].getName( ) );
}

Q2:

Please write a recursive function void reverse(int [] arr) that reverses an array. e.g. reverse([1,2,3,4]) would change the given array to [4,3,2,1].

What is the stack depth of the recursive function?????

(Hint: write a helper function that does the work).

Explanation / Answer

Answer for Question 1:

Output is Mary Mary Mary

Explanation: As of all three String indexes are filling with Mary


Answer for Question 2:

void reverseArray(int[] x){
   reverse(x, 0, x.length -1);
}

void reverse(int[] x, int i, int j){
    if(i<j){//Swap
       int tmp = x[i];
       x[i] = x[j];
       x[j] = tmp;
       reverse(x, ++i, --j);//Recursive
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote