Hello Cheggsperts! Couple of Java related questions. AGAIN, PLEASE ANSWER THE FO
ID: 3692262 • 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
The given below are the methods of reverse the objects WITH COMMENTS:
//REVERSE FUNCTION//
public static void reverse(Object [] arr){
for (int x=0;y=arr.lenght-1;x<y;x++;y--){
object.temp=arr[x];
arr[x]=arr[y];
arr[y]=temp;
}
}
The given below are the methods of reverse the number with comments to make easy understand the working of the code;
public class NumberReverse{
//MAIN FUNCTION//
public static void main (String[] args) {
int[] list = {4, 5, 6, 7};
if (Reverse(list) != 0)
System.out.println("ERROR!");
DEPTH(0);
}
//FUNCTION FOR REVERSE//
public static int Reverse(int[] list) {
System.out.println(" The list in reverse order is:");
for (int i = list.length-1; i >= 0; i--) {
System.out.print(list[i] + " ");
}
return 0;
}
// DEPTH FUNCTION//
private static void DEPTH(int i) {
try {
DEPTH(i+1);
} catch (StackOverflowError e) {
System.out.print("Recursion depth on this system is " + i + ".");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.