Monday 27 August 2012

Easy steps for Applet program


Applet

 Applet is an application running on web browser. it is designed to be transmitted over the internet and running always on client machine.

Web Architecture

     

Applet are small application that are accessed on a Internet server, transported over the Internet, automatically installed & run as part of web document.

Hierarchy of Applet


Applet Life Cycle

init() :this is for initialization as like constructor ,it is automatically called by  system itself.
start() : this methods automatically calls when java calls init method
stop() : this method automatically calls when user moves off the page on which the applet sits.
destroy() : Java is guaranted to call this method when the browser shuts down normally.
paint(Graphics g) : The paint( ) method is called each time your applet’s output must be redrawn.

Simple Applet Program

Follow the steps for applet program

1.     Write the Simple program
              import java.awt.*;
             import java.applet.*;
            class MyApplet extends Applet
              {
                        public void paint (Graphics g)
                        {
                         g.drawString ("Hello World", 25, 50);
                        }
             }
2.     Save this program as MyApplet.java,
3.     Compile Applet program by using command javac MyApplet.java
4.     After that do the HTML code as like below
<html>
<head>This is Applet Program</head>
<body>
<applet code="MyApplet" width=200 height=60>
</applet>
</body>
</html>
    5. Save this html code as Applet.html.and open it into web browser.See  output.

Restriction of Applet


  1. Applet can never run any local executable program.
  2. Applet can’t communicate with an internet site other than the one that served the web page that included the applet.
  3. Applet can’t read/write files on user’s file system.
  4. All windows popped by an applet carry a warning message.
  5. Applet can never find any info about the local computer.


If any problem persist use comment line to share your problem.

Sunday 12 August 2012

Simple way to Create Package in Java

 Package in Java

       Package is collection of classes and interfaces.
       this is the way of grouping classes and interfaces into the single directory.

How to use package ?

    In Java, lots of predefined packages are implemented.like java.util , java.io ,java.awt etc.Use an import keyword when we have to use these packages  functionality.

How to use import Keyword ? When to use import keyword ?

       these are the two basic question ,use import keyword as shown below:

 import java.NameOf Package;

               for example ,lets consider you have to import util package in your class,then your import statement is as look like below statement.

 import java.util.*;


here:
import   : Keyword in java
java.util : Name of  Package
*           : All content present in respective package.

Use the import keyword whenever the class in the particular package has to be used.


Application of Package :

  1. Avoid Naming complexity
  2. Hide the implementation details of classes.
  3. It allows us to grouping classes and interfaces into the same directory.

How to Create My Own Package

follow the next steps

  • Use the keyword package at the beginnning of your class.
  • Create Directory of that package Name
  • Compile the classes and copy the .class file into package directory. 
  • Run your program by using following command

 java NameOf Package.ClassName

Write a program to Create a User defined package


Listen friend ,here you are creating your own package.you have to follow next procedure for the same purpose.

//Simple Program

package myPackage;
class SampleOne
{
 public void run()
 {
  System.out.println("This is run method");
 }

}

//Save this program as SampleOne.java and create new java program for next program


package myPackage;
class SampleTwo extends SampleOne
{
 public void running()
 {
  System.out.println("This is running method");
 }
 public static void main(String[] args)
 {
  SampleTwo st=new SampleTwo();
  st.run();
  st.running();
  System.out.println("Hello World!");
 }
}

//Save this program as SampleTwo.java and follow next procedure

How to Compile and Run package program

  • Got to cmd.
  • In  cmd,Go to path upto Java\jdk\bin
 
  • Comile the first program :

                     javac SampleOne.java

  • copy SampleOne.class file from bin directory to package directory called myPackage
  • then compile Second program

                     javac SampleTwo.java

  • copy SampleTwo.class file from bin directory to package directory called myPackage
  • Run the program

                   java myPackage.SampleTwo

  • Output looks like as below

here is your package will create with name myPackage.
If you have any doubt or wants some more program then keep use comment line ,I will reply you as soon as possible.

Saturday 11 August 2012

Program to Implement Vector class

What is Vector class ?
         Vector class is predefined class in java.This class is present inside the java.util package.
Vector is similar like Array but having some difference between them.
  1. Vector is having many predefined method as array dont have it.
  2. Vector is synchronized.
  3. Vector is very useful when if you dont know the size of array earlier or you have to change the size over the lifetime of your program.

Vector class and their method


//Write a program to add some element into the Vector and remove any element,Display the result.
//Example of Vector class.

import java.util.*;
class VectorDemo
{
 public static void main(String[] args)
 {
  Vector v=new Vector();
  int a=args.length;
  for(int i=0;i<a;i++)
  {
   v.addElement(args[i]);
  }
  int size=v.size();
  System.out.println("Initial Size of Vector:"+size);
  String b[]=new String[size];
  v.copyInto(b);
  for(int i=0;i<size;i++)
  {
   System.out.println("Elements of Vector:"+i+":"+b[i]);
  }
  v.removeElementAt(2);
  int s=v.size();
  v.copyInto(b);
  System.out.println("After Removing Element,Size of Vector:"+s);
  for(int i=0;i<s;i++)
  {
   System.out.println("Elements of Vector:"+i+":"+b[i]);
  }
 
 }
}

Save this program as      VectorDemo.java

Compile it
                                     javac VectorDemo.java

Run it 
                                     java VectorDemo  4 5 8 9

Output
                              

If you have any trouble or wants some more program then paste your problem statement  on comment line.I will definately give you responce as early as possible.   

Thursday 9 August 2012

Pascal Triangle in Java

