C Programming III 10 marks Complete the swap and interact me

C: Programming III 10 marks] Complete the swap and interact methods in the provided Thing class.

Solution

Here is the code for you:

public class Thing
{
public int data;
public Box box;
  
public Thing(int d, Box b)
{
data = d;
box = b;
}
  
/*This method swaps the data values for this and that.
*Example: t1 = new Thing(12, null)
*       t2 = new Thing(44, null)
*       t1.swap(t2);
*       t1.data -> 44, t2.data -> 12
*/
public void swap(Thing that)
{
Thing temp;
temp.data = this.data;
this.data = that.data;
that.data = temp.data;
temp.box = this.box;
this.box = that.box;
that.box = temp.box;
}
  
/* This method swaps the boxes of this and that if
* this\'s box is less than other\'s box. (less than is defined in Problem A)
* Otherwise, it swaps other\'s data with other\'s box\'s data.
*/
public void interact(Thing that)
{
if(box < that.box)
{
Box temp = box;
box = that.box;
that.box = temp;
}
else
{
int temp = that.data;
that.data = that.box.data;
that.box.data = temp;
}
}
}

 C: Programming III 10 marks] Complete the swap and interact methods in the provided Thing class. SolutionHere is the code for you: public class Thing { public

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site