Wednesday, February 21, 2018

[Interview Question] Program in java for Array Rotation

Objective : We will be given one array and asked to rotate the array by d no. of time 

ex- Input - {1,2,3,4,5} d=2
Output - {3,4,5,1,2} 
I will Recommend you to please do it by yourself before see the answer.

Approach 1 : Using temp Array

Pseudo code 
  1. We will use one temp array where we will add the element of value d
  2. then we will copy the d+1th element to 0th place d+2th element to 1th element and so on .
  3. Now we will merge the temp array elements at the end of the array.

Approach 2: Rotate by one to left 

Pseudo Code .

  1. We will first store the first element to temp variable.
  2. Move all the element to left by one.
  3. store the temp variable in last place.
Program Code:

Approach 3 : Reversal Algorithm

Pseudo Code

  1. Let suppose we need to rotate the array by d index what we will do is as below
  2. we first reverse the (o,d) element of the array 
  3. in second we will reverse the (d+1 , length) elements of the array .
  4. In third we will reverse (0, length ) element of the array this will give use the required array.

Program Code:

I guess the Code is self explanatory and I you still face any problem and looking for help , Please let me know , will be happy to help

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

Thanks for reading 

Tuesday, February 20, 2018

Making Games With Java- Introduction

  • Need for Speed.
  • Mario Bros.
  • Sonic.
  • Grand Theft Auto.

Admit it, we all love games. Ever since we were little kids, we all remember the first game we ever played. How those tiny, beautiful cartoon things on the screen moved around at your command, getting the power-ups and little coins, beating the big boss at the end. And finally, that epiphany moment when you’re like “I now know why God gave me thumbs.”
Aaaah. The good old days. Before real life was actually a thing.
Growing up, most of us continued to indulge in the art of gaming. Whether it’s testing out the latest EA or Rockstar game, or sitting in your room playing the latest Subway Surfers update and ignoring your mom’s pleas for you to do your homework, gaming has evolved from a mere pastime to a way of life. Scientist also say that gaming actually improves your cognitive ability, allowing you to make better life decisions (take that underpaid guidance counsellor!)
Of course, as a lot of us grow in our gaming lifestyles, most people decide to use their very limited free time to ask themselves one of life’s most pressing questions.
“Won’t it be cool if I made a game?”
Now, if you’re reading this right now, it’s most likely you asked yourself that question and were like, “Of course it is! What kind of stupid question is that?”. I’m also one of those people. I mean won’t it be awesome to make something like this:

Or this:

Or even this:
Or probably even this:

Ok, I admit the last one’s not very impressive in comparison to the others, but you have to admit, that game was fun. Besides, when you start learning how to make games, believe me, you’ll think it’s the most impressive thing you’ve ever seen. I mean, I’m sure you’ve been told that games aren’t made by magic. Well, let me tell you that that is a complete fabrication, an utter falsehood and a disgusting misrepresentation! (basically, it’s a lie). Many have told me that games aren’t made by magic, but I say that they are. They are made by a very new kind of magic, not as old as necromancy, sorcery, or the dark arts, but just as diabolical. We all know this form of black magic, a skill even older than the IPhone 5. An arcane art known only by one name…


Now it’s no lie that programming is quickly becoming one of the most popular occupations in the world. Years ago, when it was still a noble art, only a select few with almost impossibly high IQ’s would choose to drop everything and dedicate their lives to their computers and codes. Now, everybody wants to code, even that girl in our class that probably didn’t even know what a computer could do beside Microsoft Word.
And it’s not like any one can blame them. Games are really just the tip of the iceberg. If you’re a very good programmer, you’ll be able to create so many things. You could create a million-dollar software, or a social network that could put Zuckerberg to shame. You could programme your own robotic butler to do everything for you, or you could just create a mobile game to show off your mad skills to your friends.
The possibilities are endless.
Programming is power. And with power comes responsibility.
Luckily, the complex appearance of most programming languages keeps the riffraff out, but that doesn’t mean anybody can’t code. As long as you’re willing and able to learn the basics, and you put a lot of time into practicing, then you’re well on your way to being a decent programmer.
Now, as a beginner, the best way to learn how to code is by browsing some tutorial sites. (But of course you know this, otherwise you won’t be reading this). This course is for beginners to Java and programming in general; of course those that have some knowledge of programming are welcome. But before you browse for sites, one of the most important questions a beginner should ask themselves is “What language do I learn first?”
With a myriad of programming languages in the world (many that are popular, and many that are largely unknown), sometimes deciding the one to learn is part of the hassle of learning to code. Many believe that starting with a hard language like C will make learning the others easier. I don’t know how true that is, but I know that starting with hard languages can be kind of discouraging for a first timer.
There have been a lot of programming languages created for new programmers; including Python and Visual Basic, two languages that are very good. But in this course, we’re going to deal the one I used to learn programming.

