Name |
Code |
MergeSort
|
public class MergeSort { /** * Merges two arrays into one. * @param A array that contains the two arrays. * @param temp temporary array that we will work with * @param lo lowest index value in the array * @param mid middle index value in the array (represents break between low and high values) * @param hi highest index value in the array */ private static void Merge(int[] A, int[] temp, int lo, int mid, int hi) { int i = lo; int j = mid; for (int k = lo; k < hi; k++) { if (i == mid) temp[k] = A[j++]; //if lo-mid array is empty else if (j == hi) temp[k] = A[i++]; //if mid-hi array is empty else if (A[j] > A[i]) temp[k] = A[i++]; //i is lower so we put it in the array first else temp[k] = A[j++]; //j is lower or equal to i, so we put it in the array first. } //now we need to copy back temp array to its place in A array for (int k = lo; k < hi; k++) A[k] = temp[k]; //we are copying only the numbers we just merged. } private static void MergeSort(int[] A, int lo, int hi) { if (hi - lo == 1) return; //only one element in the array, return. int mid = lo + (hi - lo) / 2; //find middle MergeSort(A, lo, mid); //sort first part MergeSort(A, mid, hi); //sort second part Merge(A, new int[A.length], lo, mid, hi); //merge both parts } /** * Sorts the given list. (Not in place) * Worst Case Performance O(nlogn) * Best Case Performance O(nlogn) * Average Case Performance O(nlogn) * @param A list of int array. */ public static void sortNumbers(int[] A) { MergeSort(A, 0, A.length); } /** * Just for testing purposes. */ public static void main(String[] args) { int[] list = {156,344,54,546,767,23,34,64,234,654, 234,65,234,65,87,3,5,76,24,2,3,7, 9,5,34,32,4525,345,0}; sortNumbers(list); for (int i = 0;i< list.length;i++) { System.out.println(list[i]); } } }
|
Method Overriding
|
class A { String name() { return "A"; } } class B extends A { String name() { return "B"; } } class C extends A { String name() { return "C"; } } public class Overriding { public static void main(String[] args) { A[] tests = new A[] { new A(), new B(), new C() }; for (int i = 0; i < tests.length; i++) System.out.print(tests[i].name()); } }
|
Number is Even Or Odd with Average
|
class EvenOdd_Avg { public static void main(String args[]) { int n = Integer.parseInt(args[0]); int cntEven=0,cntOdd=0,sumEven=0,sumOdd=0; while(n > 0) { if(n%2==0) { cntEven++; sumEven = sumEven + n; } else { cntOdd++; sumOdd = sumOdd + n; } n--; } int evenAvg,oddAvg; evenAvg = sumEven/cntEven; oddAvg = sumOdd/cntOdd; System.out.println("Average of first N Even no is "+evenAvg); System.out.println("Average of first N Odd no is "+oddAvg); } }
|
Palindrome
|
class Palindrome { public static void main(String args[]) { int num = Integer.parseInt(args[0]); int n = num; //used at last time check int reverse=0,remainder; while(num > 0) { remainder = num % 10; reverse = reverse * 10 + remainder; num = num / 10; } if(reverse == n) System.out.println(n+" is a Palindrome Number"); else System.out.println(n+" is not a Palindrome Number"); } }
|
Polymorphism
|
class Box { int w,h; void info() { System.out.println("This is a simple box"); System.out.println("width = "+ w + " hieght "+ h); } } class WoddenBox extends Box { int life; void info( ) { System.out.println("This is a Wodden box"); } } class SteelBox extends Box { int wg; void info( ) { System.out.println("This is a steel box"); } } class LargeWoddenBox extends WoddenBox { void info() { System.out.println("This is a Huge Wodden box"); } } class BoxDemo { public static void main ( String ary[ ] ) { Box x; Box b1 =new Box( ); WoddenBox wb=new WoddenBox( ); SteelBox s1=new SteelBox( ); LargeWoddenBox p1=new LargeWoddenBox( ); b1.info( ); wb.info( ); s1.info( ); p1.info( ); } }
|
Prime Number
|
class PrimeNo { public static void main(String args[]) { int num = Integer.parseInt(args[0]); int flag=0; for(int i=2;i< num;i++) { if(num%i==0) { System.out.println(num+" is not a Prime Number"); flag = 1; break; } } if(flag==0) System.out.println(num+" is a Prime Number"); } }
|
Setting Priorities on the Thread objects
|
public class Main { public static void main(String[] args) throws Exception { Thread thread1 = new Thread(new TestThread(1)); Thread thread2 = new Thread(new TestThread(2)); thread1.setPriority(Thread.MAX_PRIORITY); thread2.setPriority(Thread.MIN_PRIORITY); thread1.start(); thread2.start(); thread1.join(); thread2.join(); } } class TestThread implements Runnable { int id; public TestThread(int id) { this.id = id; } public void run() { for (int i = 1; i <= 10; i++) { System.out.println("Thread" + id + ": " + i); } } }
|
All Real Solutions to the Quadratic Equation
|
class Quadratic { public static void main(String args[])throws IOException { double x1,x2,disc,a,b,c; InputStreamReader obj=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(obj); System.out.println("enter a,b,c values"); a=Double.parseDouble(br.readLine()); b=Double.parseDouble(br.readLine()); c=Double.parseDouble(br.readLine()); disc=(b*b)-(4*a*c); if(disc==0) { System.out.println("roots are real and equal "); x1=x2=-b/(2*a); System.out.println("roots are "+x1+","+x2); } else if(disc>0) { System.out.println("roots are real and unequal"); x1=(-b+Math.sqrt(disc))/(2*a); x2=(-b+Math.sqrt(disc))/(2*a); System.out.println("roots are "+x1+","+x2); } else { System.out.println("roots are imaginary"); } } }
|
QuickSort
|
public class QuickSort { /** * Sorts the given list in place. * Worst Case Performance O(n^2) * Best Case Performance O(nlogn) * Average Case Performance O(nlogn) * @param list - int array that you want to sort. */ public void sortNumbers(int[] list) { Quicksort(list, 0,list.length-1); } public void Quicksort(int[] A, int start, int end) { if (start < end) { //we partition the list and get back two lists (lower than pivot, and bigger than pivot) int middle = Partition(A, start, end); //we then do a recursive call to sort out lower numbers than pivot Quicksort(A, start, middle-1); //and we do same with higher numbers than pivot Quicksort(A, middle+1, end); //NOTE: pivot is already in it's place, where he supposed to be (in a sorted list). } } public int Partition (int[] A, int start, int end) { int pivot = A[end]; //we define pivot (the number we will be comparing other numbers to int lo = start-1; // we define low value index for (int hi = start; hi < end; hi++) { //we go throug the list, compare other numbers to pivot if (A[hi] <= pivot) { //if pivot is lower that the current number lo++; //we increase lo value //and exchange current lo with hi (so we will have all lower numbers than pivot on one side) int temp = A[lo]; A[lo] = A[hi]; A[hi] = temp; } } //at the end we put in pivot right inbetween low and high values and we know that pivot element is in the right place. int temp = A[lo+1]; A[lo+1] = A[end]; A[end] = temp; return lo+1; //we return the pivot elements place } public static void main(String[] args) { int[] list = {156,344,54,546,767,23,34,64,234, 654,234,65,234,65,87,3,5,76,24,2,3,7, 9,5,34,32,4525,345,0}; QuickSort qs = new QuickSort(); qs.sortNumbers(list); for (int i = 0;i < list.length;i++) { System.out.println(list[i]); } } }
|
Read File Using Java BufferedInputStream Example
|
public class ReadFileUsingBufferedInputStream { public static void main(String[] args) { //create file object File file = new File("C://FileIO//ReadFile.txt"); BufferedInputStream bin = null; try { //create FileInputStream object FileInputStream fin = new FileInputStream(file); //create object of BufferedInputStream bin = new BufferedInputStream(fin); /* * BufferedInputStream has ability to buffer input into * internal buffer array. * * available() method returns number of bytes that can be * read from underlying stream without blocking. */ //read file using BufferedInputStream while( bin.available() > 0 ) { System.out.print((char)bin.read()); } } catch(FileNotFoundException e) { System.out.println("File not found" + e); } catch(IOException ioe) { System.out.println("Exception while reading the file " + ioe); } finally { //close the BufferedInputStream using close method try { if(bin != null) bin.close(); }catch(IOException ioe) { System.out.println("Error while closing the stream : " + ioe); } } } }
|