Tuesday, 19 March 2019

Implementation of Data Structure using Java

Implementation of Data Structure using Java

import java.util.*;

public class DSJava
{
 public static void main(String[] args) throws Exception
 {
  Scanner sc=new Scanner(System.in);
  int choice;

  PriorityQueue<Integer> q1=new PriorityQueue<>();
  Stack<Integer> s1=new Stack<>();
  LinkedList<Integer> l1=new LinkedList<>();
  ArrayList<Integer> arrayList=new ArrayList<>();
  HashMap<Integer, Integer> hs=new HashMap<>();
 
  do
  {
   System.out.println("\n====MENU====\n1.Queue\n2.Stack\n3.Linked List\n4.ArraList\n5.Map\n6.Exit\nENter your choice:");
   choice=sc.nextInt();
   switch(choice)
   {
   case 1:
    do
    {
     System.out.println("\n====Queue=======\n");
     System.out.println("1. Enqueue\n2.Dequeue\n3.Size\n4.Search\n0.Exit");
     choice=sc.nextInt();
     switch(choice)
     {
     case 1:
      System.out.println("\nEnter Number: ");
      q1.add(new Integer(sc.nextInt()));
      System.out.println("Added :"+q1.peek());
      break;
     case 2:
      System.out.println("\nRemoved element:=>"+q1.remove());
      break;
     case 3:
      System.out.println("\nSize= "+q1.size());
      break;
     case 4:
      System.out.println("\nENter Element to Search: ");
      if(q1.contains(sc.nextInt()))
      {
       System.out.println("\nElement is present.");
      }
      else
      {
       System.out.println("\nElement is absent.");
      }
      break;
     case 0:
      break;
     }
    }
    while(choice!=0);
    break;
   case 2:
    do
    {
     System.out.println("\n=======Stack=======");
     System.out.println("\n1.Push\n2.Pop\n3.Top\n0.Exit;");
     choice=sc.nextInt();
     switch(choice)
     {
     case 1:
      System.out.println("\nENter Number: ");
      s1.push(new Integer(sc.nextInt()));
      break;
     case 2:
      System.out.println("\nPopped elemet:=> "+s1.pop());
      break;
     case 3:
      System.out.println("\nStack top: "+s1.peek());
      break;
     case 0:
         break;
     }
    }
    while(choice!=0);
    break;
   case 3:
    do
    {
     System.out.println("\n====Linked LIST=====\n1.ADD Element\n2.Remove Element\n3.First Element\n4.Last Element\n0.Exit");
     choice=sc.nextInt();
     switch(choice)
     {
     case 1:
      System.out.println("\nENter Number: ");
      l1.add(new Integer(sc.nextInt()));
      break;
     case 2:
      System.out.println("\nEnter element to remove: ");

      System.out.println("\nRemoved element:"+l1.remove(sc.nextInt()));
      break;
     case 3:
      System.out.println("\nFirst Element: "+l1.getFirst());
      break;
     case 4:
      System.out.println("\nLast Element: "+l1.getLast());
      break;
      case 0:
         break;
     }
    }while(choice!=0);
    break;
   case 4:
    do
    {
     System.out.println("\n====ARRAY LIST ========");
     System.out.println("\n1.Add element\n2.Remove element\n3.Remove element by index\n4.Size\n0.Exit");
     choice=sc.nextInt();
     switch(choice)
     {
     case 1:
      System.out.println("\nEnter Element: ");
      arrayList.add(new Integer(sc.nextInt()));
      break;
     case 2:
      System.out.println("\nEnter element to remove: ");
      arrayList.remove(new Integer(sc.nextInt()));
      System.out.println("\nElement Removed.");
      break;
     case 3:
      System.out.println("\nEnter Index to remove element");
      int index=sc.nextInt();
      System.out.println("Removed element  at index 0 from arrayList: "+arrayList.remove(index));
      break;
     case 4:
      System.out.println("\nSize:=> "+arrayList.size());
      break;
     case 0:
         break;
     }
    }while(choice!=0);
    break;

   case 5:
    do
    {
     System.out.println("\n===== Hash Map=====");
     System.out.println("\n1.Insert\n2.Remove by Key\n3.Size\n0.Exit\nEnter Choice:");
     choice=sc.nextInt();

     switch(choice)
     {
     case 1:
      System.out.println("\nENter Key");
      int key=sc.nextInt();
      System.out.println("\nENter Value: ");
      int value=sc.nextInt();
      hs.put(key, value);
      System.out.println("\nRecord Inserted");
      break;
     case 2:
      System.out.println("\nENter Key to delete associated value:");
      //sc.next();
      int key1=sc.nextInt();
      System.out.println("\nRemoved element (roll):"+hs.remove(key1));
      break;
     case 3:
      System.out.println("\nSize: "+hs.size());
      break;
     case 0:
      break;
     }
    }while(choice!=0);
    break;
   default:
    System.out.println("\nWrong Choice");
   }
  }while(choice!=6);
 }
}