Why Java?

Now that is an excellent question. I’m sure you’re asking yourself that question right now. Since there are thousands of programming languages out there, like C, C++, C#, other languages that don’t start with C like Python, what’s so special about Java? I mean, it’s not even the strongest one out there. Compared to C++, which was used to make Windows, Java, which is mostly used for measly mobile apps and games, must be the scum of the programming earth, right? Right?
No, not right.
I admit Java may not be the strongest language, but compared to others, it’s actually quite simple. I mean have you seen C syntax (If you haven’t, I really envy you.) (Also, if you haven’t, probably means you’re new to programming, so on a quick note, syntax is a fancy word for the words and word structure used in a particular language).  To tell you the truth, Java may not be the best, but it may as well be the best, because it has a lot of advantages over older and comparatively more advanced languages.
In case you’re wondering what said advantages are, don’t worry, I’ve listed them below.
  • Java is platform independent. For those who don’t know, it just means that it is not limited to a particular operating system. Once you write your code, you can run it on Windows, Linux and even Mac OS. With the Believe it or not, most other languages can’t do this. In fact, the only reason it does this is because of something called Java Runtime Environment (JRE), also called the Java Virtual Machine (JVM). We’ll look at this in more detail in our next post. Basically; with it, code can run everywhere. Without it, it can’t run anywhere.
  • Unlike C and C++ which are procedural languages, Java is Object-Oriented. This is more lingo that experienced programmers use to confuse the noobs, and it just means that each individual part of the software (or object) is worked on as a separate unit, making coding easier to implement, read and maintain.
  • Java can easily be configured to the web. In the webbed based world, we’re in now (that’s just begging to be taken over by robots) this comes in really handy. In fact, a lot of websites make use of Java when operating. The reason for this is that Java was created shortly after the invention of the Wed, so the smart guys that made it made it to complement the web. Smart move, I say.
  • Most Android apps and games are made with Java. I said it before that Java run everywhere; did you honestly think that the Android could escape its grasp. In fact, before the Android even knew it would be invented, Java was being used to create mobile games for the Nokia Symbian OS. (Remember when that used to be a thing? Yeah, me neither). I’m sure people thought that Java would die out with the coming of Android, but I guess the Java people up there are a lot smarter than we thought.
  • Java is easier than most languages. It might not seem so at first sight, but it actually is. Java is very easy to learn, especially for those that know the procedural languages like C and C++, because it steals (I’m sorry, “borrows”) most of their syntax, so you don’t have to worry about learning a whole new language from scratch if you already know those and maybe some other languages. Also, like I said earlier, it’s object –oriented, which is simpler and more efficient than those “more advanced” languages.

I could mention more reasons why Java is the best (or at least more preferable) choice for programmers, especially those that are new to programming.
     If you’ve gone this far in this post, it means you didn’t get distracted by some random notification, and you might actually be interested in learning Java. Remember, Java is not for the fainthearted, so if you feel you’re up for it, look out for our next post, were I’ll be talking about how to get everything you need to be a Java wizard (sorry, “programmer”).
In Next Post we will see what all we need for Java to we in my system.

Thanks for reading 

First Rest web service application using spring framework

First Rest API Application using Spring Rest and Springboot?

What is SpringBoot?

Objective : To build an application of User Creation using Rest Services with Spring boot.

We will first create the Spring boot project (see below image)
Now we will download the project and extract and import it into eclipse.
See the Project Hierarachy after all the package created.
Now we need to add below class – It will have all the Endpoints defined for External – This is a DTO class – This is the Entity Class – This is the CRUDRespository interface for User Operations.  This is the user service Interface having all the methods signature for User Operations. – This is the IUserService implementated Class having methods Implementation of User Operations.

For Database as per simplicity we are use H2 Database(it is embedded database )

Explanation of Classes
@RestController is an annotation used to make the Class as a Rest Controller for Spring to identify.
@Requestmapping(<arg>) is annotation is used to provide the identity path for the URL  of this controller.

How to connect database using JDBCTEMPLATE

Arg – the path of the controller

       UserService userService; It will inject the Service class in Controller Class

       public ResponseEntity<JsonUser> create(@RequestBody @Valid JsonUser user ,BindingResult result ,HttpServletRequest request){
              if(user== null)
                     return ResponseEntity.badRequest().build();
               return ResponseEntity.ok().body(userService.createUser(user));

@PostMapping is showing that this method will be taking Post type of calls where the Request Body will be of type JsonUser.
After that we are checking the user is not null ,if it is we will return bad request error.
If not , we will call the service call createuser method with jsonUser object as a argument.
This class is a DTO class
This is Entity Class which will be persisted in Database.

@Entity is used to say that the class is entity class for hibernate.
@Table is used to define the table name for This class , if the table name is same as the class name we don’t need to difine the name otherwise it will become @Table(name=”mytable”)
@Id is to as this is identity attribute of the table which @GeneratedValue to show how the id will be automatically generated.
@Column is used to defined the Column Attributes.
This is Interface which implementes JpaRespository<User,Serializable> that means , It provide all the Db Operations function like save, findAll etc
This is Service Interface having all the User Operations like createUser, getUser, updateUser, deleteUser
This is Service Implementation of IUserService where we are implementing the logic of creating the user using Respository Functionality.

Testing of Application Using Rest Client
Now we need to run the Application and then we will use RestClient like RestLetClient to test the API

Congratulations have creating our first Rest Service Application Using Spring framework.

If you like this article please share it with your friend or colleagues.

Thanks for reading

Monday, February 19, 2018

What is Spring boot ?

Spring boot is one among all the projects of Spring Framework Community , Which is mainly focused on building production ready application right from the initials.

We do not need to configure so many thing in spring boot as it will work on convention then configuration

What does convention over configuration means ?

Spring boot has writing its API in a way that it has pre assumed the placeholder of the different configuration and it assumed that the developer will also place the configuration in that placeholder only.
Example - The file is a property file where if I will write as
Server.port  = 8181
The Spring boot will configurate the tomcat server port as 8181 as server.port is defined in the code of the spring boot apis

So Again what is spring boot ?
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration. (source

What are the features of Spring boot?

  • Create stand-alone Spring applications
    • This Means , Spring Boot makes the Project as a Stand alone Spring application by making making it as Jar.
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  • Provide opinionated 'starter' POMs to simplify your Maven configuration
    • We do not need to define all the dependency , it has provided the starter where all the dependency associated with particular functionality will be automatically downloaded Ex: If I will defined spring-boot-starter-web in pom , it will automatically download the spring-core, spring-aop, spring-web,spring-webmvc etc and all.
  • Automatically configure Spring whenever possible
  • Provide production-ready features such as metrics, health checks and externalized configuration
  • Absolutely no code generation and no requirement for XML configuration

Note : You can also reference the Official documentation of Spring Boot

Spring also has provided Spring Initializr to create the spring-boot compatible structured.
Current Version of Spring boot is 1.5.10
The Official Site of Spring boot project is  

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

Thanks for reading

First Simple Application using Spring Boot

Hello Guys as we all know that spring framework is now a day is very popular in IT industry because of its Features and the APIs it provide , One of them is Spring boot .

Do you know how to do Dependency Injection in Spring 

What is spring boot ?

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration. (source
Lets Quick Start with Create our first spring boot application in java

Note : Pivotel provides custom Eclipse type IDE for Spring projects its called as STS (Spring Tool Suit)

Objective : We are creating one project where I will create one rest endpoint to just hit the site using spring boot.

See below steps 
  1. Provide
               Group Idcom.programinjava.learning
         Name: RestDemo
               Dependencies : Web
      3. Now download The project
      4. After download you will get the dowloaded code in zip , unzip it.

      5. Open Eclipse and Import the Project as Maven Project
    6) Now Below will be the code which you need to write in new class


    7) After all this you just need to run your main class (in my case as java application it will start the tomcat internally

     8)Now we need to check the rest end point we have created
     9)To check that we need to hit below url in browser
    You will see the below spring 

