Skip to content

Instantly share code, notes, and snippets.

@FaAway
Created March 19, 2016 12:56
Show Gist options
  • Save FaAway/3d92ac83f9f285dba0aa to your computer and use it in GitHub Desktop.
Save FaAway/3d92ac83f9f285dba0aa to your computer and use it in GitHub Desktop.
javarush level30.lesson08.home01
package com.javarush.test.level30.lesson08.home01;
public class Pair {
private int x;
private int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return String.format("x=%d, y=%d", x, y);
}
public void swap() {
x = x ^ y;
y = x ^ y;
x = x ^ y;
}
}
package com.javarush.test.level30.lesson08.home01;
/* Swap по-новому
В классе Pair реализуйте метод swap, который должен для x, y менять местами их значения.
Можно использовать только операции 1)исключающее или, 2) присваивание
Не оставляйте комментарии, не меняйте остальной код
*/
public class Solution {
public static void main(String[] args) {
/* expected output
x=4, y=5
x=5, y=4
*/
Pair pair = new Pair(4, 5);
System.out.println(pair);
pair.swap();
System.out.println(pair);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment