Sunday, 7 October 2012

EVOLVING FROM C++ TO JAVA-2


TUTORIAL -2

Hi guys,this is my second tutorial in evolving from c++ to java,in my first i said about main function and file naming.In this one i will explaining you how to get AN INPUT AND TO DISPLAY IT.  

Some basic characteristics of java regarding input and output:

Java reads and write inputs as bytes.Java uses I/O streams for input and output operations. Since this is a beginners tutorial i don't want to explain fully about streams.For know you can consider stream's as a buffer ,which can store the data and connect two end points.Since java read's and write as bytes ,we must convert the bytes into different forms during reading data and convert the data that we are going to write into bytes.Java provides different streams for this conversion.For now since you are beginning in java programming i will be explaining you just how to get input and display output,as we go for further.in forth coming  tutorials i will explain you how streams work.

How to get INPUT in java?

I will explain two ways of reading a input in a java program.First i will explain you using BufferedReader.

USING BUFFEREDREADER:

 When we use bufferedreader method it reads the input as line and store it in a string.Even when we read a integer or float value it will take it as string.So when we are reading a integer or float value we must convert the string into regarding values using parse methods.
  • Step 1:Create a object to bufferreader class.Lets for example we can take that object as "in".
in=new BufferedReader(new InputStreamReader(System.in));
  • Step 2:how to use the buffereader object?
TO READ A STRING VALUE:
  • Create a string object and use readLine() or read() method.
String m;
m=in.readLine();
OR
m=in.read();
 read():It reads the string till there is a space.It wont read space,and it wont read after the space.
readLine():It reads including space,till the string we enter ends.
TO READ A INTEGER OR FLOAT OR DOUBLE VALUE
  • Create a string object and read the input using readLine() or read() method.
  • Now using parse method convert the string into your data type.


String m;
m=in.readLine();
int i=Integer.parseInt(m); //converts the string into integer value and assign it to i
Float f=Float.parseFloat(m);//converts the string into float value and assign it to f
Double d=Double.parseDouble(m);//converts the string into double value and assign it to d

Using Scanner class

I prefer to use scanner class,because its simple and easy to use.
  • Step1:Create a object for scanner class.Let the object be"in".
Scanner in=new Scanner(System.in);
  • Step2:Now just call the scanner methods to read value.
TO READ INTEGER:
int i=in.nextInt();//Reads a integer value and assign it to i

TO READ FlOAT:
Float f=in.nextFloat();//Reads a float value and assign it to f

TO READ DOUBLE:
Double d=in.nextDouble();//Reads a double value and assign it to d

TO READ STRING:
String str=in.nextLine();//Reads a string value and assign it to str.

How to display output in java?

The output method to display the result in java is similar to c++ and c.Here we use "System.out.print("") to display the output which is similar to cout and printf.
  • To add more than one data variables ,use + symbol in between them. 
  • To concatenate the data variables to a string,we can use +symbol.
System.out.println("hai");//prints the string hai in a new line.
System.out.print("hai");//prints the string hai in the same line.
System.out.println("hai"+i);//prints the string hai and variable i in a new line.
System.out.print(i+str);//prints the variable i and str in a line.It dose not perform addiction between them.

Note:

There are various method to perform input and output operations.I have just explained some basic methods to read data and display it on screen.




Saturday, 6 October 2012

