Sunday, March 9, 2014

20 Things Every Java Developer Must Know!

The term 'java developer' covers a wide range of individuals. It starts with fresh graduates looking for jobs and goes up till experienced Java Enterprise Edition developers. There are a wide range of concepts and skills that Java developers must posses in order to be important in the industry. It is highly advisable to know the language fundamentals thoroughly, rather than specific frameworks or syntax.
College Graduates:
A college graduate with no job experience is a fresh mind waiting to become a true Java developer. There are certain things that you need to understand as a fresher in the industry to get your dream job.
  1. College graduates must know how a Java Virtual Machine works for starters. Concepts like Platform Independence, Garbage Collection, class files etc must be clearly understood. 
  2. They must be aware of the many Object Oriented Programming Concepts implemented in Java. 
  3. They must know what is multi-threading. 
  4. Basic understanding of data types and java.lang classes such as String, Math, System etc. is advisable. 
  5. They must understand the concept of Swing/AWT event based programming. 
  6. Knowledge of Java Collection framework. 
  7. They must be clear in the concepts of servlets and JSP.
Java Developers:
You may be a professional Java developer, but that doesn’t firmly guarantee you a job.
  1. You must understand design patterns and its usage in Java.
  2. You must understand improvements on language from major version changes such as Generics, Annotations, Enums etc.
  3. Must know coding conventions.
  4. Understand the concepts of Build tool (Ant) and Project Management Tool (Maven).
  5. Understand version control systems, for instance CVS, SVN, Perforce, Clearcase etc.
  6. You must know about Apache common libraries among other open source libraries.
  7. Understand Unit testing and Continuous Integration Tools.
  8. Highly advisable to have fundamental understanding of XML.
You must also understand business layer frameworks like Spring.

Circular Queue ADT using Linked Lists.

Hi guys here is a code on Circular Linked List in Java! Program :

import java.util.Scanner;
interface CllAdt{
public void insert();
public void delete();
public void display();
public int len();
}
class CNode{
int data;
CNode link;
}
public class CQSll extends CNode implements CllAdt{
CNode first;
Scanner s=new Scanner(System.in);
public CQSll(){
first=null;
}

public void insert(){
System.out.println("Enter the position to insert a node :");
int pos=s.nextInt();
System.out.println("Enter the data into a node :");
int ele=s.nextInt();

CNode p=new CNode();
p.data=ele;
p.link=null;
CNode temp;
if(pos<1 || pos>len()+1)
System.out.println("Invalid position and insertion is not possible");
else if(pos==1&&len()==0)
{
first=p;
first.link=first;
System.out.println("Entered position is(1st):"+pos);
}
else if((pos==1)&&(len()!=0)){
temp=first.link;
while(temp.link!=first)
temp=temp.link;
p.link=first;
first=p;
temp.link=first;
System.out.println("Entered position is(1st):"+pos);
}
else if(pos==len()+1){
temp=first;
while(temp.link!=first)
temp=temp.link;
temp.link=p;
p.link=first;
System.out.println("Entered position is(Last) :"+pos);
}
else
{
temp=first;
for(int i=1;i<pos-1;i++)
temp=temp.link;

p.link=temp.link;
temp.link=p;
System.out.println("Entered position is:"+pos);
}
}

public void delete(){
System.out.println("Enter the position to delete a node :");
int pos=s.nextInt();
CNode p,temp;
if(pos<1||(pos>len()+1)||(len()==0))
System.out.println("Invalid position and deletion is not possible");
else if(pos==1&& len()==1){
temp=first;
first=null;
System.out.println("Only one i.e. Deleted item:"+temp.data);
}
else if(pos==1&&len()!=1){
temp=first.link;
while(temp.link!=first)
temp=temp.link;
p=first;
first=p.link;
temp.link=first;
System.out.println("From begininning i.e. Deleted item :"+p.data);
}
else if(pos==len()){
temp=first;
for(int i=1;i<pos-1;i++)
temp=temp.link;
p=temp.link;
temp.link=first;
System.out.println("From Ending i.e. Deleted item :"+p.data);
}
else{
temp=first;
for(int i=1;i<pos-1;i++)
temp=temp.link;
p=temp.link;
temp.link=p.link;
System.out.println("Deleted item :"+p.data);
}
}

public void display(){
CNode temp;
if(first==null)
System.out.println("Circular Linked list is empty :");
else{
System.out.println("List of elements :");
System.out.println("First->");
temp=first;
System.out.println(temp.data+"-->");
temp=first.link;
while(temp!=first){
System.out.println(temp.data+"-->");
temp=temp.link;
}
System.out.println("End");
}
}
@Override
public int len(){
int count=1;
CNode temp=first;
if(temp==null) return(0);
else if(temp.link==first) return(1);
else{
while(temp.link!=first){
count++;
temp=temp.link;
}
return count;
}
}
public static void main(String args[]){
System.out.println("Circular Linked List ADT");
Scanner s=new Scanner(System.in);
CQSll c=new CQSll();
String ch="y";
while(ch.equalsIgnoreCase("y")){
System.out.println("\n CLL Operations :");
System.out.println("1. Insert 2. Delete 3. Display 4. Length 5. Exit :");
System.out.println("Enter the option :");
int opt=s.nextInt();
switch(opt){
case 1: c.insert();break;
case 2: c.delete();break;
case 3: c.display();break;
case 4: System.out.println("Length of CLL is :"+c.len());
case 5: System.exit(0);
case 6: System.out.println("Invalid option!");
}
System.out.println("\nDo you want to continue(y/n)");
ch=s.next();
}
}
}

