Tuesday, August 18, 2020

Java Program to Perform Set Operations

Set in java-

       The set interface is  present in the java.util  package. It extends the Collection interface.

       It is an unordered collection of objects in which duplicate values are not allowed.

        It is an interface which can implements the mathematical set.

       This interface contains the methods inherited from the Collection interface and adds a feature to restricts the insertion of the duplicate elements.

       The set interface allows the users to perform the basic mathematical set operations as following.

        Intersection: This operation returns all the common elements from the given two sets.

       Union: This operation adds all the elements in one set with the other. 

       Difference: This operation removes all the values present in one set from the other set.

        --------------------------------------------------Program------------------------------------------------------

/**
 *
 * @author Shikha
 */
import java.util.*; 
public class SetExample 
public static void main(String args[]) 
{       int ans=0;
                char ch='n';
Set<String> a = new HashSet<String>(); 
                a.add("Mumbai");
                a.add("Delhi");
                a.add("Chennai");
                a.add("Calcutta");
                a.add("Bengaluru");    
                a.add("Hyderabad");
                Set<String> b = new HashSet<String>(); 
                b.add("Mumbai");
                b.add("Delhi");
                b.add("Chennai");
                b.add("Calcutta");
                b.add("Bengaluru");  
b.add("Pune");  
                b.add("Indore");  
               
                          
               Scanner sc=new Scanner(System.in);
               //System.out.print(" 1:Union\t 2:Insertion\t 3:Difference\t 4:Stop");
                do{
                System.out.println(" 1:Union\t 2:Insertion\t 3:Difference\t 4:Stop");    
                ans=sc.nextInt();   
                    
                switch(ans)
                {
                
                    case 1:
                    Set<String> union = new HashSet<String>(a); 
                    union.addAll(b); 
                    System.out.println("Union of the two Set"); 
                    System.out.println(union);
                    break;
                    
                    case 2:
                    Set<String> intersection = new HashSet<String>(a); 
                    intersection.retainAll(b); 
                    System.out.println("Intersection of the two Set"); 
                    System.out.println(intersection);       
                    break;
                    
                    
                    case 3:
                    Set<String> difference = new HashSet<String>(a); 
                    difference.removeAll(b); 
                    System.out.println("Difference of the two Set"); 
                    System.out.println(difference); 
                    break;
                    
                    default:
                    System.out.println("Wrong Choice");   
                    
                            
                }
                
                System.out.println("Do you want to continue Y/N");
                ch=sc.next().charAt(0); 
                }while((ch=='y')||(ch=='Y'));

------------------------------------------------------------------------------------------------------------------------
OUTPUT
 1:Union 2:Insertion 3:Difference 4:Stop
1
Union of the two Set
[Delhi, Calcutta, Chennai, Bengaluru, Pune, Indore, Mumbai, Hyderabad]
Do you want to continue Y/N
y
 1:Union 2:Insertion 3:Difference 4:Stop
2
Intersection of the two Set
[Delhi, Calcutta, Chennai, Bengaluru, Mumbai]
Do you want to continue Y/N
y
 1:Union 2:Insertion 3:Difference 4:Stop
3
Difference of the two Set
[Hyderabad]
Do you want to continue Y/N
n

Sunday, August 9, 2020

Java Program using Array to implement CRUD-Create,Read ,Update and Delete Operations.

import java.util.*;

public class Main

{

static Scanner sc = new Scanner(System.in);

public static void main(String[] args)

{

int[] arr = new int[20];

int ans,len;

len = create(arr);

do

{

System.out.println("\n\nSelect \n1.Insert\t2.Delete\t3.Search\t4.Update\t5.Display\t6.Stop\n");

ans=sc.nextInt();

switch (ans)

{

case 1:

System.out.println("Enter no to insert");

int num = sc.nextInt();

arr[len]=num;

len++;

break;

case 2:

System.out.println("Enter Element TO Delete");

int del = sc.nextInt();

int D = search(arr, len, del);

if(D != -1)

{for (int i = D; i < len-1; i++)

arr[i] = arr[i+1];

len--;

System.out.println("Delete Succesful");

}

else

System.out.println("Element not found");

break;

case 3:

System.out.println("Enter Element TO be searched");

int ele = sc.nextInt();

int s = search(arr, len, ele);

if(s != -1)

System.out.println("Element "+ele+" present at "+s);

else

System.out.println("Element not found");

break;

case 4:

System.out.println("Enter Element TO be Updated");

int up1 = sc.nextInt();

int U = search(arr, len, up1);

if(U != -1)

{

System.out.println("Enter Replacing element");

int up2=sc.nextInt();

arr[U]=up2;

System.out.println("Update Successful");

}

else

System.out.println("Element not found");

break;

case 5:

display(arr,len);

break;

case 6:

System.out.println("Thank You!");

break;

default:

System.out.println("Wrong chice");

break;

}

} while (ans != 6);

}

public static int create(int arr[])

{

System.out.println("Enter the length of string");

int n = sc.nextInt();

System.out.println("Enter "+ n +" numbers");

for(int i =0 ; i<n ;i++)

arr[i]=sc.nextInt();

return n;

}

public static void display(int arr[],int len)

{

System.out.println("Array elements are");

for (int i = 0; i <len; i++)

System.out.print(arr[i]+"\t");

System.out.println("");

}

public static int search(int arr[],int len,int ele)

{

for (int i = 0; i < len; i++)

if(arr[i] == ele)

return i;

return -1;

}

}

************************************Output**************************************

********************************************************************************

Enter the length of string

4

Enter 4 numbers

12

13

14

15



Select 

1.Insert 2.Delete 3.Search 4.Update 5.Display 6.Stop


1

Enter no to insert

11



Select 

1.Insert 2.Delete 3.Search 4.Update 5.Display 6.Stop


5

Array elements are

12 13 14 15 11



Select 

1.Insert 2.Delete 3.Search 4.Update 5.Display 6.Stop


2

Enter Element TO Delete

11

Delete Succesful



Select 

1.Insert 2.Delete 3.Search 4.Update 5.Display 6.Stop


5

Array elements are

12 13 14 15



Select 

1.Insert 2.Delete 3.Search 4.Update 5.Display 6.Stop


4

Enter Element TO be Updated

14

Enter Replacing element

11

Update Successful



Select 

1.Insert 2.Delete 3.Search 4.Update 5.Display 6.Stop


5

Array elements are

12 13 11 15



Select 

1.Insert 2.Delete 3.Search 4.Update 5.Display 6.Stop


Java Program to Perform Set Operations

Set in java- •        The set interface is   pres ent in the java.util   package. It extends the Collection interface. •        It  is an ...