TORTOISE AND HARE ALGORITHM(Floyd's Cycle Finding Algorithm)

Floyd's Cycle Finding Algorithm

Floyd's cycle finding algorithm is also know as TORTOISE AND HARE ALGORITHM.It is used for two different purposes in data structures.
  • To Find Cycles in graphs.
  • To Find Cycles in  linked list.

Algorithm:

I like to explain the algorithm by means of a singly linked list.We construct a singly linked list with one data member and an address location.The algorithm consist of two pointer,we take them as X and Y
  • LET X BE TORTOISE 
  • LET Y BE HARE

AT STARTING:

  •  LET X BE IN FIRST POSITION 
  • AND Y BE IN Kth POSITION(lets take k as 3rd position).

TRAVERSAL OF  HARE AND TORTOISE

  • Increment the Tortoise position by ONE.(X+1)
  • Increment the Hare position by TWO(Y+2)

IN CASE OF A CYCLE

  • THE TORTOISE AND HARE WILL MEET.
  • OR WHEN THE HARE OVER TAKES THE TORTOISE.


IN CASE IF THERE IS NO CYCLE

  • THE HARE WILL REACH THE END OF THE LINKED LIST.

DISADVANTAGE

  • It has a time complexity of O(length of loop to be find+first node of equal value).

Friday, 5 October 2012

Data Structure Tutorial-1-Singly linked list

Data Structure:

Data can be stored in different ways in a computer system.Data structure explains different ways of storing the data in the computer so that it can be used efficiently. 

Singly linked list:

The most simplest form of data structure is singly linked list.Its simple and it is used to implement other data structures like stack,queue.

ARRAYS VS SINGLY LINKED LIST

  • The single linked list is similar to ARRAYS.It over comes the disadvantage of arrays.
  • The main disadvantage of ARRAYS is, it is difficult to insert or delete an element in it.Singly linked list gives solution to it.
  • In most of the cases arrays size are not dynamic.In some cases array size is insufficient while in others there is lots of memory space get wasted.In singly linked list we have no need to declare the size of it at starting of the program.We can increase the size when ever we need it.So there is no memory wastage in it.

Structure of Singly Linked list:

  1. Head node
  2. Data nodes

HEAD NODE

 The basic structure of linked list is very similar to arrays.The HEAD node consist of the starting address of the first data node in the linked list.Its points to the address of the first data node.

DATA NODE

It consist of two section.The Data section and Address section.The Data section is used to store the data of the node and the Address section is used to point the next data node address.The last data node is called as tail node whose address section is empty.

Traversal

Using the head node we can find the first node.From the head node we can travers through the node using the address pointer.

OPERATIONS IN SINGLY LINKED LIST

  • INSERTION
  • DELETION 
  • SEARCHING

Insertion

  • To insert THE NODE IS LAST POSITION.Create the node and give the tail node address pointer to the new node and make the address section of new node as empty.Thus making the new node as tail node.
  • To insert THE NODE IN BETWEEN THE NODES.Create the node.Traverse through the list and find the node after which the node as to be inserted,lets call that node as X.First copy the address pointer of the X node to the new node.So now the new node will be pointing to next node of the X node.Now change the address pointer of X node to the newly created node.
  • To insert THE NODE AS THE FIRST NODE.Create the node.Now copy the address pointer of head node to new node.Then change the address pointer of the head node to point to the new node.

Deletion 

  • To delete the node in LAST.Traverse through the list and find the node that's before to the last node.Make the address pointer of that node as empty.
  • To delete the node in FIRST position.Copy the address pointer of the first node to the head node.Then make the address pointer of first node to NULL.
  • To delete the node IN MIDDLE.Traverse through the list,find the node that is previous to the node that as to be deleted.Copy the address pointer of the node that as to be deleted to its previous node and make the address pointer of the node to NULL.

Searching

  • Traverse through the node and find the data in a sequential manner.

Advantages of Singly linked list

  • Less memory wastage
  • We can insert and delete nodes to our wish.

Disadvantages of Singly linked list

  • It takes time to search through the list
  • We cannot traverse back through the list.We can traverse in only one way.

Sunday, 30 September 2012

EVOLVING FROM C++ TO JAVA-1

TUTORIAL -1

In this tutorial, i will be saying how to transform from c++ programming to java programming.Since this is the first tutorial,I WILL BE EXPLAINING ABOUT JAVA FILE NAMING AND MAIN SECTION.

JAVA FILE NAMING:

In c++ we can name the program file as we wish.But in java WE MUST NAME THE PROGRAM FILE WITH THE NAME OF THE CLASS THAT CONTAINS THE MAIN FUNCTION or else WHILE RUNNING WE MUST RUN WITH THE CLASS NAME THAT CONTAINING THE MAIN FUNCTION.This helps the compiler to understand that there is a class in the name of the file and it contains main function.The compiler will start reading from that class,staring it from the main function.And another thing we must care about it is, There can be ONLY ONE PUBLIC CLASS FUNCTION IN A JAVA PROGRAM.We cannot have more than one public class in java. 

For example:

A c++ program:

public class A
{
       .......
       ........
}
public class B //more than one class can be public
{
     ....................
     ....................
}

 public void main()// the main function can be out of the class
        {
             ..................
             .................
             .................
         }
FILE NAME: example.cpp

A java program:

public class A   //must name the file name with this class name
{
     public static void main(String args[]) // main function must be inside the class
     {
        ...............
        ...............
        ...............
      }
}
class B    //only one class can be public
{
    ................
    ................
}
FILE NAME: A.java

JAVA MAIN FUNCTION:

Syntax: public static void main(String args[])

We must declare THE MAIN FUNCTION AS STATIC because it helps the compiler to call the main function without any object.A STATIC MAIN FUNCTION DOESN'T NEED A OBJECT TO BE CALLED.TheString args[] parameter helps to pass the value from command line.The value passed are stored in string args array.We must declare the main function as public too.