In: Computer Science
JAVA -The next three questions use the following class:
class Identification {
private int idNum;
public Identification() {
this(0);
}
public Identification(int startingIdNum) {
idNum = startingIdNum;
}
public int getIdNum() { return idNum; }
public void setIdNum(int idNum) {
this.idNum = idNum;
}
}
Here is one program using the above class:
public class Main {
public static void main(String[] args) {
Identification i1 = new
Identification();
Identification i2 = new
Identification(7);
swap(i1.getIdNum(),
i2.getIdNum());
System.out.print(i1.getIdNum() + "
" +
i2.getIdNum());
}
public static void swap(int c1, int c2) {
int temp = c1;
c1 = c2;
c2 = temp;
}
}
If you run the program, you’ll see that the output is:
0 7
Explain using words and/or pictures why that is the
output.
Here is a second program using the above class:
public class Main {
public static void main(String[] args) {
Identification i1 = new
Identification();
Identification i2 = new
Identification(7);
swap(i1, i2);
System.out.print(i1.getIdNum() + "
" +
i2.getIdNum());
}
public static void swap(Identification i1,
Identification i2) {
int temp = i1.getIdNum();
i1.setIdNum(i2.getIdNum());
i2.setIdNum(temp);
}
}
If you run the program, you’ll see that the output is:
7 0
Explain using words and/or pictures why that is the
output.
Here is a third program using the above class:
public class Main {
public static void main(String[] args) {
Identification i1 = new
Identification();
Identification i2 = new
Identification(7);
swap(i1, i2);
System.out.print(i1.getIdNum() + "
" +
i2.getIdNum());
}
public static void swap(Identification i1,
Identification i2) {
Identification temp = i1;
i1 = i2;
i2 = temp;
}
}
If you run the program, you’ll see that the output is:
0 7
Explain using words and/or pictures why that is the
output.
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
Here is one program using the above class:
public class Main {
public static void main(String[] args) {
Identification i1 = new
Identification();
Identification i2 = new
Identification(7);
swap(i1.getIdNum(),
i2.getIdNum());
System.out.print(i1.getIdNum() + "
" +
i2.getIdNum());
}
public static void swap(int c1, int c2) {
int temp = c1;
c1 = c2;
c2 = temp;
}
}
Explanation:
In problem 1, inside the swap method their is no statement to set the identation number, so basically here swap method is of no use, as the values outside the swap function remains same.
So, since their is no statement of setIdNum inside swap function, so the values remains same after the swap function executed that's why the output comes 0 7
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Here is a second program using the above class:
public class Main {
public static void main(String[] args) {
Identification i1 = new
Identification();
Identification i2 = new
Identification(7);
swap(i1, i2);
System.out.print(i1.getIdNum() + "
" +
i2.getIdNum());
}
public static void swap(Identification i1,
Identification i2) {
int temp = i1.getIdNum();
i1.setIdNum(i2.getIdNum());
i2.setIdNum(temp);
}
}
Explanation:
In this swap method, the values are getting exchanged and get updated using the setIdNum(), so after the swap function ends, the values are updated via setUdNum in swap function.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Here is a third program using the above class:
public class Main {
public static void main(String[] args) {
Identification i1 = new
Identification();
Identification i2 = new
Identification(7);
swap(i1, i2);
System.out.print(i1.getIdNum() + "
" +
i2.getIdNum());
}
public static void swap(Identification i1,
Identification i2) {
Identification temp = i1;
i1 = i2;
i2 = temp;
}
}
Explanation:
In this also inside swap function, the swapping is done locally,i.e, only restricted to the swap function, outside swap function, the values would be same as before or earlier, that's why after the swap function , the values printed are same ,i.e, 0 7