Sunday, July 9, 2023

Library Management System JAVA


 Library Management System IN  JAVA

 package com.company;

import java.util.ArrayList;


/*

 Create a library management system which is capable of issuing books to the students.

 Book should have info like:

 1. Book name

 2. Book Author

 3. Issued to

 4. Issued on

 User should be able to add books, return issued books, issue books

 Assume that all the users are registered with their names in the central database

  */

class Book{

    public String name, author;


    public Book(String name, String author) {

        this.name = name;

        this.author = author;

    }


    @Override

    public String toString() {

        return "Book{" +

                "name='" + name + '\'' +

                ", author='" + author + '\'' +

                '}';

    }

}


class MyLibrary{

    public ArrayList<Book> books;

    public MyLibrary(ArrayList<Book> books) {

        this.books = books;

    }

    public void addBook(Book book){

        System.out.println("The book has been added to the library");

        this.books.add(book);

    }

    public void issueBook(Book book, String issued_to){

        System.out.println("The book has been issued from the library to " + issued_to);

        this.books.remove(book);

    }

    public void returnBook(Book b){

        System.out.println("The book has been returned");

        this.books.add(b);

    }


}

public class cwh_113_ex7sol {

    public static void main(String[] args) {

        // Exercise 7 Solution

        ArrayList<Book> bk = new ArrayList<>();

        Book b1 = new Book("Algorithms", "CLRS");

        bk.add(b1);


        Book b2 = new Book("Algorithms2", "CLRS2");

        bk.add(b2);


        Book b3 = new Book("Algorithms3", "CLRS3");

        bk.add(b3);


        Book b4 = new Book("Algorithms4", "CLRS4");

        bk.add(b4);

        MyLibrary l = new MyLibrary(bk);

        l.addBook(new Book("algo4", "myAuthor"));

        System.out.println(l.books);

        l.issueBook(b3, "Harry");

        System.out.println(l.books);


    }

}


Hashing in Java

 

Hashing in Java

Hashing is the technique to convert the range of key-value pairs to a range of indices. In hashing, we use hash functions to map keys to some values.

Example :

Let arr=[11,33,22,14]

hashIndex = (key %10)

Hashing Example


Collision: The hash function may map two key values to a single index. Such a situation is known as a collision.

Example : Let 1l=[22,42,64,71]

H(x) = keys%10

Hashing collision example

In the above image, you can see that the 22 and 44 are mapped to the index number 2. Therefore we need to avoid the collision. Following techniques are used to avoid collision in hashing :

  • Open addressing
  • Chaining

HashSet in Java

 

HashSet in Java

  • HashSet class uses a hash table for storing the elements.
  • It implements the set interface.
  • Duplicate values are not allowed.
  • Before storing any object, the hashset uses the hashCode() and equals() method to check any duplicate entry in the hash table.
  • Allows null value.
  • Best suited for search operations.

Constructors Of HashSet :

  1. HashSet(): This constructor is used to create a new empty HashSet that can store 16 elements and have a load factor of 0.75.
  2. HashSet(int initialCapacity): This constructor is used to create a new empty HashSet which has the capacity to store the specified number of elements and having a load factor of 0.75.
  3. HashSet(int initialCapacity, float loadFactor): This constructor is used to create a new empty HashSet with the capacity & load factor equal to specified integer and float value.
  4. HashSet(Collection<? extends E> c): This constructor is used to create a HashSet using the elements of collection c.

Performing Various Operations On HashSet :

  1. Inserting elements :
    • add() method is used to add elements in HashSet.
    • The insertion order of the elements does not remains preserved in HashSet.
    • All the duplicate elements are ignored because the set contains only unique values.
      Example :
      import java.util.*;
      public class CWH extends Thread{
          public static void main(String[] args) {
              HashSet<Integer> myHashSet = new HashSet<>(6, 0.5f);
              myHashSet.add(6);
              myHashSet.add(8);
              myHashSet.add(3);
              myHashSet.add(11);
              myHashSet.add(11); // This element will be ignored
      
              System.out.println(myHashSet);
      
          }
      }
      
Output :
[8, 3, 11, 6]
Removing elements from the HashSet : 
  • remove() method is used to delete the specified element from the HashSet.
  • This method does not throws any exception if the specified element is not present in the HashSet.
    Example :
    import java.util.*;
    public class CWH extends Thread{
        public static void main(String[] args) {
            HashSet<Integer> myHashSet = new HashSet<>(6, 0.5f);
            myHashSet.add(6);
            myHashSet.add(8);
            myHashSet.add(3);
            myHashSet.add(11);
            myHashSet.add(11); // This element will be ignored
    
            System.out.println("myHashSet before removing any element : " + myHashSet);
            myHashSet.remove(3); //deletes 3 from the hashset
            System.out.println("myHashSet after removing a element : " + myHashSet);
    
        }
    }
    
