Wednesday, November 5, 2014

SYNCHRONIZED,BINARY PROGRAM



Disable Button

import java.applet.Applet;
import java.awt.Button;
public class DisableButtonExample extends Applet
{
public void init()
{
Button Button1 = new Button("Ok");
Button Button2 = new Button("Cancel");
add(Button1);
add(Button2);
Button2.setEnabled(false);
    }
}

Java String Buffer setLength

public class JavaStringBufferSetLengthExample
{
public static void main(String[] args)
{
StringBuffer sbf = new StringBuffer("StringBuffer setLength method example");
sbf.setLength(12);
System.out.println("StringBuffer contents: " + sbf);
sbf.setLength(0);
System.out.println("StringBuffer contents deleted:" + sbf);
   }
}

Convert String to long

public class StringToPrimitiveLongExample
 {
public static void main(String[] args)
{
String str = new String("10");
long l = Long.parseLong(str);
System.out.println(l);
   }
}

Get Selected Item From Single Select AWT List

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.List;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class GetSelectedItemFromSingleSelectExample extends Applet implements ItemListener
{  
List list = null;
public void init()
{
list = new List(5, false);
list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.add("Five");
list.add("Six");
list.add("Seven");
add(list);
list.addItemListener(this);
}
public void paint(Graphics g)
{
g.drawString("Selected Item: "+ list.getSelectedItem(), 10, 120);
}
public void itemStateChanged(ItemEvent arg0)
                    {
repaint();     
  }
}

Performing Binary Search on Java float Array

import java.util.Arrays;
public class BinarySearchFloatArrayExample
{
public static void main(String[] args)
{
float floatArray[] = {1.23f,2.10f,4.74f,5.34f};
Arrays.sort(floatArray);float searchValue = 4.74f;
int intResult = Arrays.binarySearch(floatArray,searchValue);
System.out.println("Result of binary search of 4.74 is : " + intResult);
searchValue = 3.33f;
intResult = Arrays.binarySearch(floatArray,searchValue);
System.out.println("Result of binary search of 3.33 is : " + intResult);
    }
}

Get Synchronized List from Java ArrayList

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GetSynchronizedListFromArrayListExample
{
public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
List list = Collections.synchronizedList(arrayList);  
   }
}

 

No comments:

Post a Comment