Collections - Telephone Directry -Add / Remove

 Collections - Telephone Directry -Add / Remove

public class MapTest
{

    public static void main(String[] args)
    {
        Map mp = new Hashtable<String, String>();
        mp.put("Ram", "9830632010");
        mp.put("Sham", "9830632011");
        mp.put("Hari", "12001010002");
       
        System.out.println(mp);
       
        System.out.println( mp.containsKey("Ram"));
        mp.remove("Ram");
       
        System.out.println(mp);
       
       

    }

}

Retrive numeric values from an ArrayList

Retrive numeric values from an ArrayList

ArrayList a= new ArrayList();
        a.add(1);
        a.add(1.78);
        a.add('A');
        a.add("Hello");
        a.add(78);
        double sum=0;
        for(Object o:a)
        {
        Number d=null;
        // cast the object based on type and use it
        if(o instanceof Number)
        {
        d=(Number)o;
        sum =sum+d.doubleValue();
        }
    }
        System.out.print(sum);}


Library management -book borrow /Fine calculation

            Library management -book borrow /Fine calculation


package muttablepack;

public class Book {

    private int id;
    private String name;
   
    public Book(){}
   
    public Book(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

   
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Book [id=" + id + ", name=" + name + "]";
    }
   
   
}


--------------------------------------------------------------------------------------------------------------------
package muttablepack;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Borrow
    {

    private int slno;
    private Member member;
    private Book book;
    String borrowDate;
   
   
   
    public Borrow(){}

   

    public Borrow(int slno, Member member, Book book, String borrowDate)
    {
        this.slno = slno;
        this.member = member;
        this.book = book;
        this.borrowDate = borrowDate;
    }



    public Member getMember()
    {
        return member;
    }

    public void setMember(Member member)
    {
        this.member = member;
    }

    public Book getBook()
    {
        return book;
    }

    public void setBook(Book book)
    {
        this.book = book;
    }



    public int getSlno()
    {
        return slno;
    }



    public void setSlno(int slno)
    {
        this.slno = slno;
    }



    public String getBorrowDate()
    {
        return borrowDate;
    }



    public void setBorrowDate(String borrowDate)
    {
        this.borrowDate = borrowDate;
    }
   
   
}


--------------------------------------------------------------------------------------------------------
package muttablepack;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class Library {

    private ArrayList<Member> memberList = new ArrayList<Member>();
    private ArrayList<Book> bookList = new ArrayList<Book>();
    static Date date = new Date();
   
   
    public Library(){}
    public void addBook(Book book)
    {
         bookList.add(book);
    }
    public void addMember(int index, Member member)
    {
        memberList.add(index, member);
    }
    public ArrayList searchMember()
    {
        return memberList;
    }
   
    public static Date getDate() {
        return date;
    }
    public static void setDate(Date date)
    {
        SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy");
        ft.format(date);
        Library.date = date;
    }

    public String issue(Book book, Member member)
    {
        if(!member.hasBook())
        {
        member.setBook(book);
        return "issued the book successfully";
        }
        else
            return " member cant access more than one book";
       
    }

    public double bill(Borrow b) throws ParseException
    {
        double amount = 0.0;
        SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy");
        Date d = ft.parse(b.getBorrowDate());
        int days =  (int) (Library.getDate().getTime() - d.getTime())/(24*60*60*1000);
        System.out.println("days = " + days);
        if(days<=7)
        {
            days = 0;
            amount = 0.0;
        }
        else if ((days>7) && (days<=30))
        {
           
            days = days - 7;
            System.out.println("inside days = " + days);
            amount = days * 10;
           
        }
        else if((days>30) && (days<=60))
        {
            days = 23 + (days - 30);
            amount = days * 50;
        }
        else
            {
                int remainingDays  = days - 53;
                double months = Math.ceil(remainingDays/30);
                amount = 53 * 50 + months * 50;
            }
        return amount;
       
    }

    public static void main(String ar[]) throws ParseException
    {
        Library l = new Library();
        Book b1 = new Book(100,"Java");
        Member m1 = new Member(2000,"Vikas");
        l.addBook(b1);
        l.addMember(0, m1);
        System.out.println(l.issue(b1, m1));
        //System.out.println(l.issue(b1, m1));
        Borrow b = new Borrow(1, m1, b1, "10/10/2014");
        System.out.println(b.getBorrowDate());
        System.out.println(l.bill(b));
    }
}

Anagram using Java !

 Anagram using Java !


package pkgArrayToString;
import java.util.*;
public class Anagram {

    public static void main(String[] args)
    {
         Scanner scn = new Scanner(System.in);
         boolean b=false;
       
            System.out.println("Put in a set: ");
            String input = scn.nextLine();
           
            System.out.println(input);
           
            Scanner tokenizer = new Scanner(input);
           
            tokenizer.useDelimiter(" ");
            ArrayList<String> arr = new ArrayList<String>();
           
            while(tokenizer.hasNext())
            {
                arr.add(tokenizer.next());
            }
           
            for (int i=0;i<arr.size();i++)
            {
                String strAng = arr.get(i);
                for (int j=0;j<arr.size();j++)
                {
                    b= Check(strAng,arr.get(j));
                    if (b==true)
                        System.out.println("The strings are Anagram :" + strAng + "And" + arr.get(j));
                }
            }
    scn.close();
    tokenizer.close();
   
    }
   