What is  Pascal Triangle ?


                                        Pascal triangle is triangular array of the binomial coefficent in a triangle.It is named from the name of french mathematician  Blaise Pascal.
                                         Figure shows the structure of pascal triangle having result up to six th rows.
        Pascal Triangle

//Write a Program to implement Pascal Triangle by using Java?


import java.util.*;
class Pascal
{
  public static void main(String args[])
  {
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the limit: ");
     int x=sc.nextInt();
     int arr[][]=new int [x][x];
 
    for(int i=0;i<x;i++)
    {
     for(int j=0;j<x;j++)
      {
         arr[i][0]=1;
       }
    }
 
   for(int i=1;i<x;i++)
   {
      for(int j=1;j<x;j++)
       {
         arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
        }
     }
   for(int i=1;i<x;i++)
     {
      for(int j=1;j<x;j++)
       {
          if(i==j)
           arr[i][j]=1;
        }
       }
   for(int i=0;i<x;i++)
   {
     for(int k=x;k>=i;k--)
      {
        System.out.print(" ");
       }
      for(int j=0;j<=i;j++)
       {
         System.out.print(arr[i][j]+" ");
       }
      System.out.println();
    }

  }
}

Save this program as Pascal.java

Compile  Java program by using command     javac Pascal.java

Run this program by using command  java Pascal

See the Output :


If you want some more pattern code then just mail me nihalcse@gmail.com

Some more  Reference:

                                                Pascal's triangle ,|| Blaise Pascal ,|| triangular array ,||  binomial coefficients

Tuesday 31 July 2012

Pattern Programming

 Pattern Programming:
           Pattern programming is a series of program which may contain curtain pattern having an output into the form like shapes of triangle,diamond,kites,square,rectangle and many more.

       Below is some of the example of Pattern :




//Pattern Programming : Program of Kites from Java
class Pattern
{
    public static void main(String args[])
    {
        int no=4;
        for(int i=0;i<no;i++)
        {
            for(int k=no;k>i;k--)
            {
                System.out.print(" ");
            }
            for(int j=0;j<=i;j++)
            {
                System.out.print("* ");
            }
            System.out.print("\n");
        }
        for(int i=no;i>=0;i--)
        {
            for(int k=no;k>i;k--)
            {
                System.out.print(" ");
            }
            for(int j=0;j<=i;j++)
            {
                System.out.print("* ");
            }
            System.out.print("\n");
        }
        for(int i=1;i<no;i++)
        {
            for(int k=no;k>i;k--)
            {
                System.out.print(" ");
            }
            for(int j=0;j<=i;j++)
            {
                System.out.print("* ");
            }
            System.out.print("\n");
        }
    }
}


Save this program as Pattern.java

Compile  Java program by using command     javac Pattern.java

Run this program by using command  java Pattern.java

See the Output :


If you want some more pattern output then just mail me @   nihalcse@gmail.com

Friday 27 July 2012

Armstrong Number

What is Armstrong Number ?
   An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself.

for example:
  Lets consider the following number:
                                                         153
                                                         370
                                                         371
                                                         407

153=1^3+5^3+3^3
      =153

370=3^3+7^3+0^3
      =370

371=3^3+7^3+1^3
      =371

407=4^3+0^3+7^3
      =407

//Program to check Whether given number is armstrong number or not by using Java.
class ArmstrongNumber
{
   public static void main(String args[])
   {
     int a,b,c=0,n=153;
     a=n;
     while(n>0)
     {
        b=n%10;
        c=c+(b*b*b);
        n=n/10;
     }
    if(a==c)
    {
      System.out.println("This number is Armstrong number:"+c);
     }
    else
     {
       System.out.println("This number is not Armstrong number:"+c);
     }
  }
}


Save this program as      ArmstrongNumber.java

Compile it
                                     javac ArmstrongNumber.java

Run it 
                                     java ArmstrongNumber

Output
                                     
                                    This number is Armstrong number:153

If you have any trouble or wants some more program then just reach @ nihalcse@gmail.com

Wednesday 25 July 2012

Harmonic series

Harmonic Series

Harmonics means wavelengths of the overtones of a vibrating string are 1/2, 1/3, 1/4, etc., of the string's fundamental wavelength.  the phrase harmonic mean likewise derives from music.

Diagram indicate Harmonic series:

Program for Harmonic Series

class Harmonic
{
    public static void main(String args[])
    {
        int i,no;
        double j=0;
        no=5;                        //It shows length of Harmonic series
        while(no>0)
        {
           j=j+(double) 1 / no;
           no--;
        }
        System.out.println("Output of Harmonic Series:"+j);
    }
}


Save this program as                       Harmonic.java

Compile it by using command
                                                      javac Harmonic.java

run this program by using command
                                                
                                                       java Harmonic
OUTPUT will look like:

                                                       Output of Harmonic Series:2.28333333

If you want it some more program then paste your problem statement below on comment line.


Tuesday 24 July 2012

program for Fibonacci Series

What is fibonacci Series?
  Fibonaccci Series means the addition of last two adjecent number starting from 0&1.

Fibonacci Series is look like this,
     0 1 1 2 3 5 8 13 21 .....

//PROGRAM FOR FIBONNACI SERIES

class Fibonacci
{
   public static void main(Strings args[])
   {
      int a,b,c,no;
      a=0;
      b=1;
      no=5;                                 
      System.out.println(a);
      System.out.println(b);
     while(no>0)
      {
        c=a+b;
        System.out.println(c);
        a=b;
        b=c;
        no--;
    }
}


Compile your program by using command
 javac Fibonacci.java

if complied succesfully
 java Fibonacci

OUTPUT
0
1
1
2
3
5
8

ShareThis

Related Posts Plugin for WordPress, Blogger...