Name |
Code |
Reading Directories
|
class DirList { public static void main(String args[]) { String dirname = "/java"; File f1 = new File(dirname); if (f1.isDirectory()) { System.out.println( "Directory of " + dirname); String s[] = f1.list(); for (int i=0; i < s.length; i++) { File f = new File(dirname + "/" + s[i]); if (f.isDirectory()) { System.out.println(s[i] + " is a directory"); } else { System.out.println(s[i] + " is a file"); } } } else { System.out.println(dirname + " is not a directory"); } } }
|
Reverse Number
|
class Reverse { public static void main(String args[]) { int num = Integer.parseInt(args[0]); //take argument as command line int remainder, result=0; while(num>0) { remainder = num%10; result = result * 10 + remainder; num = num/10; } System.out.println("Reverse number is : "+result); } }
|
SelecionSort
|
public class SelecionSort { /** * Sorts the given list in place. * Worst Case Performance O(n^2) * Best Case Performance O(n^2) * Average Case Performance O(n^2) * Insertion sort can be- and usually is a much faster sorting algorithm, than selection sort. * Selection sort is superior because it does less swaps. * @param list - int array that you want to sort. */ public static void sortNumbers(Integer[] list) { //go through the list for (int i=0; i< list.length;i++) { //define min int min = i; //go through the remaining list and see if there is smaller number for (int j=i+1;j< list.length;j++) { //if there is a smaller number if (list[j] < list[min]) { //remember its place min = j; } } if (i != min) { //swap the min element, moving it to its proper place in the list. int temp = list[min]; list[min] = list[i]; list[i] = temp; } } } /** * Just for testing purposes. */ public static void main(String[] args) { Integer[] list = new Integer[5]; list[0] = 32; list[1] = 24; list[2] = 235; list[3] = 1; list[4] = 0; sortNumbers(list); for (int i = 0;i< list.length;i++) { System.out.println(list[i]); } } }
|
ShellSort
|
public class ShellSort { private long[] data; private int len; public ShellSort(int max) { data = new long[max]; len = 0; } public void insert(long value) { data[len] = value; len++; } public void display() { System.out.print("Data:"); for (int j = 0; j < len; j++) System.out.print(data[j] + " "); System.out.println(""); } public void shellSort() { int inner, outer; long temp; //find initial value of h int h = 1; while (h <= len / 3) h = h * 3 + 1; // (1, 4, 13, 40, 121, ...) while (h > 0) // decreasing h, until h=1 { // h-sort the file for (outer = h; outer < len; outer++) { temp = data[outer]; inner = outer; // one subpass (eg 0, 4, 8) while (inner > h - 1 && data[inner - h] >= temp) { data[inner] = data[inner - h]; inner -= h; } data[inner] = temp; } h = (h - 1) / 3; // decrease h } } public static void main(String[] args) { int maxSize = 10; ShellSort arr = new ShellSort(maxSize); for (int j = 0; j < maxSize; j++) { long n = (int) (java.lang.Math.random() * 99); arr.insert(n); } arr.display(); arr.shellSort(); arr.display(); } }
|
Simple Thread Example
|
public class SimpleThread extends Thread { private int countDown = 5; private static int threadCount = 0; public SimpleThread() { super("" + ++threadCount); // Store the thread name start(); } public String toString() { return "#" + getName() + ": " + countDown; } public void run() { while(true) { System.out.println(this); if(--countDown == 0) return; } } public static void main(String[] args) { for(int i = 0; i < 5; i++) new SimpleThread(); } }
|
Square Root of BigInteger
|
public class SquareRootOfBigIntegerExample { public static void main(String[] args) { SquareRootOfBigIntegerExample SquareRootOfBigIntegerExample = new SquareRootOfBigIntegerExample(); String n = ""; MathContext mc = new MathContext(0, RoundingMode.DOWN); mc = MathContext.DECIMAL32; BigInteger my2P100000 = new BigInteger("0"); BigInteger two = new BigInteger("2"); BigInteger one = new BigInteger("1"); my2P100000 = two.shiftLeft(2000 - 1); System.out.println("2^2000 -- Step 1"); System.out.println("Value of 2^2,000 " + my2P100000 ); System.out.println(""); System.out.println("Finding the Square Root of 2^2000"); String mys = my2P100000 + ""; n = (mys) ; int firsttime = 0; BigDecimal myNumber = new BigDecimal(n); BigDecimal g = new BigDecimal("1"); BigDecimal my2 = new BigDecimal("2"); BigDecimal epsilon = new BigDecimal("0.0000000001"); BigDecimal nByg = myNumber.divide(g, 9, BigDecimal.ROUND_FLOOR); //Get the value of n/g BigDecimal nBygPlusg = nByg.add(g); //Get the value of "n/g + g BigDecimal nBygPlusgHalf = nBygPlusg.divide(my2, 9, BigDecimal.ROUND_FLOOR); //Get the value of (n/g + g)/2 BigDecimal saveg = nBygPlusgHalf; firsttime = 99; do { g = nBygPlusgHalf; nByg = myNumber.divide(g, 9, BigDecimal.ROUND_FLOOR); nBygPlusg = nByg.add(g); nBygPlusgHalf = nBygPlusg.divide(my2, 9, BigDecimal.ROUND_FLOOR); BigDecimal savegdiff = saveg.subtract(nBygPlusgHalf); if (savegdiff.compareTo(epsilon) == -1 ) { firsttime = 0; } else { saveg = nBygPlusgHalf; } } while (firsttime > 1); System.out.println("For " + mys + "\nLength: " + mys.length() + "\nThe Square Root is " + saveg); } }
|
StringBuffer To File Java Example
|
public class JavaStringBufferToFileExample { public static void main(String[] args) throws IOException { //create StringBuffer object StringBuffer sbf = new StringBuffer(); //StringBuffer contents sbf.append("StringBuffer contents first line."); //new line sbf.append(System.getProperty("line.separator")); //second line sbf.append("StringBuffer contents second line."); /* * To write contents of StringBuffer to a file, use * BufferedWriter class. */ BufferedWriter bwr = new BufferedWriter(new FileWriter(new File("d:/demo.txt"))); //write contents of StringBuffer to a file bwr.write(sbf.toString()); //flush the stream bwr.flush(); //close the stream bwr.close(); System.out.println("Content of StringBuffer written to File."); } }
|
Convert String to primitive byte Example
|
public class StringToPrimitiveByteExample { public static void main(String[] args) { //declare String object String str = new String("10"); /* use parseInt method of Byte class to convert String into byte primitive data type. This is a static method. Please note that this method can throw a NumberFormatException if the string is not parsable to byte. */ byte b = Byte.parseByte(str); System.out.println(b); } }
|
Sum Product of Digit
|
class Sum_Product_ofDigit { public static void main(String args[]) { int num = Integer.parseInt(args[0]); //taking value as command line argument. int temp = num,result=0; //Logic for sum of digit while(temp>0) { result = result + temp; temp--; } System.out.println("Sum of Digit for "+num+" is : "+result); //Logic for product of digit temp = num; result = 1; while(temp > 0) { result = result * temp; temp--; } System.out.println("Product of Digit for "+num+" is : "+result); } }
|
Create a thread by implementing Runnable
|
class MyThread implements Runnable { int count; MyThread() { count = 0; } public void run() { System.out.println("MyThread starting."); try { do { Thread.sleep(500); System.out.println("In MyThread, count is " + count); count++; } while (count < 5); } catch (InterruptedException exc) { System.out.println("MyThread interrupted."); } System.out.println("MyThread terminating."); } } class RunnableDemo { public static void main(String args[]) { System.out.println("Main thread starting."); MyThread mt = new MyThread(); Thread newThrd = new Thread(mt); newThrd.start(); do { System.out.println("In main thread."); try { Thread.sleep(250); } catch (InterruptedException exc) { System.out.println("Main thread interrupted."); } } while (mt.count != 5); System.out.println("Main thread ending."); } }
|
No comments: