Wednesday, 14 September 2016

Write a program in JAVA which accept a String address from user and return true if the address contains pincode at any position and return false if the address does not contain any pincode.

This is another tricky question for the job seekers. Concept is pretty simple here. You have to write a boolean method which will validate the address and return true if it finds PINCODE and return false it does not find.

I am using regular expression to solve this approach.

Sample input:
61 B, Palm Avenue, Joy Bijoy Apartment, 1st Floor, Flat No 1D, West Bengal

Output:
Pincode does not exist.

Sample Input:
61 B, Palm Avenue, Joy Bijoy Apartment, 1st Floor, Flat No 1D, West Bengal 700112

Output:
Pincode exist.

Sample Input:
61 B, Palm Avenue, Joy Bijoy Apartment, West Bengal 700112, 1st Floor, Flat No 1D

Output:
Pincode Exist.

Implemented code:




Sample Output 1:


Sample Output 2:


Sample Output 3:


Tuesday, 13 September 2016

Write a method in Java which accepts a String argument and returns a number of words in it

One of my friends asked me to solve this. User will give the input through console and our program will show the total no of words present in the string.
Input: 
hello, Sourin whats up,what are you doing
Output:
No of words: 8

Implemented code:



Sample Output:



Producer Consumer Problem using Multithreading in JAVA

One of my  friends had faced this problem during his interview in Subex. Concept is pretty simple here. We have two threads here. Consumer thread can not consume anything unless and until Producer Thread has produced.

There are couple of ways to solve this problem. Here I am using volatile Stack to solve this.

Implemented Code:







Sample Output:





Sunday, 4 September 2016

Write a program in JAVA to print EVEN and ODD numbers within a range using multithreading.

I was asked this question in Nokia Networks. You have to write a Java multithreaded program which will take the input range from user and print those numbers as Even or Odd. 


Sample example:
Enter input range: 5
Odd: 1
Even: 2
Odd: 3
Even: 4
Odd: 5

Here is the implemented code:



























Sample Output:



Saturday, 3 September 2016

Java Interview Coding Challanges

 Java Coding is an integral part of JAVA INTERVIEW. If you want to shape your career as a JAVA developer, you will be facing most of java coding challenges which I am going to discuss here. I have faced all of these questions during my java interview with different companies like British Telecom, Edge Verbe, JCPenney, GE, Accenture etc..

I will discuss all of these question one after another in my upcoming blog. I hope that will help you out. To solve these, you should have a basic knowledge on JAVA.

Thanks for following my blog. Happy learning.!!!


Thursday, 5 May 2016

Different annotation in restful webservices

In this tutorial, we are going to discuss different annotations in RESTful webservices in depth. Every annotation has their own meaning.


@GET: Get is used to get some content in XML or JSON format.

  • @GET
  • public String getDetails()
  • {
  • .......
  • }


@PRODUCES: It specifies the type of output method will generate. A method can generate XML or JSON output.

@CONSUMES: It specifies the MIME type that our web service can consume.

  • @GET
  • @PRODUCES("application/xml")
  • @CONSUMES("application/xml")
  • public Person getPersonDetailsInXML()
  • {
  • ........
  • }


  • @GET
  • @PRODUCES("application/json")
  • @CONSUMES("application/json")
  • public Person getPersonDetailsInJSON()
  • {
  • ......
  • }


@PATH: It specifies the URL path on which method is invoked.

  • @GET
  • @PRODUCES("application/json")
  • @CONSUMES("application/json")
  • @PATH("/getPersonDetails")
  • public Person getPersonDetails()
  • {
  • ......
  • }


@PATHPARAM: PathParam annotation is used to bind some value with argument so that we can get dynamic result. For example we want to fetch the person details based on person id. 

  • @GET
  • @PRODUCES("application/json")
  • @CONSUMES("application/json")
  • @PATH("/getPersonDetails/{personId}")
  • public Person getPersonDetails(@PathParam("personId") String personId)
  • {
  • .....
  • }

@POST: Post annotation is used to insert data. For example if we want to create a new Person record then we should use @POST .


  • @POST
  • @PRODUCES("application/json")
  • @CONSUMES("application/json")
  • @PATH("/createPerson")
  • public Person createPerson(@RequestBody Person person)
  • {
  • ......
  • }


@RequestBody annotation is used to bind a Object.

@PUT: Put is used when we need to update the data which is already there in DB.


  • @PUT
  • @PRODUCES("application/json)
  • @CONSUMES("application/json")
  • @PATH("/editPerson")
  • public Person editPersonDetails(@RequestBody Person person)
  • {
  • .........
  • }


@DELETE: It specifies the delete operation based on path variable. For example we will delete the person object based on personId.


  • @DELETE
  • @PRODUCES("application/json")
  • @CONSUMES("application/json")
  • @PATH("/deletePerson/{personId}")
  • public Person deletePerson(@PathParam("personId") String personId)
  • {
  • ......
  • }


Guys, these are some of basic annotations which are used frequently in restful application. Please keep in mind that PUT is used to update the data and POST is used to create a fresh data in DB. Many people get confused between PUT and POST.

Hope this article will help you. I would love to hear feedback. Thank you.