myTechpartner: java Join Me On Facebook
Join Me On Facebook

Blogroll

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, 5 May 2013

Application of linkedlist

Polynomial Addition
import java.io.*;
class node
{
       int coe,exp;
       public node next;
       public node(int id,int dd)
         {
              coe=id;
              exp=dd;
         }
public void displaynode()
      {
         System.out.print(coe +"x^" +exp );
      }
}
class nodeList
{
   public node first;
   public node last;
   public void nodeLIst()
    {
      first=null;
      last=null;
    }
   public boolean isEmpty()
    {
      return(first==null);
    }
  public void insert(int id,int dd)
   {
    if(isEmpty())
     {
      first=new node(id,dd);
      last=first;
     }
   else
    {
      last.next=new node(id,dd);
      last=last.next;
    }
  }
public void displayList()
  {
   node current=first;
   while(current!=null)
    {
      current.displaynode();
      if(current.next!=null)
      System.out.print(" + ");
      current=current.next;
    }
}
public void read() throws Exception
  {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int c,e,h=1;
    while(h==1)
     {
     System.out.println("Enter coefficient\n");
        c=Integer.parseInt(br.readLine());
        System.out.println("Enter exponent\n");
        e=Integer.parseInt(br.readLine());
        insert(c,e);
     System.out.println("1.Enter add more terms\n 0:Exit");
        h=Integer.parseInt(br.readLine());
        }
     }
public void add(nodeList x,nodeList y,nodeList z) throws Exception
  {
    node p=x.first;
    node q=y.first;
    while(p!=null&&q!=null)
     {
      if(p.exp==q.exp)
       {
         z.insert(p.coe+q.coe,p.exp);
     p=p.next;
     q=q.next;
       }
      else
       if(p.exp>q.exp)
        {
         z.insert(p.coe,p.exp);
         p=p.next;
        }
      else
        {
         z.insert(q.coe,q.exp);
         q=q.next;
        }
     }
   while(p!=null)
   {
     z.insert(p.coe,p.exp);
     p=p.next;
    }
  while(q!=null)
   {
     z.insert(q.coe,q.exp);
     q=q.next;
    }
}
}
class polynomial2
{
  public static void main(String arg[])throws Exception
  {
   int id;
   int dd;
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   nodeList obj1=new nodeList();
   System.out.println("\nEnter the first polynomial");
   obj1.read();
   nodeList obj2=new nodeList();
   System.out.println("\nEnter the second polynomial");
   obj2.read();
   System.out.print("\nThe polynomial are\np(x) =");
   obj1.displayList();
   System.out.print("\nq(x)  =");
   obj2.displayList();
   System.out.print("\np(x)+q(x)  =");
   nodeList obj3=new nodeList();
   obj3.add(obj1,obj2,obj3);
   obj3.displayList();
}
}

 
            
         
  
 Java

Monday, 8 April 2013

JAVA

Variables
• Variables may be tagged as constants (final
keyword).
• Variables may be initialized at creation time
– final variables must be initialized at creation time
• Objects are variables in Java and must be
dynamically allocated with the new keyword.
– E.g., a = new ClassA();
• Objects are freed by assigning them to null, or when
they go out of scope (automatic garbage collection).
– E.g., a = null;
int n = 1;
char ch = ‘A’;
String s = “Hello”;
Long L = new Long(100000);
boolean done = false;
final double pi = 3.14159265358979323846;
Employee joe = new Employee();
char [] a = new char[3];
Vector v = new Vector();
Pointers & References Variables
• Java does not support pointers.
• All variables are passed by value except
objects.
• Java classes either:
– Reference an object (new keyword)
– Alias an object (assign to another object)

