Tuesday, March 20, 2018

Program of Rotating 2D Array in java


Objective : We will be given one 2D array we need to rotate the array by 90 degree or by given degree.



There can be 2 type of implementation we can do for this problem solution

  1. Novice Solution , we will create one array and fill the array according to the degree provided.
  2. In Place Rotation in which we will change the value in same array only.

Implementation : Using Temp Array for Rotation:

public class R2 {
    public static void main(String[] args) {
        // NOTE: The following input values will be used for testing your solution.
        int a1[][] = {{1, 2, 3},
                      {4, 5, 6},
                      {7, 8, 9}};
        // rotate(a1, 3) should return:
        // [[7, 4, 1],
        //  [8, 5, 2],
        //  [9, 6, 3]]

        int a2[][] = {{1, 2, 3, 4},
                      {5, 6, 7, 8},
                      {9, 10, 11, 12},
                      {13, 14, 15, 16}};
        // rotate(a2, 4) should return:
        // [[13, 9, 5, 1],
        //  [14, 10, 6, 2],
        //  [15, 11, 7, 3],
        //  [16, 12, 8, 4]]
    }

    // Implement your solution below.
    public static int[][] rotate(int[][] a, int n) {
        int[][] rotated = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                rotated[j][n - 1 - i] = a[i][j];
            }
        }
        return rotated;
    }
 }


The Space Complexity of this approach is higher than the new approach , Let see the new Approach.




Second Way : Using Inplace Rotation way

public class R2InPlace {
    public static void main(String[] args) {
        // NOTE: The following input values will be used for testing your solution.
        int a1[][] = {{1, 2, 3},
                      {4, 5, 6},
                      {7, 8, 9}};
        // rotate(a1, 3) should return:
        // [[7, 4, 1],
        //  [8, 5, 2],
        //  [9, 6, 3]]

        int a2[][] = {{1, 2, 3, 4},
                      {5, 6, 7, 8},
                      {9, 10, 11, 12},
                      {13, 14, 15, 16}};
        // rotate(a2, 4) should return:
        // [[13, 9, 5, 1],
        //  [14, 10, 6, 2],
        //  [15, 11, 7, 3],
        //  [16, 12, 8, 4]]
    }

    // Implement your solution below.
    public static int[][] rotate(int[][] a, int n) {
        // n/2 gives us floor(n/2)
        // and n/2 + n%2 gives us ceiling(n/2)
        for (int i = 0; i < n / 2 + n % 2; i++) {
            for (int j = 0; j < n / 2; j++) {
                int[] tmp = new int[4];
                int currentI = i;
                int currentJ = j;
                for (int k = 0; k < 4; k++) {
                    tmp[k] = a[currentI][currentJ];
                    int[] newCoordinates = rotateSub(currentI, currentJ, n);
                    currentI = newCoordinates[0]; currentJ = newCoordinates[1];
                }
                for (int k = 0; k < 4; k++) {
                    a[currentI][currentJ] = tmp[(k + 3) % 4];
                    int[] newCoordinates = rotateSub(currentI, currentJ, n);
                    currentI = newCoordinates[0]; currentJ = newCoordinates[1];
                }
            }
        }
        return a;
    }

    public static int[] rotateSub(int i, int j, int n) {
        int[] newCoordinates = new int[2];
        newCoordinates[0] = j;
        newCoordinates[1] = n - 1 - i;
        return newCoordinates;
    }
 }

I hope this will help you in solving the problem, If you like this post , please share with your friends and colleagues.

Thanks for reading
Noeik 

Monday, March 19, 2018

What is Classloader in java ?


Classloader is very interesting topic in java , Every thing in  java is made up of classes only , So how to load these classes is one of the important aspect to learn.
When a program executed , JVM need to load all the classes either created by the programmer or the system classes to executed the program these loading of classes is done by classloader.



Also see Difference between JDK , JRE & JVM 

Type of ClassLoaders :

There are three different type of classloader in java , as there are basically 3 type of classes exist in java.

  • System classloader - Load all the classes from classpath.
  • Extension classloader - Load all the classes from extension directory
  • Bootstrap classloader - Load all the java core classes

Order of Execution of Classloader ?

Below is the Order of Classloader in which classloader are executed.
  1. First JRE ask the Classloader to load this particular class , First Executed Classloader is System classloader , which find the class in CLASSPATH if found will load the class , if not go to point 2.
  2. If the class is not found in classpath , then extension classloader will be called , it checks the class in Extension directory if found will load the class if not go to point 3
  3. If class is not found in extension directory , the Bootstrap classloader will be called , it checks the class in java core classes , if not found , then ClassNotFoundException will throw otherwise it return the class instance.
Above is the hierarchy of the Classloader to be executed.

If you like this article please share it with your friends and colleagues.

Thanks for reading
Noeik




Sunday, March 18, 2018

Java 8 Features with Examples

java 8 features with examples-programinjava

Java 8 was a huge release from this development platform. The Java programming model went through a major upgrade with this release along with an evolution of the libraries and JVM. 
There was a significant increase in the overall ease of use, performance and productivity. Java 8.0 was officially released in 2014.
Also see : Java 9 Features with Examples

Wednesday, March 14, 2018

Difference between JDK ,JRE and JVM


This is one of the very basic java interview question for specially junior java developer , as everyone heard about JDK JRE and JVM but some of the people don’t exactly know what are these actually.
Today we will understand these three words and whats there meaning and how they are different from each other.

What is JDK ?
JDK Stands for Java Developement Kit which is required by the Java Developer for Development of application . JDK consist of JRE along with some Utitlies like java compiler and other library files with it like interpreter , archiever (jars).
JDK is basically provide the development environment to java developers.
JDK Current Version is – 1.9  10

What is JRE ?
JRE Stand for Java Runtime Environment , That means It provide the runtime environment for the Java application to run on the system. It is just for running the java application , if you dont have any JDK in system and you just want to run the java application the minimum requirement is to have JRE
Current Version of JRE is 1.9  10

What is JVM?
JVM is Java Virtual Machine , It is the heart of the java programming language. Its main responsibility is to convert the java byte code to machine specific code using native apis, It is platform dependent and it perform all the memory management , garbage collector work if JVM crashes the JAVA program will be stoped working.




Let see the Difference between JDK & JRE & JVM


JDK
JRE
JVM
It contains all the Debugging tools, development tools for application development along with JRE as well
It contains the JVM along with the Other java binaries for java program to be executed
It is the heart of the java programming language , responsible to convert byte code to machine codes.
It is for developer
It is for non-developers
It required for both and is platform dependent



Also see : Top 10 Interview Questions for Java developers
There are so many other times as well to see but for the simple difference , mentioned above are the important differences.

We have also heard about JIT( Just in time ) Compiler?

JIT is not JVM , its very confusing for the student to uderstand , but the simple concept is JIT is in JVM A Just-In-Time (JIT) compiler is a feature of the run-time interpreter, that instead of interpreting bytecode every time a method is invoked, will compile the bytecode into the machine code instructions of the running machine, and then invoke this object code instead. Ideally the efficiency of running object code will overcome the inefficiency of recompiling the program every time it runs

Thanks for reading
Noeik

Tuesday, March 13, 2018

Top 10 Technology Trends of 2018

2018 will show make us believe that machines are truly taking over. To help us of course! They will do all the tedious and boring mundane tasks leaving us to do the actual challenging work that only the human mind can achieve. This will be the year where each and every aspect of human life will be touched by technology. Here are the top 10 technology trends that I feel will dominate this year.


1)Deep Learning


The initial goal of Machine Learning(ML) was AI (Artificial Intelligence). This objective is being achieved, now that we are moving towards Deep Learning. As larger neutral networks are trained with increasing amount of data there is an increase in their performance. The earlier learning techniques used to result in a performance rise and then continue at the same level as the amount of data increases.
Some of the redefining applications include

  • Adding colours to Black and White images automatically
  • Instant visual translation
  • Automatic Sentence and text generation with all punctuations
There are many more applications as Deep Learning spreads and more and more tasks, not just the repetitive and mundane ones will start getting automated.

Some Applications where Deep Learning used
Amazon, Google, Tesla, Microsoft, Facebook and IBM are the major companies that have invested significantly in ML.

2) Bots (Robotics)