Output:


====MENU====
1.Queue
2.Stack
3.Linked List
4.ArraList
5.Map
6.Exit
ENter your choice:
1

====Queue=======

1. Enqueue
2.Dequeue
3.Size
4.Search
0.Exit
1

Enter Number:
10
Added :10

====Queue=======

1. Enqueue
2.Dequeue
3.Size
4.Search
0.Exit
3

Size= 1

====Queue=======

1. Enqueue
2.Dequeue
3.Size
4.Search
0.Exit
0

====MENU====
1.Queue
2.Stack
3.Linked List
4.ArraList
5.Map
6.Exit
ENter your choice:
2

=======Stack=======

1.Push
2.Pop
3.Top
0.Exit;
1

ENter Number:
10

=======Stack=======

1.Push
2.Pop
3.Top
0.Exit;
2

Popped elemet:=> 10

=======Stack=======

1.Push
2.Pop
3.Top
0.Exit;
0

====MENU====
1.Queue
2.Stack
3.Linked List
4.ArraList
5.Map
6.Exit
ENter your choice:
3

====Linked LIST=====
1.ADD Element
2.Remove Element
3.First Element
4.Last Element
0.Exit
1

ENter Number:
10

====Linked LIST=====
1.ADD Element
2.Remove Element
3.First Element
4.Last Element
0.Exit
4

Last Element: 10

====Linked LIST=====
1.ADD Element
2.Remove Element
3.First Element
4.Last Element
0.Exit
0

====MENU====
1.Queue
2.Stack
3.Linked List
4.ArraList
5.Map
6.Exit
ENter your choice:
4

====ARRAY LIST ========

1.Add element
2.Remove element
3.Remove element by index
4.Size
0.Exit
1

Enter Element:
10

====ARRAY LIST ========

1.Add element
2.Remove element
3.Remove element by index
4.Size
0.Exit
3

Enter Index to remove element
0
Removed element  at index 0 from arrayList: 10

====ARRAY LIST ========

1.Add element
2.Remove element
3.Remove element by index
4.Size
0.Exit
0

====MENU====
1.Queue
2.Stack
3.Linked List
4.ArraList
5.Map
6.Exit
ENter your choice:
5

===== Hash Map=====

1.Insert
2.Remove by Key
3.Size
0.Exit
Enter Choice:
1

ENter Key
10

ENter Value:
1000

Record Inserted

===== Hash Map=====

1.Insert
2.Remove by Key
3.Size
0.Exit
Enter Choice:
3

Size: 1

===== Hash Map=====

1.Insert
2.Remove by Key
3.Size
0.Exit
Enter Choice:
0

====MENU====
1.Queue
2.Stack
3.Linked List
4.ArraList
5.Map
6.Exit
ENter your choice:
6


Java Program for Package and Interface


Java Program for Package and Interface


package P1;

public interface Shop
{
public void getdata();
public void display();
public int total();

}



package P2;

import P1.Shop;
import java.util.*;

public class Customer implements Shop {

    int banana, apple, grapes, papaya, mango;
    Scanner s = new Scanner(System.in);

    public void display() {
        System.out.println("\n Final Order of Customer:");
        System.out.println("\n\tItem    Quantity   Amount");
        System.out.println("\n\tBananas :" + banana + " kg" + "    Rs " + (banana * 50));
        System.out.println("\tApple   :" + apple + " kg" + "    Rs " + (apple * 200));
        System.out.println("\tGrapes  :" + grapes + " kg" + "    Rs " + (grapes * 100));
      

    }

    public void getdata() {

        System.out.println("\n\n Details for Customer:");
        System.out.println("\n\t Enter the number of bananas(in kg): ");
        banana = s.nextInt();
        System.out.println("\n\t Enter the number of apple(in kg): ");
        apple = s.nextInt();
        System.out.println("\n\t Enter the number of grapes(in kg): ");
        grapes = s.nextInt();
        

    }

    public int total() {
        int total;

        //System.out.println("\nThe total bill of the customer 2:");
        total = (banana * 50) + (apple * 200) + (grapes * 100) + (papaya * 60) + (mango * 120);
        System.out.println("\n Total bill of customer:Rs " + total);

        return total;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Customer obj1 = new Customer();
        Customer obj2 = new Customer();
        int price1, price2;
        obj1.getdata();
        obj1.display();
        price1 = obj1.total();
        obj2.getdata();
        obj2.display();
        price2 = obj2.total();

        if (price1 > price2) {
            System.out.println("\n\n\t Congragulation!!!!! Customer 1 own a gift hamper.");
            System.out.println("\n\t The bill of customer 1 is more. Bill amount:Rs " + price1);
        } else if (price1 < price2) {
            System.out.println("\n\n\t Congragulation!!!!! Customer 2 own a gift hamper.");
            System.out.println("\n\t The bill of customer 2 is more. Bill amount:Rs " + price2);
        } else {
            System.out.println("\n\t Both bills are equal:Rs " + price1);
        }

        System.out.println("\n\n\t\t\t Thank You!!!!!!!");

    }

}