Output :
myHashSet before removing any element : [8, 3, 11, 6]
myHashSet after removing a element : [8, 11, 6]
Checking if the HashSet is empty or not :  
    • isEmpty() method is used to check if there is any object in the HashSet or not.
    • This method returns a boolean value.
      Example :
      import java.util.*;
      public class CWH extends Thread{
          public static void main(String[] args) {
              HashSet<Integer> myHashSet = new HashSet<>(6, 0.5f);
              myHashSet.add(6);
              myHashSet.add(8);
              myHashSet.add(3);
              myHashSet.add(11);
      
              HashSet<Integer> myHashSet1 = new HashSet<>();
      
              System.out.println(myHashSet.isEmpty());
              System.out.println(myHashSet1.isEmpty());
      
      
          }
      }
      
Output :
false
true
Removing all the elements from the HashSet : 
  • clear() method is used to remove all the elements from the HashSet at once.
    Example :
    import java.util.*;
    public class CWH extends Thread{
        public static void main(String[] args) {
            HashSet<Integer> myHashSet = new HashSet<>(6, 0.5f);
            myHashSet.add(16);
            myHashSet.add(33);
            myHashSet.add(78);
            myHashSet.add(19);
            myHashSet.add(29);
            myHashSet.add(10); 
    
            System.out.println("myHashSet before : " + myHashSet);
            myHashSet.clear(); //deletes all the elements from the hashset
            System.out.println("myHashSet after  : " + myHashSet);
            
        }
    }
    
Output :
myHashSet before : [16, 33, 19, 10, 29, 78]
myHashSet after  : []
Printing the size of the HashSet :
  • size() method is used to get the size of the HashSet.
    Example :
    import java.util.*;
    public class CWH extends Thread{
        public static void main(String[] args) {
            HashSet<Integer> myHashSet = new HashSet<>(6, 0.5f);
            myHashSet.add(16);
            myHashSet.add(33);
            myHashSet.add(78);
            myHashSet.add(19);
            myHashSet.add(29);
            myHashSet.add(10); 
    
            System.out.println("The size of myHashSet is : " + myHashSet.size());
    
        }
    }
    
Output :
The size of myHashSet is : 6

Code as described/written in the video :

package com.company;

import java.util.HashSet;

public class cwh_95_set {
    public static void main(String[] args) {
        HashSet<Integer> myHashSet = new HashSet<>(6, 0.5f);
        myHashSet.add(6);
        myHashSet.add(8);
        myHashSet.add(3);
        myHashSet.add(11);
        myHashSet.add(11);
        System.out.println(myHashSet);
    }
}

ArrayDeque in Java

 

ArrayDeque in Java

  • ArrayDeque = Resizable array + Deque interface.
  • ArrayDeque implements the Queue & Deque interface.
  • There are no capacity restrictions for ArrayDeque, and it provides us the facility to add or remove any element from both sides of the queue.
  • Also known as Array Double Ended Queue.
  • It is faster than Linked list and stack.

Constructors of ArrayDeque class :

  1. ArrayDeque(): Used to create an empty array deque that has the capacity to hold 16 elements.
  2. ArrayDeque(int numElements): Used to create an empty array deque that has the capacity to hold the specified number of elements.
  3. ArrayDeque(Collection<? extends E> c): Used to create an array deque containing all the elements of the specified collections.

Performing Various Operation On ArrayDeque() :

  1. Inserting an element :
    • Insertion at front : add(), offerFirst() and addFirst() methods are used to insert an element at front of an array deque.

      Example :
      import java.util.*;
      public class CWH extends Thread{
          public static void main(String[] args) {
      
              ArrayDeque<Integer> ad1 = new ArrayDeque<>();
              ad1.add(6);
              ad1.add(56);
              ad1.add(9);
              ad1.addFirst(5);
              ad1.offerFirst(10);
              System.out.println(ad1);
      
          }
      }
      