Saturday, March 8, 2014

Linear Queue ADT using Single Linked List

Program :

package adsa;
import java.util.Scanner;
/**
*
* @author sudeep
*/
interface LQSll{
void insert();
void delete();
void display();
}
class QNode{
int data;
QNode link;
}
class QueueSll extends QNode implements LQSll{
QNode front,rear;
Scanner s=new Scanner(System.in);
public QueueSll(){
front=rear=null;
}
public void insert(){
System.out.println("Insert the element into the linear Queue :");
int ele=s.nextInt();
QNode p=new QNode();
p.data=ele;
p.link=null;
if(front==null)
front=p;
else rear.link=p;
rear=p;
}
public void delete()
{
//QNode temp;
if(front==null)
System.out.println("Linear Queue is empty");
else
{
//temp=front;
System.out.println("Deleted item :"+front.data);
front=front.link;
}
}
public void display(){
QNode temp;
if(front==null)
System.out.println("Linear Queue is empty");
else{
System.out.println("Queue elements are:");
for(temp=front;temp!=null;temp=temp.link)
System.out.println("-->"+temp.data);
}
}

public static void main(String[] args){
System.out.println("Single Linked List Linear Queue ADT");
Scanner s=new Scanner(System.in);
QueueSll q=new QueueSll();
String ch="y";
while(ch.equalsIgnoreCase("y")){
System.out.println("\n Linear Queue ooperations :");
System.out.println("1. Insert 2.Delete 3. Display 4.Exit");
System.out.println("Enter the option :");
int opt=s.nextInt();
switch(opt){
case 1:q.insert();break;
case 2:q.delete();break;
case 3:q.display();break;
case 4:System.exit(0);
default:System.out.println("Invalid option ");
}
System.out.println("Do you want to continue");
ch=s.next();
}
}
}

Stack ADT using Sinlgle Linked List

Program :
import java.util.Scanner;
interface SStackADT{
    void push();
    void pop();
    void display();
}
class Ssnode{
    int data;
    Ssnode link;
}
public class StackSll extends Ssnode implements SStackADT{
    Ssnode top;
    Scanner s=new Scanner(System.in);
    public StackSll(){
        top=null;
    }
    public void push(){
        System.out.print("Insert the element into the stack :");
        int ele=s.nextInt();
        Ssnode p=new Ssnode();
        p.data=ele;
        p.link=top;
        top=p;
    }
    public void pop(){
        if(top==null)
            System.out.println("Stack is empty");
        else{
            System.out.println("Popped item :"+top.data);
            top=top.link;
        }
    }
    public void display(){
        Ssnode temp;
        if(top==null)
            System.out.println("Stack is empty");
        else{
            System.out.println("Stack elements are :");
            for(temp=top;temp!=null;temp=temp.link)
                System.out.println("-->"+temp.data);
        }
    }
    public static void main(String args[]){
        System.out.println("Single Linked List Stack ADT");
        Scanner s=new Scanner(System.in);
        StackSll s1=new StackSll();
        String ch="y";
        while(ch.equalsIgnoreCase("y")){
            System.out.println("\n Stack operations:\n1.Push 2.Pop 3.Display 4.Exit\nEnter the option :");
            int opt=s.nextInt();
            switch(opt){
                case 1:s1.push();break;
                case 2:s1.pop();break;
                case 3:s1.display();break;
                case 4:System.exit(0);
                default:System.out.println("Invalid input");
            }
            System.out.println("Do you want to continue");
            ch=s.next();
        }
    }
}

Differences Between C++ and Java

Are you having a tough time differentiating between C++ and Java? Do you keep using the semicolon when you're not supposed to? While the two languages are similar, there are certain syntactical differences that can be confusing. Here’s something that will come in handy.

The main function

C++

