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: 3692172 • 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

public static void reverse(Object[] arr) {

        List<Object> list = Arrays.asList(arr);

        Collections.reverse(list);

        System.out.println(list.toArray());

    }

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++) {

//array of person objects equals to another person object

people[i] = blank;

//so all now blank object

people[i].setName(names[i]);

}

reverse(people);

for (int i=0; i<len; i++) {

System.out.println(people[i].getName( ) );

}

o/p

Mary, Paul, Peter

Q2:

void reverseArray(int[] arr){

   reverse(arr, 0, arr.length -1);

}

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