Another term that has gained popularity and is on almost everyone’s lips is Bots. We are already used to the basic ones that just provide scripted dialogue and information in FAQ format. The Bots are getting updated as we speak, with abilities that will make us doubt a life without their assistance. They will have the capacity of replying to human dialogue, they will improve with repeated usage through machine learning and also may be able to respond to human emotions.

Ideas2IT is a company that specialises in chatbots. Major companies like Amazon, Google and IBM have also ventured into this domain with Amazon Echo, Google Home smart speakers respectively from Amazon and Google and NLP/AI platform IBM Watson.

3)Augmented Reality (AR)/Virtual Reality (VR)


When digital information is integrated with a user’s environment it results in Augmented Reality. Using the existing environment, information is overlaid on top of it. The best example of AR from daily use is the GPS(Global Positioning System) in a Smartphone that shows the exact location of a person.

In Virtual Reality, the user is transposed to a totally new environment. It can be achieved by putting a VR headset over your eyes. Once this is done, be ready to get blown away by the kind of experience it will give to all your senses. It is a mind-blowing experience.
Companies working on this technology
Some of the major players in this are Snap, NVIDIA, Facebook, Google among many others.

4)Blockchain


There has been a significant rise in Cryptocurrencies the past year. This has lead to the world taking notice of the Blockchain as a technology whose application is not restricted to just the Bitcoin. Industry heavyweights have also taken notice of Blockchain technology and are investing heavily in its usage and adoption. If it will replace the client-server architecture for good is yet to be seen.


Companies working on blockchain
Some of the major companies in the Blockchain industry are Ethereum, Ripple, Coinbase, IOTA, and OmiseGo.

5)Internet Of Things (IoT) and BIoT(Blockchain IoT)



Internet of Things (IoT) has evolved in this past year with tremendous results. By adding sensors to everyday connected devices around the house, we are able to control them with just a voice command. But with the addition of Blockchain to IoT, it will tremendously reduce the chances of getting hacked. With BloT, companies will be able to track their remote warehouses for example with great ease and they will always have updated real-time information. Companies working on this technology:
IBM is the leader when it comes to the internet of things. The others in the race includeIntel, Google, Microsoft and Cisco. Ericson, Facebook and Qualcomm are also some of the major names in the race.

6)Smart cars/Assisted transport





Fully assisted transport as in a totally autonomous vehicle are still a distant reality, there is a significant rise in assistance in vehicles in the form of video recognition, assistance in parking, alerts for obstacles or lane discipline. As the use of Machine Learning and Deep Learning continues to grow in this industry, there will be some major leaps in assisted transport.


All major car manufacturing companies have entered into the smart car race including BMW, Tesla, Daimler, Tesla,Google GM and Toyota

7) 3D


The addition of the 3rd dimension has changed the dynamics of every industry that it has touched. From motion pictures to AR/VR and 3D printing, it is a revolution in itself. 3-D printing has proven to be a game changer in the world of Aerospace, Defence and Surgery. A team of plastic surgeons, back in 2014, achieved an extraordinary feat by restoring a 3-year-olds deformed skull to its original shape using a 3D printer.
The 3D printing market has only risen exponentially since then with companies like Stratasys and Optomec have partnerships and alliances in 3D printing in India.

8) Wearable Technologies


Wearable Technology is everywhere right from devices that are worn as accessories to the ones that are implanted. Micro-controllers have been embedded on to everything from clothing to jewellery. The most significant and common of all wearable devices are the smartwatches. Fitness bands are a close next in the list with as they are handy and provide a lot of information on your daily fitness activities. Smart shoes, smart clothes, smart jewellery all these may become very common in the years to come.

Apple, Adidas, Fitbit, Google, Garmin, Nike, Jawbone are few of the major vendors in wearable technology products.

9) Humanized Big Data




Big Data is nothing but an analysis of huge data sets to find certain patterns or trends that are connected to human behaviour. These patterns sometimes cannot be interpreted by people other than data-scientists to actually take some concrete actions based on them. This is where humanizing Big Data will come in wherein anyone will be able to access and apply the information resulting out of Big Data analysis. This will provide relevant and useful information which anyone is capable of interpreting that was previously only available to IT.
The major p[layers in Big Data are IBM, Microsoft, Google, VMWare, Palantir and PWC.


10) Everything on Demand


We are getting more and more impatient and the technological trend of “instant everything” is adding to it. Restaurants and retailers are already in the race for “who makes the quickest delivery”. There has been a significant rise in the 1-day delivery advertisements by e-commerce giants to attract and retain consumers. IT services are also improving the infrastructure to meet the demands of this quick delivery of goods.

This year holds a lot of promise in disruptive innovations. These are the trends that we should look out for as 2018 unfolds.
All major online learning sites like coursera, udemy provide courses in all these technologies.

If you thing there are other technologies which can be in the list of Top 10 trends of 2018 , Leave us a comment will review and update the list accordingly.
If you like the article please share it with your friends and colleague.

Thanks for reading











Monday, March 12, 2018

Loops in java

You remember that last week we discussed the three forms of programming:
  1. Sequential Programming
  2. Selective Programming
  3. Iterative Programming
Since we started these tutorials, we’ve been dealing with sequential programming. The flow of sequential programming is usually downward, from top to bottom, and each and every line of code is executed unless you tell the program not to.
Before move further Let see the Previous Tutorials

Friday, March 9, 2018

Conditional Logic in java


Before we see Conditional Logics lets first see the Previous topics in the series of tutorials

Now we’ve been running a lot of programs since I started these tutorials. That should be… a few weeks ago? Six, maybe? I’m not exactly sure. What I am sure of is that by now you’re becoming comfortable with Java programming, and you’ve probably made a few small programs of your own initiative. (If you haven’t been doing this, then you probably should start doing so. It’s great programming practice).
Since we started these tutorials… six…seven weeks ago…that’s not important right now…we’ve only being working with one form of programming called Sequential Programming. Sequential Programming is basically when the code runs every line one by one, from top to bottom. It’s a very linear form of programming, and it is built to complete a particular task, and get one particular set of results.
Of course, not all programs work sequentially. You might often want a code to only do something when another thing as happened (when a condition is met). This is done with Selective Programming. At other times, you might want your code to do something more than once until some other conditions are met. This is done with Iterative Programming.
For the sake of this tutorial, we’re going to deal strictly with Selective Programming.
Selective Programming/Conditional Logic
Conditional Logic consists mainly of the use of IF statements. An if statement is basically saying that if this is true, or this is false, then this should happen OR if this is greater than this and less than this, then this is equal to that. (That should be not confusing at all).
The standard structure of IF statements in Java (and most other languages) is this:


if(condition) {
     //code
}

Let’s take a quick example. Open a new project or class and type this down:


boolean answer = true;
 if (answer) {
   System.out.println("The answer is true");
}

Run it and your code should give you this:
Okay, if we’re okay with that, let’s do a walkthrough. We started by creating a Boolean value named answer (remember from one of our last tutorial, we said that Boolean data types saved only two values: true or false). In this case, we saved the value true into the Boolean. We then proceeded to write an IF statement, we put that answer mist be equal to true in the conditions bracket. In the curly brackets, which is where our actual program goes, there’s a print statement, that is set to print to “The answer is true” on the output window.
All that is computer for: “If the answer is true, then tell us that the answer is true.”
You know when you’re playing a game, and when you make a particular decision, like moving left, and something happens, like a boulder falls on you and kills you. Yeah, it uses this basic concept. You could think of the coding like this (don’t type this out):

boolean turnLeft, boulderfall, die;
 if (turnLeft) {
    boulderfall = true;
 }
 if (boulderfall) {
    die = true;
}
Again, this is basically saying that if you turn left, a boulder will fall on you, and you will die. Note that the default value of Boolean is false, so it won’t be true until you set that it’s true. Also, you can put an IF statement after your previous statement. You can even put an IF statement inside an IF statement. This is called a Nested IF.
Now, change the answer variable from true to false, and run it again. When you’re done running it, and can see the output window, you probably ask yourself “What? Why didn’t anything print out?”.
 If you’re asking that question, then your output window probably looks like this:




And the reason there was nothing printed out is because there was nothing to tell the system to print out a message if the answer was true. In fact, there was nothing to tell the system what to do if the answer was true. If you want to do this, you could just add another IF statement.
OR
…you could use a form of conditional logic called…

IF…ELSE Statements

The basic concept of this is: If the answer is true, then tell us it’s true, if not, tell us it’s false. The standard structure of an IF…ELSE is this:

