Posts

Showing posts with the label CS Fundamentals

How to convert the given string to number in Java?

 In this tutorial, i am going to explain how to convert a given String to Integer in Java with and without using predefined methods of Java. Input: "1234" Output: 1234 Input: "abc" Output: an Exception. Problem:     How to convert the given string to number in Java? Method 1 - Using predefined method of Integer Class: use ParseInt() method of Integer class to convert given String to Integer. The code snippet is showed below. private static int convertToInteger2(String aString) {                 return Integer.parseInt(aString);      }     Method 2 - Without using predefined methods: In this approach, we are not going to use any predefined methods for conversion. we are going to write our own algorithm for this purpose. The algorithm is simple, we are going to iterate through each character of the give String and convert it to digit. we maintain a tempo...

In Detail : A Simple Insertion Sort In Java Along With Generics Implementation

Image
Insertion Sort :     Insertion Sort is an elementary sorting algorithm. It is easy to understand and implement, but is unacceptably slow when there are multiple records to sort. A Real Life Example Illustrating Insertion Sort :     Imagine you have a stack of electricity bills from the past two years & you wish to sort them by date. A fairly natural way to do this might be look at the first two bills & put them in ascending order. Then take the third bill & put it into the right order with respect to the first two & so on. As you take each bill, you would add it to the sorted pile that you have already made. This naturally intuitive process is the inspiration for Insertion Sort. How Insertion Sort Works ?      Insertion Sort starts iteration from second element of the input array/ list. In each iteration the current element is compared with its previous element & swaps, if the previous element is greater tha...

Programming Paradigms

Image
     Paradigm: In science,  a paradigm describes distinct concepts or thought patterns in some scientific discipline. In programming we can distinguish 3 main paradigms. 1. Imperative programming 2. Functional Programming 3. Logic Programming Object Oriented Programming is orthogonal to the above 3 paradigms, in the sense it can be combined well with both imperative programming but also with functional programming or even logic programming. Imperative Programming : Imperative Programming is really about a. Modifying mutable variables using assignments and those will be composed with control structures such as if-then-else, loops, break, continue, return and so on. The most common informal way to understand imperative programs is as instruction sequences for a Von Neumann computer. Von Neumann was one of the pioneers of the computer built first computer around in 1945. Von Neumann computer essentially consists of a processor, memory and then there i...

Difference Between Compiler And Interpreter

Image
Computers can understand only machine level language which can be written using 0's & 1's. It is difficult to write and maintain programs in machine level language. The present languages such as C, C++, Java, Scala all are high level languages which can be understand by humans. Computers can not understand higher level languages. The programs written in high level languages need to converted into machine level language using translators. Translators are just computer programs which accept a program written in high level or low level language and produce an equivalent machine level program as output. Translators are of three types. Assembler Compiler  Interpreter Assembler is used for converting the code of lower level language ( assembly language) into machine level language.  Compilers and interpreters are used to translate the code of high level language into machine language. The high level program is known as source code and the corresponding machine leve...