class ItemPair {
private int item1;
private int item2;

// Constructor to initialize the items
public ItemPair(int item1, int item2) {
this.item1 = item1;
this.item2 = item2;
}

// Method to display the items
public void display() {
System.out.println(“Item 1: ” + item1 + “, Item 2: ” + item2);
}

// Method to swap items and return the modified object
public ItemPair swapAndReturn() {
// Swapping items
int temp = item1;
item1 = item2;
item2 = temp;

// Returning the modified object
return this;
}
}

public class SwapItemsDemo {
public static void main(String[] args) {
// Creating an instance of ItemPair
ItemPair pair = new ItemPair(10, 20);

// Display initial items
System.out.println(“Before swapping:”);
pair.display();

// Swap items and get the modified object
ItemPair modifiedPair = pair.swapAndReturn();

// Display items after swapping
System.out.println(“After swapping:”);
modifiedPair.display();
}
}


Leave a Reply

Your email address will not be published. Required fields are marked *