if(condition) {
   //code
}
else {
  //code
}
If you’re wondering why the else doesn’t need a condition, think of it like a last resort. If all the other conditions aren’t met, then the computer just has to do it. Now add this to your code (the answer variable should stay false:

else{
  System.out.println("The answer is not true");
}
Your code should now look like this:


Run it, and you should get the desired result.
Let’s try this out with integer values this time, but before we do this, we’ll have to take a quick look at some conditional operators (also called relational operators, these are the symbols that relate two values to each other):

<  Less Than
>  Greater Than
<= Less Than or Equal to
>= Greater Than or Equal to
&& AND
|| OR
== Equals to/Has a value of
! NOT
I’m sure these are quite self-explanatory. If there’re those who still don’t understand, It’ll all be clear in the next example. What we’ll be making is a programming that scans your age, and tells you if you’re over eighteen or under eighteen. Now, comment out everything and write this out.


     int age = 17;
        if (age < 18){
            System.out.println("You're underaged");
        }
        else{
            System.out.println("You're an adult");
        }

Now, this says: “If the age is under eighteen, then say that the user is underaged. If it’s not (i.e. if it’s 18 and above), then the user is an adult. Now before running it, look at the code. What do you think the result will be? Is it the same as the output in the picture below?


Try changing up the age variable. Make it 21, then 15, then 0, then 68; or something like that. But what if we want to put more conditions, since the IF…ELSE can only take one condition at a time. For example, what if we want a code that will tell if you’re a child when you enter a value within a particular age bracket, then it will tell you you’re an adult if you enter within another age bracket.
Well, you can enable the computer to make more than one of such decisions, with the use of…

IF…ELSE IF Statements

The standard structure for this is:

if(condition) {
  //code
}
else if (condition) {
  //code
}
else if(condition) {
  //code
}
else{
  //code
}
Depending on your code, an IF…ELSE IF can be as long as it needs to be, and can even end with an ELSE, as the default if none of the other conditions are met. Now, delete everything you’ve written and put this:

Now, the first IF will only run if ‘age’ is less than 13. The second IF, or the ELSE IF, contains two conditions: if age is greater than or equal to 13, and if it is less than or equal to 17. The presence of the double ampersand (&&) which means ‘and’, tells the computer not to run that programme unless both of those conditions are met. If I changed it to this (||) which means ‘or’, the code will run if at least one of the conditions are met.



The third IF says that the code should be run, only if the age is equal to 18 (note the double equal signs. I’ve made that mistake too many times). The last one is just another variation of the second IF, the only difference being the change of values to 19 and 35, the age bracket of adults according to the code. Then there’s the default else; if the input doesn’t meet the conditions, then the user must be old.
Switch up the value of age and run it. Then try modifying the conditions based on your definitions of an adult and a child (people have different opinions of which age determines an adult).

SWITCH STATEMENTS

Called Select Case, for those familiar with Visual Basic, this is another form of Selective Programming. It gives the option to test for a particular range of values, and it’s easier than writing long complex IF…ELSE statements. This is the basic structure:

switch (variable) {
case value:
 //code;
 break;
   default:
 //code;
 break;
}
Let’s take an example. Comment out everything except the variable age and write out this code







Bow every switch statement starts with the keyword switch, followed by brackets. In the brackets, you put in the name of the variable you want to test (in this case, it’s age). You then put curly brackets, and write case, then a value (like 17 for example). After a colon, you put the code you want to be executed when that condition is met. After that is the keyword break, which simply ends that statement.

What this is simply saying is: If age is 17, the do that. Or if it’s 18, then do something else. The keyword default works the same way as an ELSE, in the sense that it only runs when all the other requirements aren’t met.

Run it and you should get this:



Now, I think that’s enough for Conditional Logic, now it’s time to wrap this post up. I’d advise that you build on what we’ve done today. Try using my lessons on user input to make the IF statements more interactive. Also, experiment with other data types.

NB: Strings don’t use the same relationship operators as integers. Instead of this:

if (name == “Daniel”) {

…use this:

if (name.equal(“Daniel”) {

Next week, we’re going to be talking about Loops. We’ll be picking up the pace a bit, so try to keep up.



If you liked the article please share it with your friends and colleagues. 



Happy Learning 

Thanks for reading