Monday, November 3, 2014

Insertion Java



Write a program for Insertion Sort in java

package com.java2novice.algos;
public class MyInsertionSort
{
public static void main(String[] args)
{
        int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
        insertionSort(input);
 }
 private static void printNumbers(int[] input)
{
    for (int i = 0; i < input.length; i++)
    {
            System.out.print(input[i] + ", ");
        }
        System.out.println("\n");
    }
 public static void insertionSort(int array[])
 {
        int n = array.length;
        for (int j = 1; j < n; j++)
         {
            int key = array[j];
            int i = j-1;
            while ( (i > -1) && ( array [i] > key ) )
            {
                array [i+1] = array [i];
                i--;
            }
            array[i+1] = key;
            printNumbers(array);
        }
    }
}

Write a program to implement hashcode and equals

package com.java2novice.algos;
import java.util.HashMap;
public class MyHashcodeImpl
{
public static void main(String a[])
{
        HashMap hm = new HashMap();
        hm.put(new Price("Banana", 20), "Banana");
        hm.put(new Price("Apple", 40), "Apple");
        hm.put(new Price("Orange", 30), "Orange");
        Price key = new Price("Banana", 20);
        System.out.println("Hashcode of the key: "+key.hashCode());
        System.out.println("Value from map: "+hm.get(key));
    }
}
class Price
{
     
    private String item;
    private int price;
 public Price(String itm, int pr)
 {
        this.item = itm;
        this.price = pr;
    }
  public int hashCode()
  {
        System.out.println("In hashcode");
        int hashcode = 0;
        hashcode = price*20;
        hashcode += item.hashCode();
        return hashcode;
    }
  public boolean equals(Object obj)
   {
        System.out.println("In equals");
        if (obj instanceof Price)
        {
            Price pp = (Price) obj;
            return (pp.item.equals(this.item) && pp.price == this.price);
        }
        else
        {
            return false;
        }
    }
   public String getItem()
  {
        return item;
    }
   public void setItem(String item)
  {
        this.item = item;
    }
    public int getPrice()
   {
        return price;
    }
  public void setPrice(int price)
  {
        this.price = price;
    }
  public String toString()
   {
        return "item: "+item+"  price: "+price;
    }
}

How to get distinct elements from an array by avoiding duplicate elements

package com.java2novice.algos;
public class MyDisticntElements
{
public static void printDistinctElements(int[] arr)
{
        for(int i=0;i
        {
            boolean isDistinct = false;
            for(int j=0;j
             {
                if(arr[i] == arr[j])
                {
                    isDistinct = true;
                    break;
                }
            }
            if(!isDistinct)
            {
                System.out.print(arr[i]+" ");
            }
        }
    }
   public static void main(String a[])
   {
        int[] nums = {5,2,7,2,4,7,8,2,3};
        MyDisticntElements.printDistinctElements(nums);
    }
}

Write a program to get distinct word list from the given file

package com.java2novice.algos;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class MyDistinctFileWords
{
public List getDistinctWordList(String fileName)
{
        FileInputStream fis = null;
        DataInputStream dis = null;
        BufferedReader br = null;
        List wordList = new ArrayList();
        try
        {
            fis = new FileInputStream(fileName);
            dis = new DataInputStream(fis);
            br = new BufferedReader(new InputStreamReader(dis));
            String line = null;
            while((line = br.readLine()) != null)
             {
                StringTokenizer st = new StringTokenizer(line, " ,.;:\"");
                while(st.hasMoreTokens())
                 {
                    String tmp = st.nextToken().toLowerCase();
                     if(!wordList.contains(tmp))
                     {
                        wordList.add(tmp);
                    }
                }
            }
        }
         catch (FileNotFoundException e)
         {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try{if(br != null) br.close();}catch(Exception ex){}
        }
        return wordList;
    }
    public static void main(String a[])
    {
        MyDistinctFileWords distFw = new MyDistinctFileWords();
        List wordList = distFw.getDistinctWordList("C:/sample.txt");
        for(String str:wordList)
        {
            System.out.println(str);
        }
    }
}

No comments:

Post a Comment