Congratulations you have just completed your simple Application using Spring boot!!

I hope this will help you in understanding the Complete code 
I will add the code on github as well in few days.

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

Thanks for reading 

Thursday, February 15, 2018

Top 10 Core java Frequently Asked Interview Questions for beginner level

Java is leading programming language in market still as well and as the language is famous for enterprise level of development , Most of the company looks for great java developer who have a strong basic core java understanding.

Below are some of the frequently asked questions for fresher level of java developer.

           1)    Which of the following  are valid java identifiers? (choose all apply)
a.    A$B
b.    _hello
c.    false
d.    Java.util
e.    Public
f.     19_abc
           2)    Which of the following are true ? (choose all apply)
a.    short myPet =5;
b.    int myNumber = 5.6;
c.    String theString =”Scruffy”
d.    myPet.length();
e.    myNumber.length();
f.     theString.length();
                                          i.    Line a generate a compiler error
                                         ii.    Line b generate a compiler error
                                        iii.    Line c generate a compiler error
                                       iv.    Line d generate a compiler error
                                        v.    Line e generate a compiler error
                                       vi.    Line f generate a compiler error
                                      vii.    The Code compiles as is.

          3)    What is the output of the following program ?
public class WaterTank {
       private String brand;
       private boolean empty;
       public static void main(String[] args) {
              WaterTank waterTank = new WaterTank();
              System.out.println("Empty "+ waterTank.empty);
              System.out.println("Brand "+waterTank.brand);

a.    Line 6 generates a compiler error.
b.    Line 7 generates a compiler error
c.    There is no output
d.    Empty = false , Brand = null 
e.    Empty = false , Brand =
f.     Empty = null , Brand = null

          4)    Which of the following are legal entry point methods that can be run from the command line?               (Choose all that apply)
a.    Private static void main(String[] args)
b.    Public static final main (String[] args)
c.    Public void main (String[] args)
d.    Public static void test (String[] args)
e.    Public static void main(String [] args) 
f.     Public static main(String [] args)
g.    None of the above

          5)    Given the following classes , what is the maximum number of imports that can be removed and have the code still compile ?
Package company; public class Employee{}
Package company;
Import java.lang.*;
Import java.lang.System;
Import company.employee;
Import company.*;
Public class Company{
Public void print(Employee employee){
a)    0
b)    1
c)    2
d)    3
f)     Does not compile     

            6)    Which of the following are true?
a.    A local variable of type boolean defaults to null
b.    A local variable of type float defaults to 0
c.    A local variable of type Object defaults to null
d.    A local variable of type boolean defaults to false
e.    A local variable of type boolean defaults to true
f.     A local variable of type boolean defaults to 0.0
g.    None of the above   

            7)    Given the following class which of the following line of code can replace INSERT CODE HERE to make the code compile? (Choose all that apply)
Public class Price{
Public void admission(){
a)    Int amount = 9L;
b)    Int amout = 0b101; 
c)    Int amount = 0xE;
d)    Double amount = 0xE;
e)    Double amount = 1_2_.0_0;
f)     Int amount =1_2_;
g)    None of the above;

           8)    Which of the following are true statements ?
a.    Java allows operator overloading.
b.    Java code compiled on windows can run on linux 
c.    Java has pointers to specific locations in memory
d.    Java is a procedural language
e.    Java is an object oriented language 
f.     Java is a functional programming language

          9)     will the below code compile ?
public class JavaBasics {
       public static void main(String[] args) {
              http ://

           10)     What will be the output of the below program ?
public class JavaBasics {

public static void main(String[] args) {
class A{
static class Y{
static String Z ="White";

static C Y = new C();


class C{
String Z="Black";

Now give some Thought on the above question and then see the answers after you finished with your answers otherwise it will not help you 

  1.  a,b,e
  2.  ii ,iv,v
  3. d
  4. e
  5. e
  6. g
  7. b,c,d
  8. b,e
  9. yes it will , as http: will become label and  // will be comment 
  10. Black

If you guys are not able to understand the answer reason , let us a comment will be happy to help 
if you like the set of questions please share it with your friends