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

Implement a passing by reference in the function swap. For this purpose define t

ID: 3604783 • Letter: I

Question

Implement a passing by reference in the function swap. For this purpose define the class IntWrapper as a wrapper of an integer. Define the class IntWrapper as an inner class on the class Main.

This is a program using java, it will not compile and I keep getting errors


//Class has values for 2 integers and swaps their values by reference

public class Main {

static class IntegerWrapper{
int numb;
// constructor
IntegerWrapper(int numb) {this.numb = numb;}
}
public static void swap( IntegerWrapper x1, Integer Wrapper y1){
int temp = x1.numb;
x1.numb = y1.numb;
y1.numb = temp;

}

public static void main(String[] args){
int num1 = 8;
int num2 = 2 ;
IntegerWrapper x1 = new IntegerWrapper(num1);
IntegerWrapper y1 = new IntegerWrapper(num2);
System.out.println("Before the Swap" + "The numbers are " + x1.numb + " and " + y1.numb) ;

swap(x1,y1);

System.out.println("After the Swap" + "The numbers are " + x1.numb + " and " + y1.numb) ;

}

Explanation / Answer

//Class has values for 2 integers and swaps their values by reference
public class Main {
static class IntegerWrapper{
int numb;
// constructor
IntegerWrapper(int numb) {this.numb = numb;}
}
//there is space between Integer and Wrapper for y1.
public static void swap( IntegerWrapper x1, IntegerWrapper y1){
int temp = x1.numb;
x1.numb = y1.numb;
y1.numb = temp;
}
public static void main(String[] args){
int num1 = 8;
int num2 = 2 ;
IntegerWrapper x1 = new IntegerWrapper(num1);
IntegerWrapper y1 = new IntegerWrapper(num2);
System.out.println("Before the Swap" + "The numbers are " + x1.numb + " and " + y1.numb) ;
swap(x1,y1);
System.out.println("After the Swap" + "The numbers are " + x1.numb + " and " + y1.numb) ;
}
}

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