Create User Defined Linked List with Sorting Facility

In this article you will learn:

  • Create user defined linked list.
  • Use of generics, Enum and Comparable.
  • Basic design principle.

maxresdefault

Problem Statement:

Create custom linked list (i.e. don’t use java’s default linked list) with sorting facility. Sorting facility means data should be inserted in the specified order either ascending or descending.

The Background:

Now days, one of the most famous question is to create your own linked list. Also data should be inserted in some specified order.

Techniques used to impress Interviewer:

  • Followed code to interface design principle- to create generalized API.
  • Use of generics to make code independent of specific data type.
  • Use of ENUM’s to provide type safety.
  • Use of Comparable- to provide sorting facility.

Explore this:

Let’s start with basics. We will create following classes in given order:

  1. Concrete Class: Node- This is most basic part of linked list which contains data and information about next node.
  2. Enum: SortOder- This ENUM class will contain sort orders to provide.
  3. Interface: CustomLinkedList- This will be template creating methods for our linked list.
  4. Concrete class: CustomLinkedListimpl- This will have logic to create our linked list using class node.
  5. ConcreteClass: CustomLLClient- This will client class to show created linked list.

To learn basics of linked please follow this.

The code is as follows:

 

package customlinkedlist;

public class Node<T> {
 
 private Comparable<T> data;
 private Node<T> next;
 
 public Comparable<T> getData() {
 return data;
 }
 public void setData(Comparable<T> data) {
 this.data = data;
 }
 public Node<T> getNext() {
 return next;
 }
 public void setNext(Node<T> next) {
 this.next = next;
 }
}
 
package customlinkedlist;

public enum SortOrder {
 ASC,
 DSC
}
package customlinkedlist;

public interface CustomLinkedList<T> {
 
 Node<T> insert(Comparable<T> data);
}

 
package customlinkedlist;

public class CustomLinkedListimpl<T> implements CustomLinkedList<T> {
 
 private Node<T> head;
 private SortOrder sortOder;
 
 public CustomLinkedListimpl(SortOrder sortOrder) {
 head = new Node<T>();
 head.setNext(null);
 head.setData(null);
 this.setSortOder(sortOrder);
 }
 
 public Node<T> insert(Comparable<T> data) {
 
 Node<T> nodeToInsert = new Node<T>();
 
 if (head.getNext() == null) {
 
 nodeToInsert.setData(data);
 nodeToInsert.setNext(null);
 
 head.setNext(nodeToInsert);
 } else {
 
 Node<T> tempNode = head;
 while(tempNode.getNext() != null) {
 
 if (SortOrder.ASC.equals(this.getSortOder()) && data.compareTo((T) tempNode.getNext().getData()) < 0) {
 break;
 } else if (SortOrder.DSC.equals(this.getSortOder()) && data.compareTo((T) tempNode.getNext().getData()) > 0) {
 break;
 }
 tempNode = tempNode.getNext();
 }
 
 nodeToInsert.setData(data);
 nodeToInsert.setNext(tempNode.getNext());
 
 tempNode.setNext(nodeToInsert);
 }
 return nodeToInsert;
 }
 
 public void printList() {
 
 if (head == null) {
 System.out.println("List is null.");
 return;
 }
 
 Node<T> tempNode = head.getNext();
 System.out.print(tempNode.getData());
 
 while (tempNode.getNext() != null) {
 tempNode = tempNode.getNext();
 System.out.print(" --> " + tempNode.getData());
 }
 }

 public SortOrder getSortOder() {
 return sortOder;
 }

 public void setSortOder(SortOrder sortOder) {
 this.sortOder = sortOder;
 }
}

 
package customlinkedlist;

public class CustomLLClient {

 public static void main(String[] args) {
 
 System.out.println("Integer LinkedList");
 CustomLinkedListimpl<Integer> intcustomLinkedList = new CustomLinkedListimpl<Integer>(SortOrder.ASC);
 intcustomLinkedList.insert(8);
 intcustomLinkedList.insert(3);
 intcustomLinkedList.insert(5);
 intcustomLinkedList.insert(2);
 intcustomLinkedList.printList();
 
 System.out.println("\n\nString LinkedList");
 CustomLinkedListimpl<String> customLinkedList = new CustomLinkedListimpl<String>(SortOrder.DSC);
 customLinkedList.insert("Tango");
 customLinkedList.insert("Alpha");
 customLinkedList.insert("Ram");
 customLinkedList.insert("Romeo");
 customLinkedList.printList();
 }
}

