Skip to main content

Posts

Showing posts with the label java

Binary Search Tree in Java implementation (reference based, dynamic memory)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 import java.util.Scanner ; class BST { static BST . Node root = null ; public void insert ( int num ) { if ( root == null ) { root = new BST . Node ( num ); } else { // root node is not empty BST . Node temp = root ; while ( temp != null ) { if ( num <= temp . getVal ()) { if ( temp . getLeft () != null ) temp = temp . getLeft (); ...

Comaparison between ParallelSort and Sort in JAVA

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 import java.util.ArrayList ; import java.util.Arrays ; import java.util.Random ; public class sortvsparllelsort { public static void main ( String [] args ) { Random random = new Random (); System . out . println ( "Comparison between Sort and Parallel Sort" ); ArrayList < Integer > arraylist = new ArrayList <>(); for ( long i = 1 ; i < 10000000L ; i ++) { arraylist . add ( random . nextInt ( 900 )); } System . out . println ( "ArrayList has been initialised" ); Integer a []= new Integer [ arraylist . size ()]; Integer b []= new Integer [ arraylist . size ()]; System . out . println ( "Both normal arrays are initialised" ); a = arraylist . toArray ( a ); b = Arrays . copyOf ( a , a . length ...

JAVA program to Calculate Factorial using Function Interface (involve lambdas )

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import java.util.Scanner ; import java.util.function.Function ; public class fact { public static void main ( String [] args ) { Function < Integer , String > fact =( n )-> { String res = "Factorial of " + n + " is=" ; double a = 1 ; for ( int i = 1 ; i <= n ; i ++) a *= i ; return res + a ; }; Scanner in = new Scanner ( System . in ); System . out . print ( "Enter the number :" ); int num = in . nextInt (); System . out . println ( fact . apply ( num )); } }

Literals in JAVA notes

Literals: ( Java is case sensitive language except some cases: l or L (explicit conversion for long) b or B (for binary form representaion preceded by 0 ex:0B or 0b) f or F (for denoting explicitly float ) A-F or a-f (range in Hexadecimal Notation) x or X (for denoting Hexa form preceded by 0 : ex 0X or 0x) ) A) Integer Literals: i) Decimal Representation:10 ,20 ,40 ii) Hexadecimal Literals:0x123A , 0x 343F iii) Binary Representation: 0b101110 iv) Octal Representation: 0117, 01121 v) (underscore Representation): 1_2__3 ,12_3,1_2_3_4 _ is just a separator which is removed by the compiler. NOTE: 1.underscore should come only between the numbers. 2. We can have more than one underscore also in between the numbers. 3. Compiler skips all the underscore and represents the integer literals in the actual form. It means _ representation is provided for the programmers to represent large number easily...

Thread in Java and Hooking concept

THREAD: Thread is the lightweight Process(uses Shared Memory of application). States: Mansur, [01.10.18 09:35] Thread is a lightweight (uses the shared memory of application) process (program in execution in waiting state). Multiprogramming execution of more than one application (vlc and notepad execute at a time) Multithreading executes more than one threads in a single application. (in NFS 4 cars are executing at a time) States of threads: New state->ready .->running/waiting->dead state To create thread: 1.Extending Thread Class it contains start() method. 2.BY implementing Runnable Interface . Start () method is present in the Thread class. It is used to create Thread. Whenever the Start method of Thread class is called it registers the thread into Thread Scheduler and calls the run method. Recommendation: we should not override start method in implementing class or extending class ot...

Exception Handling in Java

Exception Handling() it is an event that stops the normal flow of execution of the program and terminates the program abnormally. Exception always occurs at runtime. There Is two type of Exception: Checked Exception.:-The exception which is checked by the compiler at the compilation time. Ex: IOException,ClassNotFoundException. Unchecked Exception: The Exception which Cant be checked by the compiler at the compilation time and directly occurs at runtime is called an unchecked Exception. Ex:NullPointerException,StringIndexOutOfBoundsException. Error: Error is the type of unchecked Exception which occurs due to programmers mistake, end-user input mistake, resource unavailability or network problem etc. OutOfMemoryError StackOverFlowError etc. 5 keywords to handle Exception: try catch finally throw throws possible combination: try-catch try-catch-catch try-catch-finally try finally try-...