Showing posts with label Core Java. Show all posts
Showing posts with label Core Java. Show all posts

Thursday, 30 April 2015

Difference between Iterator and List Iterator Example

/*
 *    Iterator traverse elements only in one direction i.e forward
 *    ListIterator traverse elements in both directions i.e forward and backward
 *    Using ListIterator we can modify the existing list as well as done in below sample
 */

public class IteratorClass extends Activity
{

ArrayList<String> miteratorList;
Iterator<String> mIterator;
ListIterator<String> mListIterator;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initiViews();
}

private void initiViews()
{
//Initializing the ArrayList
miteratorList=new ArrayList<String>();

//Adding values to the ArrayList
miteratorList.add("A");
miteratorList.add("B");
miteratorList.add("C");
miteratorList.add("D");
miteratorList.add("E");


//method to traverse elements using Iterator here
usingIterator();

//Initializing the mListIterator here
mListIterator=miteratorList.listIterator();

//method to traverse and modify elements using mListIterator here
usingListIterator();

//method to traverse elements using Iterator here
usingIterator();

usingListIteratorTOTraverseBackward();

}


private void usingIterator()
{
//Initializing the mIterator here
mIterator=miteratorList.iterator();//Returns an iterator on the elements of this list.
while(mIterator.hasNext())
{
Object element = mIterator.next();
System.out.print(element + " ");
}

//O/P goes like this => 04-29 18:32:44.590: I/System.out(26302): A B C D E

System.out.println();
}


private void usingListIterator()
{
//Modifying the existing list using list iterator
while (mListIterator.hasNext()) {
Object element=mListIterator.next();
mListIterator.set(element+"Z");  
}

System.out.println();

//04-29 18:45:44.588: I/System.out(1605): AZ BZ CZ DZ EZ

}


private void usingListIteratorTOTraverseBackward()
{
// Now, display the list in reverse order using ListIterator
     while(mListIterator.hasPrevious()) {
        Object element = mListIterator.previous();
        System.out.print(element + " ");
      }
      System.out.println();
     
          // 04-29 18:52:30.478: I/System.out(5954): EZ DZ CZ BZ AZ

}

}

Monday, 6 April 2015

Generating HMC SHA1 hash in Java Using a Secret Key (Private Key)


Below is the method :

void generateHMCSHA1(){

int counter= 1;
           String privateKey = "0123456789ABCDEF0123456789ABCDEF";
           byte[] result = null;
           MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-1");
byte[] keyBytes = md.digest(privateKey.getBytes("UTF-8"));

           SecretKey sk = new SecretKeySpec(keyBytes, "HmacSHA1");
           Mac mac = Mac.getInstance("HmacSHA1");
           mac.init(sk);
            result = mac.doFinal(String.valueOf(counter).getBytes("US_ASCII"));

           StringBuilder sb = new StringBuilder();
           for (byte b : result) {
               sb.append(String.format("%02X ", b));
           }
         
           System.out.println("      Key: " + privateKey + "\n");
       
           System.out.println("  Results: " +sb );
         
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}           

}


Output :

Key: 0123456789ABCDEF0123456789ABCDEF
 
Results: 38 92 62 29 80 04 C9 83 EE 87 97 B0 C0 FD 77 4C 69 F3 82 

Wednesday, 26 June 2013

Program To Add Two Numbers In Java.

import java.util.Scanner; 

class AddNumbers
{
   public static void main(String args[])
   {
      int no1, no2, result;
      System.out.println("Enter two integers to calculate their sum ");
      Scanner in = new Scanner(System.in);
      no1 = in.nextInt();
      no2 = in.nextInt();
      result = no1 + no2;
      System.out.println("Sum of entered integers = "+result);
   }
}




Friday, 29 June 2012

Creating Package Using JAVA


package society;


public class Human
{
 public static void main(String arg[])
{
             Human t=new Human();
             t.hello();            


       }


    void hello()
  {
    System.out.println("hello");
  }


}

Use Of Scanner Class Function In JAVA


import java.util.Scanner;


public class Ar
{
   public static void main(String arg[])
 {
      int[] n=new int[10];
Scanner in=new Scanner(System.in);
System.out.println("Enter the elements : ");
      int i=0;

for(i=0;i<10;i++)
{
System.out.print("Enter the element a[ : "+ i +"] = ");
n[i]=in.nextInt();
}
for(i=0;i<10;i++)
{
System.out.println("elements are  a[ : "+ i +"] = "+n[i]);

}


   
 }
}

Coloring With JAVA


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Draw implements ActionListener
{
JFrame f;

JButton b1;
JButton b2;
JButton b3;
JLabel lb;
public Draw()
{
f=new JFrame();
b1=new JButton("RED");
b2=new JButton("GREEN");
b3=new JButton("ORANGE");
lb=new JLabel();

Shape s=new Shape();

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

f.add(BorderLayout.CENTER,s);
lb.setForeground(Color.white);
f.add(BorderLayout.NORTH,lb);
f.add(BorderLayout.SOUTH,b1);
f.add(BorderLayout.WEST,b2);
f.add(BorderLayout.EAST,b3);

f.setVisible(true);
f.setSize(400,400);
}
public void actionPerformed(ActionEvent ae)
{

}
public void paintComponent(Graphics g)
{
g.setColor(Color.red);
g.fillRect(20,30,110,110);
}
public static void main(String[] args)
{
Draw a=new Draw();
}


}