Expressions
• Java supports many ways to construct
expressions (in precedence order):
– ++,-- Auto increment/decrement
– +,- Unary plus/minus
– *,/ Multiplication/division
– % Modulus
– +,- Addition/subtraction
Assignment Operators
• Assignment may be simple
– x = y
• Or fancy with the following operators:
– *=, /=
– %=
– +=, -=
– &= (bitwise AND)
– |= (bitwise OR)
– ^= (bitwise exclusive OR)
Conditional Logic
• Conditional logic in Java is performed with the
if statement.
• Unlike C++ a logic expression does not
evaluate to 0 (FALSE) and non-0 (TRUE), it
evaluates to either true or false
• true, false are values of the boolean data
type.
• Building compound conditional statements
– && (And), || (Or), ! (Not), <, >, ==, !=, <=, >=, etc.

Sunday, 7 April 2013

Binary Search

import java.io.*;
class binarysearch
   {
         public static void main(String arg[]) throws Exception
            {
                  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                  System.out.println("How many elements");
                  int n=Integer.parseInt(br.readLine());
                  int a[]=new int[n];
                  System.out.println("Enter the array elements");
                  for(int i=0;i<n;i++)
                     {
                        a[i]=Integer.parseInt(br.readLine());
                     }
                 int mid,top,bott;
                 System.out.println("Enter the key elements");
                 int d=Integer.parseInt(br.readLine());
                 top=0;
                 bott=n-1;
                 while(top<=bott)
                  {
                     mid=(top+bott)/2;
                     if(d==a[mid])
                      {
                          System.out.println("Element found at"+(mid+1));
                           top++;
                      }
                  else
                    if(d<a[mid])
                      bott=mid-1;
                      else
                       top=mid+1;
                  }
           }
}
To run this follow steps in previews post:

Java JDK Installation & Path setting

jdk-6-windows-i586.exe installation step:

First all of you must download the  jdk-6-windows-i586.exe
you can download this from here:    images1
After downloading
1. run jdk-6-windows-i586.exe
2. After completion of installation
3. path setting for jdk
4. go to C:\Program Files (x86)\Java
5.copy the location  C:\Program Files (x86)\Java\jdk1.6.0\lib
1)right click on my computer select properties     2)  click Advance system setting                                       3)  click   Environment variables
image                      image                     image

4) click new                                                                           5) Type like as below and paste link                                       6) ok
image                            image

7)  also set path                                                           8) ok      9)  ok
image

10) Open cmd  and type javac
          image
11) successfully set java path in your systemOpen-mouthed smile

Linear search

import java.io.*;
class linear
{
public static void main(String arg[]) throws Exception
{
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("How many elements");
  int n=Integer.parseInt(br.readLine());
  int a[]=new int[n];
  System.out.println("Enter the elements");
  for(int i=0;i<n;i++)
    {
     a[i]=Integer.parseInt(br.readLine());
    } 
  System.out.println("The array is:");
  for(int i=0;i<n;i++)
  {
   System.out.println(a[i]);
  }
  System.out.println("Enter the element to be search");
     int k=0;
     int j=Integer.parseInt(br.readLine());
     for(int i=0;i<n;i++)
     {
      if(a[i]==j)
       k++;
     }
    
     if(k==0)
      System.out.println("Not found");
     else
      System.out.println("Found");
}
}
Follow this step to compile and run your code in CMD
1.Copy this and paste it into notepad and save this as class name(here linear) with extension .java      :- linear.java
  Remember where this file you save.
2. Open cmd and go to the folder ,file that you save linear.java
3.Type    javac linear.java       eg:-D:\ajith_er\labjava>javac linear.java
  if any error that will print on screen
4. then type      java linear     eg:-D:\ajith_er\labjava>java linear
now your program will be start to execute
If any help email me my id: ajitheredacheril@hotmail.com or ajither.9@gmail.com                                                                                                                                                                                                            by :- aer

Saturday, 6 April 2013

Java

Here am introducing not a core java program it only sample programs based on various
data structure in Java .I hope that all of you following this.
Sample programs:
1. Searching
2.sorting
3.Linked list
4.Stack
5.Queue
6.Tree
7.Graph


Will be published soon Stay for new updates

 

Contributors

Online Marketing

Do you Like this Article?

rss twitter facebook