dcsimg
Swapping values in Java from objects
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   P_T
Posted On:   Wednesday, June 6, 2001 12:56 PM

I have set up a class called Coord with two members : int x and int y. In main(), I have created two objects of type Coord and assigned them values.
How do I write a program, which swaps the values, i.e. x in one object is changed so that it contains the value of x in the other object, the same with y. & then display x and y for each object before and after the swap. Please advise!!!!

Re: Swapping values in Java from objects

Posted By:   Thanigai_Venkatesan  
Posted On:   Thursday, June 7, 2001 07:00 AM


Hi P T

The below code does the swapping, which uses the simple swapping logic, i don't think Java provides
any API for that. Let me know, what you asked is this or not.





Coord coord1 = new Coord(10,20); // Assume you have constructor which takes X, Y, here X = 10 and Y = 20
Coord coord2 = new Coord(30,40);

Coord temp = new Coord();//Constructor with no parameters.

temp.X = coord1.X;
temp.Y = coord1.y;

coord1.X = coord2.X;
coord1.Y = coord2.Y;

coord2.X = temp.X;
coord2.Y = temp.Y;






Cheers

About | Sitemap | Contact