Output :
[10, 5, 6, 56, 9]
Insertion At End: addLast() and offerLast() methods are used to insert an element at the end of the array deque.
Example :
import java.util.*;
public class CWH extends Thread{
    public static void main(String[] args) {

        ArrayDeque<Integer> ad1 = new ArrayDeque<>();
        ad1.add(6);
        ad1.add(56);
        ad1.add(9);
        ad1.addLast(5);
        ad1.offerLast(10);
        
        System.out.println(ad1);

    }
}
Output :
[6, 56, 9, 5, 10]
Accessing an element :
  • Accessing an element from the head of the deque array: getFirst() & peekFirst() methods are used to get the first element of the deque array.
    Example :
    import java.util.*;
    public class CWH extends Thread{
        public static void main(String[] args) {
    
            ArrayDeque<Integer> ad1 = new ArrayDeque<>();
            ad1.add(6);
            ad1.add(56);
            ad1.add(9);
            ad1.add(10);
            ad1.add(91);
            ad1.add(19);
    
            System.out.println(ad1.getFirst());
            System.out.println(ad1.peekFirst());
    
        }
    }
    
Output :
6
6
Accessing the last element: getLast() or peekLast() methods are used to print the last element of the deque array.
Example :
import java.util.*;
public class CWH extends Thread{
    public static void main(String[] args) {

        ArrayDeque<Integer> ad1 = new ArrayDeque<>();
        ad1.add(6);
        ad1.add(56);
        ad1.add(9);
        ad1.add(10);
        ad1.add(91);
        ad1.add(19);

        System.out.println(ad1.getLast());
        System.out.println(ad1.peekLast());

    }
}
Output :
19
19
Removing an element :
  • Removing the first element: removeFirst() & pollFirst() methods are used to delete an element from the head of the queue.
  • removeFirst() throws an exception if the queue is empty.
  • pollFirst() returns null if the queue is empty.
    Example :
    import java.util.*;
    public class CWH extends Thread{
        public static void main(String[] args) {
    
            ArrayDeque<Integer> ad1 = new ArrayDeque<>();
            ad1.add(6);
            ad1.add(56);
            ad1.add(9);
            ad1.add(10);
            ad1.add(91);
            ad1.add(19);
    
            ad1.pollFirst(); //deletes 6
            ad1.removeFirst(); //deletes 56
    
            System.out.println(ad1);
    
        }
    }
    
Output :
[9, 10, 91, 19]
Removing the last element: removeLast() & pollLast() methods are used to delete an element from the tail of the queue.
Example :
import java.util.*;
public class CWH extends Thread{
    public static void main(String[] args) {

        ArrayDeque<Integer> ad1 = new ArrayDeque<>();
        ad1.add(6);
        ad1.add(56);
        ad1.add(9);
        ad1.add(10);
        ad1.add(91);
        ad1.add(19);

        ad1.pollLast(); //deletes 19
        ad1.removeLast(); //deletes 91

        System.out.println(ad1);

    }
}
Output :
[6, 56, 9, 10]

Code as described/written in the video :

package com.company;

import java.util.ArrayDeque;

public class cwh_93_arraydeque {
    public static void main(String[] args) {
        ArrayDeque<Integer> ad1 = new ArrayDeque<>();
        ad1.add(6);
        ad1.add(56);
        ad1.add(9);
        ad1.addFirst(5);
        System.out.println(ad1.getFirst());
        System.out.println(ad1.getLast());
    }
}


Code as described/written in the video :

package com.company;

import java.util.ArrayDeque;

public class cwh_93_arraydeque {
    public static void main(String[] args) {
        ArrayDeque<Integer> ad1 = new ArrayDeque<>();
        ad1.add(6);
        ad1.add(56);
        ad1.add(9);
        ad1.addFirst(5);
        System.out.println(ad1.getFirst());
        System.out.println(ad1.getLast());
    }
}

LinkedList in Java: Methods

 

LinkedList in Java:  Methods

  • The LinkedList class in Java provides us with the doubly linked list data structure.
  • Each element of the linked list is known as a node.
  • Each node points to the address of the next node & its previous node.

Linkedlist image

  • Linked lists are preferred over the Array list because the insertion & deletion in the linked lists can be done in a constant time. But, in arrays, if we want to add or delete an element in between then, we need to shift all the other elements.
  • In a linked list, it is impossible to directly access an element because we need to traverse the whole linked list to get the desired element.

ArrayList Vs. LinkedList :

Although ArrayList & LinkedList both implement the List interface and have the same methods, it is important to understand when to use which one.

  • The insertion & deletion can be done in constant time in Linked List, so it is best to use the linked list when you need to add or remove elements frequently.
  • Use ArrayList when you want to access the random elements frequently, as it can’t be done in a linked list in constant time.