Output


 Details for Customer:

Enter the number of bananas(in kg): 
1

Enter the number of apple(in kg): 
1

Enter the number of grapes(in kg): 
1

 Final Order of Customer:

Item    Quantity   Amount

Bananas :1 kg    Rs 50
Apple   :1 kg    Rs 200
Grapes  :1 kg    Rs 100

 Total bill of customer:Rs 350


 Details for Customer:

Enter the number of bananas(in kg): 
2

Enter the number of apple(in kg): 
1

Enter the number of grapes(in kg): 
1

 Final Order of Customer:

Item    Quantity   Amount

Bananas :2 kg    Rs 100
Apple   :1 kg    Rs 200
Grapes  :1 kg    Rs 100

 Total bill of customer:Rs 400


Congragulation!!!!! Customer 2 own a gift hamper.

The bill of customer 2 is more. Bill amount:Rs 400


Thank You!!!!!!!


Java Program for Heap Sort


 Java Program for Heap Sort



import java.util.*;

public class Heapdemo
{

    int left(int i) {
        return 2 * i + 1;
    }

    int right(int i) {
        return 2 * i + 2;
    }

    void minheapify(int arr[], int n, int i) {
        int l, r, smallest, temp;
        l = left(i);
        r = right(i);
        if (l < n && arr[l] < arr[i]) {
            smallest = l;
        } else {
            smallest = i;
        }
        if (r < n && arr[r] < arr[smallest]) {
            smallest = r;
        }

        if (smallest != i) {
            temp = arr[i];
            arr[i] = arr[smallest];
            arr[smallest] = temp;
            minheapify(arr, n, smallest);
        }
    }

    void heapSort(int arr[], int n) {
        int i, temp;

        for (i = n / 2 - 1; i >= 0; i--) {
            minheapify(arr, n, i);
        }

        for (i = n - 1; i >= 0; i--) {
            temp = arr[0];
            arr[0] = arr[i];
            arr[i] = temp;
            minheapify(arr, i, 0);
        }
    }

    public static void main(String[] args) {
        Heapdemo h = new Heapdemo();
        int arr[] = new int[20];
        int i = 0, n;
        Scanner s = new Scanner(System.in);

        System.out.println("Enter the size of array : ");
        n = s.nextInt();
        System.out.println("\nEnter the elements : ");
        for (i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }

        for (i = (n - 1) / 2; i >= 0; i--) {
            h.minheapify(arr, n, i);
        }

        System.out.println("\nArray after Minheap \n");

        for (i = 0; i < n; i++) {
            System.out.println(arr[i]);
        }

        h.heapSort(arr, n);

        System.out.println("\nArray after Heap sort \n");

        for (i = n - 1; i >= 0; i--) {
            System.out.println(arr[i]);
        }

    }

}

Output:

run:
Enter the size of array :
7

Enter the elements :
48
0
-1
82
10
2
100

Array after Minheap

-1
0
2
82
10
48
100

Array after Heap sort

-1
0
2
10
48
82
100



Tuesday, 5 March 2019

ESIOT: Unit 3 -Pillars of Embedded IoT and Physical Devices




Contents:

Horizontal, verticals and four pillars of IoT, M2M: The internet of devices, RFID: The internet of objects, WSN: The internet of transducer, SCADA: The internet of controllers, DCM: Device, Connect and Manage, Device: Things that talk, Connect: Pervasive Network, Mangae: To create business values. IoT Physical Devices and Endpoints: Basic building blocks of and IoT device, Exemplary device: Raspberry Pi, Raspberry Pi interfaces, Programming Raspberry Pi with Python, Beagle board and Other IoT Devices.


Video Link: https://youtu.be/p_2ybeY2Z1U




ADS: Unit 3-Hashing



Contents


Hash Table- Concepts-hash table, hash function, bucket, collision, probe, synonym, overflow,open hashing, closed hashing, perfect hash function, load density, full table, load factor, rehashing,issues in hashing, hash functions- properties of good hash function, division, multiplication,extraction, mid-square, folding and universal, Collision resolution strategies- open addressing and chaining, Hash table overflow- open addressing and chaining, extendible hashing.

Dictionary- Dictionary as ADT, ordered dictionaries.
Skip List- representation, searching and operations- insertion, removal.
           



Stack and Queue as ADT in C++

Stack as ADT The functions associated with stack are: empty()  – Returns whether the stack is empty size()  – Returns the size o...