int main( int X, char* Y[])
{
printf( "Hello, world" );
}

Java

Every function in Java has to be made a part of a class. So, the main function also has to be a part of a class. Moreover, in Java there is one main() function for every class. This can come in handy when writing unit tests for the class.

class HelloWorld
{
public static void main(String X[])
{
System.out.println( "Hello, World" );
}
}

Compiling

C++

In C++, you will be compiling as,
g++ X.cc -o outfile

This will then be run with,

./outfile

Java
In Java, you will compile the classes in X.java.

javac X.java

You have to run this by invoking the static main method.

Class declarations

While C++ requires a semicolon at the end of class declarations, Java does not have any such requirement.

C++

class X{};

Java

class X{}

Method declarations

A method declaration in Java must be a part of a class always. Otherwise, both the languages are quite the same syntactically on this front. You can also use the public, private and protected access specifications in Java.

Constructors and destructors

In the case of constructors, the syntax in both C++ and Java is the same. Java though has no exact replacement for destructors.

Static members

Static members (variables and functions) are also declared in the same way in both the languages. But, Java allows for static initialisation blocks in order to initialise static variables.

class Foo
{
static private int x;
// static initialization block
{ x = 5; }
}

Scoop static namespaces and methods

C++

In C++, you will use the Class::method form in order to scoop static methods.

class MyClass
{
public:
static do();
};

Use this by,
MyClass::do();

Java

In Java, on the other hand, scooping comes with the use of the .again parameter. This is similar to accessing the fields of a class.

class MyClass
{
public static do()
{
// do something
}
}

To use the static method.
MyClass.doStuff();

Accessing fields from various objects

C++

In order to access fields from classes and other such objects, the programmer has to use dots.

myClass x;
x.field;

If a pointer is involved, then the narrow operator (->) has to be used.

myClass x = new MyClass();
x->field;

Java
In Java, only the dot is used. Since we always use references in Java, even pointers are accessed using the dot.

myClass x = new MyClass();
x.field;

Protection levels

They are specified in a different manner.

C++

public:
void X();
void Y();

Java

public void X();
public void Y();

Virtual functions

C++

virtual int X();

Virtual functions can also be used non virtually. You can simply say into X().

Java
int foo(); // or, final int foo();

In Java, functions are always virtual by default. You use ‘final’ in order to avoid them from being overridden.

Abstract Classes

C++

All you need to do is include public virtual functions.

class X{ public: virtual void X() = 0; };

Java
In Java the syntax allows the programmer to be explicit.

abstract class X{ public abstract void X(); }

If you want to specify an interface, then say,

interface X{ public void foo(); }

In that case though, you will have to implement it by,

class Y implements X
{
public void M() { /* do something */ }
}

Memory management

This is also pretty much the same in both the languages, except that Java has garbage collection, so no delete for Java.

Tuesday, March 4, 2014

Print Calender in Java

Hi guys, in this post i am going to give u Print Calender Month code using Java.
import java.util.Scanner;

public class PrintCalendar {

/** Main method */

public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
System.out.println(" Calender ");
System.out.println("");
//Prompt the user to enter year
System.out.print("Enter full year (YYYY): ");
int year = scan.nextInt();

// Prompt the user to enter month
System.out.print("Enter month in number between 1 and 12(MM): ");

int month = scan.nextInt();

// Print calendar for the month of the year
if (month < 1 || month > 12 || year < 1980)
System.out.println("Wrong input!");
else
printMonth(year, month);

}
/** Print the calendar for a month in a year */

static void printMonth(int year, int month) {

//Print the headings of the calendar
printMonthTitle(year, month);

//Print the body of the calendar
printMonthBody(year, month);
}

/** Print the month title, e.g., May, 1999 */

static void printMonthTitle(int year, int month) {

System.out.println(" " + getMonthName(month) + " " + year);
System.out.println(" ");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");

}

/** Get the English name for the month */
static String getMonthName(int month) {
String monthName = null;
switch (month) {
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December";
}
return monthName;
}

/** Print month body */
static void printMonthBody(int year, int month) {

// Get start day of the week for the first date in the month
int startDay = getStartDay(year, month);

// Get number of days in the month
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);

// Pad space before the first day of the month
int i = 0;
for (i = 0; i < startDay; i++)
System.out.print(" ");
for (i = 1; i <= numberOfDaysInMonth; i++) {
if (i < 10)
System.out.print(" " + i);
else
System.out.print(" " + i);
if ((i + startDay) % 7 == 0)
System.out.println();
}
System.out.println();
}

/** Get the start day of the first day in a month */

static int getStartDay(int year, int month) {

//Get total number of days since 1/1/1800
int startDay1800 = 3;
int totalNumberOfDays = getTotalNumberOfDays(year, month);

//Return the start day
return (totalNumberOfDays + startDay1800) % 7;
}

