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

Java 2 questions: 1. Trace the execution of the following method by listing what

ID: 3589619 • Letter: J

Question

Java 2 questions:

1.

Trace the execution of the following method by listing what is printed when the method is invoked with the following parameters:

array = [12, 5, 6, 2, 3, 4, 7]

a=2

b=5

void recMethod(int[] array, int a, int b) {

if(b > a) {

print the array

double the value in the array at position b

recMethod(array, a, b-1)

print the array

} else {

print "done"

}

}

2.

Trace the execution of the following method by listing what is printed when the method is invoked with the first node in the following chain: 4 -> 6 -> 5 -> 3 -> 2

void recMethod(Node firstNode) {

print the chain of nodes headed by firstNode

if(firstNode.next!=null) {

double the data in firstNode

recMethod(firstNode.next.next);

} else {

print "done"

}

print the chain of nodes headed by firstNode

}

Explanation / Answer

1.

import java.util.Arrays;

public class TraceExecution1 {

void recMethod(int[] array, int a, int b) {

if (b > a) {

System.out.println(Arrays.toString(array)); // Printing array

array[b] = array[b] * 2; // double the value in the array at

// position b

recMethod(array, a, b - 1);

System.out.println(Arrays.toString(array)); // Printing array

} else {

System.out.println("done");

}

}

public static void main(String[] args) {

int[] array = { 12, 5, 6, 2, 3, 4, 7 };

int a = 2;

int b = 5;

TraceExecution1 execution1 = new TraceExecution1();

execution1.recMethod(array, a, b);

}

}

OUTPUT:

[12, 5, 6, 2, 3, 4, 7]
[12, 5, 6, 2, 3, 8, 7]
[12, 5, 6, 2, 6, 8, 7]
done
[12, 5, 6, 4, 6, 8, 7]
[12, 5, 6, 4, 6, 8, 7]
[12, 5, 6, 4, 6, 8, 7]

2.  

public class TraceExecution2 {

void recMethod(Node<Integer> firstNode) {

Node<Integer> iterator = firstNode;

while (iterator != null) {

System.out.print(iterator.value + " ");

iterator = iterator.getNext();

}

System.out.println();

if (firstNode.next != null) {

firstNode.value = firstNode.value * 2;

recMethod(firstNode.next.next);

} else {

System.out.println("done");

}

Node<Integer> iterator2 = firstNode;

while (iterator2 != null) {

System.out.print(iterator2.value + " ");

iterator2 = iterator2.getNext();

}

System.out.println();

}

public static void main(String[] args) {

Node<Integer> head = new Node<Integer>(4, null);

Node<Integer> second = new Node<Integer>(6, head);

Node<Integer> third = new Node<Integer>(5, second);

Node<Integer> fourth = new Node<Integer>(3, third);

new Node<Integer>(2, fourth);

TraceExecution2 execution2 = new TraceExecution2();

execution2.recMethod(head);

}

}

class Node<T> {

T value;

Node<T> next;

public Node(T value, Node<T> currentNode) {

// TODO Auto-generated constructor stub

this.value = value;

if (currentNode != null) {

currentNode.next = this;

}

}

public T getValue() {

return value;

}

public void setValue(T value) {

this.value = value;

}

public Node<T> getNext() {

return next;

}

public void setNext(Node<T> next) {

this.next = next;

}

}

OUTPUT:

4 6 5 3 2
5 3 2
2
done
2
10 3 2
8 6 10 3 2

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