The result is:

 Integer LinkedList</pre>
<pre>2 --> 3 --> 5 --> 8

String LinkedList
Tango --> Romeo --> Ram --> Alpha

What Next:

Suggestions are most welcome. Let me know if there is any doubt as well.

Print this Article:

Printer friendly version of this document is available: Create custom linked list

Happy Reading.

Nirbhaya Bhava!!

Tanzeem.

JVM JRE JDK- What’s the relation

Image

In this article:

  • Learn common confusion among JVM, JRE and JDK.
  • This will help to understand processing of a java class.

The background:

A java programmer writes java file. And its gets executed on any environment. Have you ever thought how? Once programmer done with writing java file, his/her job is done. He/She nothing to worry about what all needed to run java program.  But hold on this is not possible on simple computer. Then how? Time to explore this.

Explore this:

That computer must have JDK (java development kit) which provides all necessary tolls and libraries to write and execute java programs. Inside JDK, there is JVM (java virtual machine) which reads .class files (the converted .java file from compiler). JVM just understand how to read .class files, it takes help from JRE (Java run time enviroment) to execute .class file. Following pictures depicts this–

Image

If you understand set theory, then in that way- JDK is super set  of JRE; and JRE is super set of JVM.

Extra:

One of the most important interview question is why JVM is called virtual? Answer is JDK is a software bundle which contains JVM too. As there is no physical existence of JVM, hence it is virtual.

What Next:

Go in depth of  JVM internals, class loading and garbage collection. This will provide you solid base for writing efficient programs. Providing some links and books which will surely help you. All the very best.

  • JVM internals-

JVM architecture

JVM in concise way

  • Book on Garbage collection and more

Java Performance By Charlie Hunt, Binu John

Till then happy reading,

Nirbhaya Bhava!!

Problem Solving Approach: Production and Non Prod behavior is different

In this article:

  • You will able to answer how to debug issue when production code behavior is not as expected as in non-production.
  • You will able to learn some basic causes due to that same code base behave differently in production and non-production.
  • You will get idea how to approach for this kind of scenario.
  • This will help to resolve production issues in quick time to avoid SLA.

newidea

Scenario:

Some functionality which is working as expected in non-production environment, but does not work in production.

The background:

Some time we come to those situations when production code behavior on some functionality (which was part of current release) is not as expected. QA already gave sign off, UAT also performed their testing, RTP team followed proper migration document. But still that functionality does not work.  What got wrong? In production we can’t enable debug info to check everything. Then how to resolve the issue? How to make production code working as expected? Following may be the solution which are truly based on real time experiences.

Explore this:

Below are the sequential steps we usually follow. At any point of time when gap found stop the analysis and correct the migration document, follow the rest steps to make sure there is no more gap and go for release.

1.       Verify WAR file version

  • Check the WAR file version in production and non-prod where sign off was given.
  • Check the code base. If it is J2EE, verify if the java class is up-to-date in version control system.
  • Check if the above changes are in WAR file.

2.       Missing Property file/Database changes

  • Based on the project management, check the tracking document which contains all the changes required to implement particular changes.
  •  It may be design document or kind of solution approach document.

3.       Enable same level log in Non-prod

  • Based on observation found, sometime few developers write some part of code in debug/info logger level by mistake. And that may also cause this situation where same code base is working fine on non-prod but not in production.
  • To identify this, set the logger level to same level as in Production.
  • Usually Error level is enabled in production and in non-prod, it is debug.
  • Read this article for more info.

4.       Hit Node URL-

  • Production code base usually deployed on more than one server or node (may be same server with different JVM as well). So rather hitting direct URL (like google.com), hit node specific URL (like nodeid:8080).
  • If any node URL gives desired result that means there is some problem with server and/or deployment process. May be temp directory did not get cleared in case of web logic server. Clear the temp directory and restart the application.
  • Check whether startup parameter is in sync.