/** Get the total number of days since January 1, 1800 */

static int getTotalNumberOfDays(int year, int month) {
int total = 0;

//Get the total days from 1800 to year - 1
for (int i = 1800; i < year; i++)
if (isLeapYear(i))
total = total + 366;
else
total = total + 365;

//Add days from January to the month prior to the calendar month
for (int i = 1; i < month; i++)
total = total + getNumberOfDaysInMonth(year, i);

return total;
}

/** Get the number of days in a month */

static int getNumberOfDaysInMonth(int year, int month) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return 31;

if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;

if (month == 2) return isLeapYear(year) ? 29 : 28;

return 0; // If month is incorrect
}

/** Determine if it is a leap year */
static boolean isLeapYear(int year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
}
Output :
Calender Output

Sunday, March 2, 2014

Differences between XML and HTML

XML HTML

eXtensible Markup Language
Hyper Text Markup Language
Markup language defines a set of rules for encoding documents that can be read by both humans and machines. Designed with focus on storing and transporting data.
Markup language for displaying web pages in a web browser. Designed to display data with focus on how the data looks
It is used to Transport data between the application and the database and to develop other mark up languages.
It is used to display a web page
User defined tags
Predefined tags
Tags are case sensitive
Tags are not case sensitive
XML is dynamic type
HTML is of static type
Infine set of tags
Limited no of tags
Every start tag must have end tag
Every start tag need not have end tag
XML focuses on what data is
HTML focuses on how data looks
XML attribute must be enclosed within Quotation marks.
Attribute value can be presented without Quotation marks
Cannot be used as a subtype of a sql_variant instance. Does not support casting or converting to either text or ntext. Does not support the following column and table constraints. XML provides its own encoding. Collations apply to string types only. Cannot be compared or sorted. Cannot be used in Distributed Partitioned Views. Not well supported by browsers
Data does not know itself very well. Data cannot change in response to environment. Data cannot be easily maintained. Cannot store or call on variables. Lacks the capability to define new structures by defining relationships between classes. Tags are not useful for exchanging the document between applications.

Introduction to xml

In this post i am going to give an Introduction of XML technology.

What is XML?


  • XML stands for eXtensible Markup Language.
  • XML is designed to transport and store data.
  • It is not a programming language, i's a markup language.
  • It is a specification of W3C(World Wide Web Consortium)
Markup : Enclosing textual content with in textual code(tags) is nothing but markup.

Purpose :  
  • Used to exchange the information
  • XML documents are used as deployment discriptors
  • Used as textual databases.

Syntax with example :

<?xml version="1.0" encoding="utf-8"?>
<student>
   <sno>10</sno>
   <sname>sudeep</sname>
   <sage>18</sage>
</student>

Well-Formness
It indicates the readability nature of an XML document.
  1. XML Document start with PROLOG.
  2. Start tag must have a matching end tag.
  3. Root element.
  4. Level constraint.
  5. Attribute values must be quoted.
  6. Elements are case sensitive.
XML Element.

1. Start element <eleementname>
2. End element </elementname>
An element can contain
  1. Other elements
  2. Text
  3. Attribute
  4. Mix of all

XML Attribute

If you want to add supplementary information attah to an element, instead of having another element.
  1. Attribute is unique in xml
  2. It can occur in any order.

Avoid using attributes.
  1. It can not contain multiple values.
  2. More difficult to manipulate code
Entities :
                &lt;                           :                  <
                &gt;                          :                  >
                &amp;                      :                  &
                &quot;                      :                  "
                &apos;                     :                   '
                &copy;                     :                   c

Naming Rules :

  1. names can contain letters, numbers and other characters.
  2. name cannot start with number or punctuation characters.
  3. Names cannot start wiht the letters xml.

Best Practices :

  1. Make names descriptive.
  2. Names should be short and simple.
  3. Avoid using hyphen(-), period(.) and colon(:) in the code.

 Validating XML :

If an xml document is developed accorrding to rules specified in DTD/XSD.
Validating xml can be done by :
  1. XML Parsers.
  2. XML Editors.

XML Parser :

It's an API that enables XML applications to work with xml document. It performs
  1. Reading the XML document.
  2. Checking the well-formedness
  3. Verify its validity. 
  4. Making XML data available to XML applications.

XML Editors :

XML Editors are just like text editors which have the feature to check well-formness of our XML document and to validate the same.

The best one is Altova XMLSpy. You can download a windows version here. Unfortunately Altova doesn't provide Linux version for XMLSpy. 

IDE's also have the feature of validating and checking the well-formedness of xml document.