Performing various operations on LinkedList :

  1. Adding Element in LinkedList:
    • Similar to ArrayList, add() method is used to add elements in a linked list.
    • add(Object): Inserts an element at the end of the ArrayList.
    • add(Index,Object) : Inserts an element at the given index.
      Example :
      import java.util.*;
      public class CWH extends Thread{
          public static void main(String[] args) {
      
              LinkedList<Integer> l1 = new LinkedList<>();
      
              l1.add(11);
              l1.add(22);
              l1.add(33);
              l1.add(44);
              l1.add(55);
              l1.add(77);
              l1.add(5,77); // Inserts 77 at index 5
              System.out.println("L1 Linked list : "+ l1);
      
          }
      }
Output :
L1 Linked list : [11, 22, 33, 44, 55, 77, 77]
Removing an element from the LinkedList:
  • remove() method is used to remove an element from the linked list.

    Example :

    import java.util.*;
    public class CWH extends Thread{
        public static void main(String[] args) {
    
            LinkedList<Integer> l1 = new LinkedList<>();
    
            l1.add(11);
            l1.add(22);
            l1.add(33);
            l1.add(44);
            l1.add(55);
            l1.add(77);
            l1.add(5,77); 
            System.out.println("L1 Linked list before: "+ l1);
    
            l1.remove(2); //removes element present at 2nd index
            System.out.println("L1 Linked list after: "  + l1);
    
        }
    }
    
Output :
L1 Linked list before: [11, 22, 33, 44, 55, 77, 77]
L1 Linked list after: [11, 22, 44, 55, 77, 77]
Changing An Element Of Linked List :
  • set() method is used to change an already existing element of a linked list.

    Example :
    import java.util.*;
    public class CWH extends Thread{
        public static void main(String[] args) {
    
            LinkedList<Integer> l1 = new LinkedList<>();
    
            l1.add(11);
            l1.add(22);
            l1.add(33);
            l1.add(44);
            l1.add(55);
            l1.add(66);
            System.out.println("L1 Linked list before: "+ l1);
    
            l1.set(2,10); //changes element present at 2nd index (33 changed to 10)
            System.out.println("L1 Linked list after: "  + l1);
    
        }
    }
    
Output :
L1 Linked list before: [11, 22, 33, 44, 55, 66]
L1 Linked list after: [11, 22, 10, 44, 55, 66]
Inserting an element at the last of the linked list:
  • addlast() method is used to insert an element at the start of the linked list.

    Example :
    F.	import java.util.*;
    public class CWH extends Thread{
        public static void main(String[] args) {
    
            LinkedList<Integer> l1 = new LinkedList<>();
    
            l1.add(11);
            l1.add(22);
            l1.add(33);
            l1.add(44);
            l1.add(55);
            l1.add(66);
            System.out.println("L1 Linked list before: "+ l1);
    
            l1.addLast(100); //Inserting 100 at the end of L1
            System.out.println("L1 Linked list after inserting element at last index: "  + l1);
    
        }
    }
    
Output :
L1 Linked list before: [11, 22, 33, 44, 55, 66]
L1 Linked list after inserting element at last index: [11, 22, 33, 44, 55, 66, 100]
Inserting an element at the start of the linked list:
  • addFirst() method is used to insert an element at the start of the linked list.

    Example :
    import java.util.*;
    public class CWH extends Thread{
        public static void main(String[] args) {
    
            LinkedList<Integer> l1 = new LinkedList<>();
    
            l1.add(11);
            l1.add(22);
            l1.add(33);
            l1.add(44);
            l1.add(55);
            l1.add(66);
            System.out.println("L1 Linked list before: "+ l1);
    
            l1.addFirst(0); //Inserting 0 at the starting of L1
            System.out.println("L1 Linked list after: "  + l1);
    
        }
    }
    

Output :

L1 Linked list before: [11, 22, 33, 44, 55, 66]
L1 Linked list after: [0, 11, 22, 33, 44, 55, 66]

Code as described/written in the video :

package com.company;

import java.util.*;

public class cwh_92_linkedlist {
    public static void main(String[] args) {
        LinkedList<Integer> l1 = new LinkedList<>();
        LinkedList<Integer> l2 = new LinkedList<>();
        l2.add(15);
        l2.add(18);
        l2.add(19);

        l1.add(6);
        l1.add(7);
        l1.add(4);
        l1.add(6);
        l1.add(0, 5);
        l1.add(0, 1);
        l1.addAll(0, l2);
        l1.addLast(676);
        l1.addFirst(788);
        System.out.println(l1.contains(27));
        System.out.println(l1.indexOf(6));
        System.out.println(l1.lastIndexOf(6));
        //l1.clear();
        l1.set(1, 566);
        for(int i=0; i<l1.size(); i++){
            System.out.print(l1.get(i));
            System.out.print(", ");
        }
    }
}