    public static boolean Check(String stMain , String str2)
    {
       
        boolean chk=false;
        stMain= stMain.toLowerCase();
        str2= str2.toLowerCase();
       
        char[] crMain = stMain.toCharArray();
        char [] crVar= str2.toCharArray();
        if(Arrays.equals(crMain, crVar)==true)
        {
            return chk;
        }
        else
        {
        Arrays.sort(crMain);
        Arrays.sort(crVar);
        chk=Arrays.equals(crMain, crVar);
        }
        return chk;
       
       
    }
}


Words to Sentence in JAVA

.......Words to Sentence in JAVA .......

String [] arr= {"hello","How","are","You","??"};
        System.out.println("The words are :\n");
        for(String s:arr)
        System.out.println(s);
       
       
        //----------------------Form a sentence from those words==============
       
        System.out.println("The sentence is :\n");
        String joined = String.join(" ", arr);;
        System.out.println(joined);
       

Garbage collector

The Garbage Collector
Some object-oriented languages require that you keep track of all the objects you create and that you explicitly destroy them when they are no longer needed. Managing memory explicitly is tedious and error-prone. The Java platform allows you to create as many objects as you want (limited, of course, by what your system can handle), and you don't have to worry about destroying them. The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.
An object is eligible for garbage collection when there are no more references to that object. References that are held in a variable are usually dropped when the variable goes out of scope. Or, you can explicitly drop an object reference by setting the variable to the special value null. Remember that a program can have multiple references to the same object; all references to an object must be dropped before the object is eligible for garbage collection.
The Java runtime environment has a garbage collector that periodically frees the memory used by objects that are no longer referenced. The garbage collector does its job automatically when it determines that the time is right.

“finalize ()” is called by the garbage collector no an object when garbage collection determines that there are no more references to the object. A sub class overrides the “finalize ()” to dispose of of system resource or to perform other clean-up.
Garbage collection is a tool which removes unreferenced object from the memory. When garbage collector remove object from the memory it calls the finalize method.    
There is no guarantee when garbage collector will be invoked and what object referenced will be removed.

Using System.gc ()-         we can invoke garbage collector.

------------------------------->>>>>>>>>>>>
/**Garbage collector dosenot gureenties when and what object will be removed,
but gureenties its will remove unrefrence object only if removes,
by overriding the finalize method **/

public class GarbageDemo
{
public static void main (String...args)
{
int i=0;
while(i++<10000000)
{
MyClass mc=new MyClass();
mc.i=i;
}
System.gc();//Used to invoke garbage collector.
}
}
class MyClass
{
int i=0;
public void finialize()
//This method is called Automatically for clearing unreferenced object.

{
System.out.println("Object removed"+i);
}
}

ArrayList insert / Search / Delete in Java !

ArrayList insert / Search / Delete in Java !

package PKG;
import java.util.*;

//import javax.xml.bind.ValidationEvent;

public class ArrayListProg {

    public static void main(String[] args)
    {
        Integer valDel;
        int index;
       
        ArrayList<Integer> arr = new ArrayList();
        Scanner sc = new Scanner(System.in);
        System.out.println("Please Enter the elements of array");
        //Insert.................................................
       
        for (int i=0;i<5;i++)
        {
        arr.add(sc.nextInt());
        }
   
        do{
        // .. Delete.............................................
        System.out.println("Please Enter the value to be deleted");
        valDel= sc.nextInt();
        for (Integer i:arr)
        {
            if(arr.contains(valDel))
            {       
                index = arr.indexOf(valDel);
               
                arr.remove(index);
                System.out.println("Deleted");
                break;
            }
            /*else
                System.out.println("not found :");*/
        }
        //....Find...............................................
       
       
        System.out.println("Please Enter the value to be Searched :");
        valDel= sc.nextInt();
       
        if( arr.contains(valDel)==true)
            System.out.println("Value Exist :");
        else
            System.out.println("Value not Exist :");
       
        System.out.println("Please Enter 555 to exit \n any other number to continue :");
        valDel=sc.nextInt();
        } while (valDel !=555);
    }

}

Matrix Addition Using JAVA

 Matrix Addition Using JAVA
  
package PKG;
import java.util.Scanner;

public class MatrixAddition {

    static final int ROW=3;
    static final int COL=3;
   
   
    public static void main(String[] args)
    {
        Scanner sc= new Scanner (System.in);
        int [][] arr1 = new int[ROW][COL];
        int [][] arr2 = new int [ROW][COL];
        int [][] Result = new int [ROW][COL];
        int i=0 ;
        int j=0 ;
        System.out.println("1st array :");
        for(i=0;i<ROW;i++)
        {
            for(j=0;j<COL;j++)
            {
                arr1[i][j] = sc.nextInt();
               
            }
        }
        System.out.println("2nd array :");
       
        for(i=0;i<ROW;i++)
        {
            for(j=0;j<COL;j++)
            {
                arr2[i][j] = sc.nextInt();
               
            }
        }
       
        for(i=0;i<ROW;i++)
        {
            for(j=0;j<COL;j++)
            {
               
                Result[i][j]=arr1[i][j] + arr2[i][j];
               
               
            }
        }
       
        System.out .println("Result : ");
       
        for(i=0;i<ROW;i++)
        {
            for(j=0;j<COL;j++)
            {
               
                System.out.print(Result[i][j]+"\t");
               
               
            }
           
            System.out.println("");
        }
       
    }

}