5.       Stop external scripting on specific page

  • To get the user experience and/or to make changes in site look and feel using campaigns, companies are using external scripts like Adobe TestNTarget etc.
  • These script runs on our application core data returned. Hence these can be also responsible for the behavior of production code base.
  • To make sure if external scripts are creating problem, just stop on specific pages where desired functionality is not in working in production.

What Next:

  • Just apply above tips; these are based on personal experiences. May be you came across different scenario. Then please share it.
  • In next article planning to share real-time tools to resolve production issue.

Till then Happy Reading,

Nirbhaya Bhava!!

Error when Starting Eclipse: 64 Bit on Windows 7- 64 Bit

Problem: Error occurred while starting eclipse 64 Bit on Windows 64 bit.

Errors:

  1.  Failed to load the JNI shared library “C:/{some library}/bin/client/jvm.dll”`.
  2.  Java was started but returned exit code 13

Image

Quick Solution:

  1. Download correct JDK and eclipse for windows 64 bit.
  2. Make sure all installation goes in “Program Files”.
  3. Correct the PATH variable for Java.
  4. Update the eclipse.ini for following value-

    -vm
    {path_to_64bit_java}\bin\javaw.exe

Explanation and Points to Remember:

  1. Eclipse looks for working and compatible java, which is specified above in step 4.
  2. Remember, irrespective of OS bits, eclipse and JDK must be of same bits only. Obviously OS bit should be higher or equal to JDK and eclipse as well. For example: You can not set up 64 bit eclipse on 64 bit OS using 32 bit JDK.
  3. Download JDK6 (or the JDK version on which you want to run) from JDK download. Select top most hyper link- at this time it is Java SE Development Kit 6u45. Accept the license agreement click to download 64 bit JDK as marked in red—Image
  4.  As well concern to the folder name convention, X86 is representation for 32 bit windows.
  5. While installing JDK make sure default installation directory is “Program Files” rather “Program Files (x86)”.  And if it is not you have downloaded 32 bit JDK.

Hope this solves the problem. If not just let me know your problem through comments or mail.

Happy Reading.

Nirbhaya Bhava!!

Design Principle vs.Design Pattern: Describing OOP elements

In This article:

  • You will able to learn basic elements of a Object Oriented Design.
  • You will able differentiate design pattern and design principle.
  • At the end you will get some useful links and books to read.
  • Finally you will bag at least one more interview question and their clarification.

The Background:

  • Why these definitions and descriptions needed-

Most of the time developers have to work on enhancements based projects, where they have to understand the code which is already written by some other people.  The understanding of code base takes time based on project architecture. Then during analysis of code a query always tickles, in one corner of the mind of the developers, “why-this-extra-class-needed” or some other similar lines.  This type of queries came in mind when one is not aware of the paradigm used and patterns. There are several books and sites you will find on this but here you will get the base of a good design. This is not so fancy, but if you will burn it in your mind- your understanding will be much deeper.

Explore this:

Elements of OOP

Elements of OOP

OOP—

  • Stands for Object Oriented Programming.
  • Object-oriented programming (OOP) is a programming paradigm that represents concepts as “objects” that have data fields (attributes that describe the object) and associated procedures known as methods. Objects, which are instances of classes, are used to interact with one another to design applications and computer programs.
  • OO basics are-
  1. Abstraction
  2. Encapsulation
  3. Polymorphism
  4. Inheritance
  • Please avoid writing it as OOPs or OOPS. Please see this article.

OO Design Principle—

  • OO principle is a set of guidelines that ensures OOP concept.
  • Based on the OOP concept, this defines ways to design better way, a better design.
  • The basic OO design principle is SOLID.
  • SOLID is known as-
  1. Single Responsibility Principle

         A class should have only one reason to change.

     2. Open Close Principle

        Software entities like classes, modules and functions should be open for extension but closed for modifications.

     3. Liskov’s Substitution Principle

         Derived types must be completely substitutable for their base types.

     4. Interface Segregation Principle

         Clients should not be forced to depend upon interfaces that they don’t use.

     5. Dependency Inversion Principle

        High-level modules should not depend on low-level modules. Both should depend on abstractions.

        Abstractions should not depend on details. Details should depend on abstractions.

OO Design Pattern-

  • A Design pattern provides a general solution to a design problem.
  • Please note “design pattern” can be applied to noon object oriented word too. So A OO design patterns (OODPs) are those which provide a general solution to object oriented design based OO principle.
  • Design patterns are discovered, not invented.
  • There are several ways to define OODPs and most famous one is BSC [Behavioral Structural Creational].
  • Understanding BSC-
    • To create objects one need of classes. This is start of design.
    • A class must have well defined attributes. The design solution made to compose class structure is known as Structural patterns.
    • Now you have class, to create objects one needs to create object(s). The design solution made to instantiate a class or classes is known as Creational patterns.
    • Once object instantiated, think of objects interactions. So those design solutions which are concerned with object interactions are known as Behavioral patterns.

Best Practices-

  • Best practices can be used in OODPs to avoid business errors or run time errors.
  • There can be multiple techniques to do some tasks but to do in efficient and effective way can be said Best practices.
  • Example-

In java there are multiple ways to iterate a map, but if you want to get just list of keys do not use iterator or entryset, but use keyset.

Type safety can be another example of best practices.

Summary-

So by understanding above definitions we can conclude that design patterns uses to implement an OO design problem. These patterns use OO Principles and Best practices to ensure OO paradigm with highly scalable and efficient design.

What Next:

Just start with basics principles and design. If you don’t know which way to go, then follow the links and books given in useful link. Don’t just dive in technologies; first grasp the basics of all these technologies.

Useful Link:

  • Notes on basic design principle and pattern-
  1. http://www.oodesign.com/
  2. http://hillside.net/
  • Some more design principle can be found at this site
  • The best book to start with design pattern is-
  1. Head First Design Patterns.
  2. Amazon Link
  3. Flipkart link

Print this article:

Happy Reading,

Nirbhaya Bhava!!!

OOPS vs. OOP – What “s” stands for?

In this article:

  • Explores the meaning of OOP and OOPS.
  • At the end of this article you will be able to differentiate OOPS and OOP.
  • This may be part of your interview question on Design pattern.

The background:

This is most common misunderstood short form related to object oriented paradigm. If somebody asks which paradigm you use or on which paradigm Java works. We generally answer it as OOPS or OOPs. And here we do mistake. Also this came as a conundrum when interviewer asked to my buddy what “s” stands for.

Explore This:

First grab the full form, then I will explain it further—

Differentiate OOPS and OOP

Differentiate OOPS and OOP

Oops!! Yes. This is the sound you must whisper. Well OOP you already know, following is the definition of OOPS (actually oops – not in CAPS)

Definition of oops

Yes this is official definition of oops.

What Next:

So next time, to break the awkward silence between you and another developer, you have got a nice    topic to break the ice.

Printable Doc:

OOPS vs OOP

Happy Readings,

Nirbhaya Bhava!!!

Spring – The Context: ApplicationContext vs. BeanFactory

This article describes the difference between ApplicationContext and BeanFactory in Spring framework.

- BeanFactory is recommended for lightweight framework like — Mobile and Applet.

- BeanFactory is core component of Spring IoC container.

- As ApplicationContext extends BeanFactory so ApplicationContext is more powerful than BeanFactory (like AOP, Internationalization etc.)

con1

-          You can configure your beans in the Spring IoC container through XML files, properties files, annotations, or even APIs.

-          Container reads these beans by using BeanFactory (or ApplicationContext)

-          Let’s say you define the beans in XML file, say applicationcontext.xml.

-          Now BeanFactory has to read applicationcontext.xml to load the beans in container.

-          For this BeanFactory needs an interface that can provide the xml file o BeanFactory.

-          This interface called resource object.

-          Sample:

context3

-          In case of ApplicationContext, the resource object is implemented internally, so as use We don’t  need of resource object.

context4

cntext2

Hope this will clarify the basic difference.

Happy Reading,

Nirbhaya Bhava!!

Follow

Get every new post delivered to your Inbox.