Advertisement
HasteBin0

Java Integer Swap Exercise

Jun 26th, 2017
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. package swap2randomnumbers;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. final class Pair {
  7.  
  8.     private Integer a, b;
  9.     private final Random random;
  10.  
  11.     public Pair(Random random) {
  12.         this.random = random;
  13.         this.gen();
  14.     }
  15.  
  16.     public void gen() {
  17.         this.genA();
  18.         this.genB();
  19.     }
  20.  
  21.     public Integer getA() {
  22.         return a;
  23.     }
  24.  
  25.     public void setA(final Integer a) {
  26.         this.a = a;
  27.     }
  28.  
  29.     public void genA() {
  30.         this.a = this.random.nextInt();
  31.     }
  32.  
  33.     public Integer getB() {
  34.         return b;
  35.     }
  36.  
  37.     public void genB() {
  38.         this.b = this.random.nextInt();
  39.     }
  40.  
  41.     public void setB(final Integer b) {
  42.         this.b = b;
  43.     }
  44. }
  45.  
  46. /**
  47.  *
  48.  * @author Aidan
  49.  */
  50. public class Swap2RandomNumbers {
  51.  
  52.     static {
  53.         renew_input();
  54.         renew_random();
  55.     }
  56.  
  57.     private static Random random;
  58.     private static Scanner input;
  59.  
  60.     private static void renew_input() {
  61.         input = new Scanner(System.in);
  62.     }
  63.  
  64.     private static void renew_random() {
  65.         random = new Random();
  66.     }
  67.  
  68.     public static void main(String[] args) {
  69.         Pair obj = new Pair(random);
  70.         output(obj);
  71.         //
  72.         swap1(obj);
  73.         output(obj);
  74.         //
  75.         obj.gen();
  76.         output(obj);
  77.         swap2(obj);
  78.         output(obj);
  79.         //
  80.         obj.gen();
  81.         output(obj);
  82.         swap3(obj);
  83.         output(obj);
  84.     }
  85.  
  86.     private static void swap1(Pair x) {
  87.         Integer tmp = x.getA();
  88.         x.setA(x.getB());
  89.         x.setB(tmp);
  90.     }
  91.  
  92.     private static void swap2(Pair x) {
  93.         /*
  94.          a = a + b;
  95.          b = a - b;
  96.          a = a - b;
  97.          */
  98.         x.setA(x.getA() + x.getB());
  99.         x.setB(x.getA() - x.getB());
  100.         x.setA(x.getA() - x.getB());
  101.     }
  102.  
  103.     private static void swap3(Pair x) {
  104.         /*
  105.          a = a ^ b;
  106.          b = a ^ b;
  107.          a = a ^ b;
  108.          */
  109.         x.setA(x.getA() ^ x.getB());
  110.         x.setB(x.getA() ^ x.getB());
  111.         x.setA(x.getA() ^ x.getB());
  112.     }
  113.  
  114.     private static void output(Pair obj) {
  115.         System.out.println("The pair is " + obj.getA() + " and " + obj.getB() + '.');
  116.         System.out.flush();
  117.     }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement