Program which will interpret as coordinates on the number axis of end points of two intervals

Viewed 74

How I can write the program with bitwise operators instead of if statements, which will interpret as coordinates on the number axis of end points of two intervals: A = [a1, a2] and B = [b1, b2]. The program reads from the user one number (say, x) of type int and prints whether it is true that

• x ∈ A
• x ∈ B
• x ∈ A \ B
• x ∈ B \ A
• x ∈ A ∩ B
• x ∈ A ∪ B

For example, if the defined intervals are A = [2, 4] and B = [1, 6] and the number read is x = 5, the program should print something like:

Interval A = [2, 4]
Interval B = [1, 6]
Enter x 5
x in A: false
x in B: true
x in A\B: false
x in B\A: true
x in intersection of A and B: false
x in union of A and B: true

So now this is what I have


import org.w3c.dom.ranges.Range;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("input a1");
        int a1 = sc.nextInt();
        System.out.println("input a2");
        int a2 = sc.nextInt();
        System.out.println("input b1");
        int b1 = sc.nextInt();
        System.out.println("input b2");
        int b2 = sc.nextInt();
        int a =a1&a2;
        System.out.println("Interval A = " +a);
        int b = b1 & b2;
        System.out.println("Interval B = " + b);
        System.out.println("input x");
        int x = sc.nextInt();
        System.out.println("Interval A = [" + a1 + "," + a2 + "]");
        System.out.println("Interval B = [" + b1 + "," + b2 + "]");
        System.out.println("Enter x " + x);
        System.out.println("x in A: ");
        System.out.println("x in B: ");
        System.out.println("x in A/B: ");
        System.out.println("x in B/A: ");
        System.out.println("x in intersection of A and B: ");
        System.out.println("x is union of A and B: ");
        System.out.println("x in symm. diff. of A and B: ");
        
    }
}
1 Answers

You can write this (just use Array in Java):

import java.util.*;

public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("input a1: ");
    int a1 = sc.nextInt();
    System.out.print("input a2: ");
    int a2 = sc.nextInt();
    System.out.print("input b1: ");
    int b1 = sc.nextInt();
    System.out.print("input b2: ");
    int b2 = sc.nextInt();
    System.out.print("input x: ");
    int x = sc.nextInt();
    
    int arr1[] = new int [a2-a1+1];
    int arr2[] = new int [b2-b1+1];
    
    for (int i = 0, j = a1; j <= a2; i++, j++) {
        arr1[i] = j;
    }
    for (int i = 0, j = b1; j <= b2; i++, j++) {
        arr2[i] = j;
    }
    
    System.out.println("Interval A: [" + a1 + ", " + a2 + "]");
    System.out.println("Interval B: [" + b1 + ", " + b2 + "]");
    System.out.println("Enter x: " + x);
    System.out.println("x in A: " + Exist(arr1, x));
    System.out.println("x in B: " + Exist(arr2, x));
    System.out.println("x in A/B: " + Relative_Complement(arr1, arr2, x));
    System.out.println("x in B/A: " + Relative_Complement(arr2, arr1, x));
    System.out.println("x in intersection of A and B: " + Intersection(arr1,arr2, x));
    System.out.println("x is union of A and B: " + Union(arr1, arr2, x));
}

static boolean Exist(int arr[], int x){
    for (int i = 0; i < arr.length; i++) {
        if(x == arr[i])
            return true;
    }
    return false;
}

static boolean Relative_Complement(int arr1[], int arr2[], int x){
    int arr3[] = new int [arr1.length + arr2.length + 1];
    int arr4[] = new int [arr1.length + arr2.length + 1];
    
    for (int k = 0, i = 0; i < arr2.length; i++){
        for (int j = 0; j < arr1.length; j++) {
            if(arr2[i] == arr1[j]){
                arr3[k++] = arr2[i];
            }
        }
    }
    
    for (int p = 0, j = 0; j < arr1.length; j++) {
        if(!Exist(arr3, arr1[j])){
            arr4[p++] = arr1[j];
        }
   }
    
    return Exist(arr4, x);
}

static boolean Union(int arr1[], int arr2[], int x){
    int arr3[] = new int [arr1.length + arr2.length];
    int k = 0;
    for (int i = 0; i < arr1.length; i++, k++)
        arr3[k] = arr1[i];
    
    for (int i = 0; i < arr2.length; i++, k++)
        arr3[k] = arr2[i];

    return Exist(arr3, x);
}

static boolean Intersection(int arr1[], int arr2[], int x){
    int arr3[] = new int [arr1.length + arr2.length];

    for (int i = 0; i < arr2.length; i++){
        for (int j = 0; j < arr1.length; j++) {
            if(arr2[i] == arr1[j]){
                arr3[i] = arr2[i];
            }
        }
    }
    
    return Exist(arr3, x);
}
}

Notice: According to your question, a1 < a2 and b1 < b2.

Also, if you read HashSet you can solve this problem easier.

Related