Thursday, 25 September 2014

Program for Olympic Applet


import java.awt.*;
import java.applet.*;
public class Olympic_Applet extends Applet
{
    public void init()
    {
        setBackground(Color.gray);
    }

    public void paint(Graphics g)
    {
        g.setColor(Color.blue);
        g.drawOval(50,50,100,100);

        g.setColor(Color.black);
        g.drawOval(120,50,100,100);

        g.setColor(Color.red);
        g.drawOval(190,50,100,100);

        g.setColor(Color.yellow);
        g.drawOval(85,120,100,100);

        g.setColor(Color.green);
        g.drawOval(155,120,100,100);
    }
}

/*


*/
Output

Tuesday, 23 September 2014

Program For Reverse Number


import java.io.*;
class  Reverse
{
    public static void main(String s[]) throws Exception
    {
        int n = 0,rev;
        Reverse r = new Reverse();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nEnter no: ");
        n = Integer.parseInt(br.readLine());

        rev = r.reverse(n);

        System.out.print("\nReverse no: "+rev);
        System.out.println();
    }


    public int reverse(int num)
    {
        int reverse = 0;
        while (num != 0)
        {
            reverse = reverse * 10;
            reverse = reverse + num%10;
            num = num/10;
        }
        return reverse;
    }
}
Output

Thursday, 18 September 2014

Program for String Palindrome


import java.io.*;
class String_pallindrome
{
    public static void main(String s[]) throws Exception
    {
        String str1;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
        System.out.print("\nEnter String: ");
        str1 = br.readLine();
      
        StringBuffer sb = new StringBuffer(str1);
        StringBuffer str2 = sb.reverse();

        if((sb).equals(str2))
        {
            System.out.println("\nString is Pallindrome");
        }
        else
        {
            System.out.println("\nString is not Pallindrome");
        }
    }
}
Output

Wednesday, 17 September 2014

Program for Multiple Inheritance




class Student
{
    private int roll_no;
    private String name;

    void get_student(int roll_no,String name)
    {
        this.roll_no = roll_no;
        this.name = name;
    }
    void display_student()
    {
        System.out.print("\nRoll_no: "+roll_no);
        System.out.print("\nName: "+name);
    }
}

interface Test
{
    final static int m1 = 100;
    final static int m2 = 100;
    void display_test();
}

class Result extends Student implements Test
{
    private float per;
    public void display_test()
    {
        System.out.print("\nMarks1: "+m1);
        System.out.print("\nMarks2: "+m2);
    }

    void display_result()
    {
        per = (((m1+m2)/200)*100);
        System.out.println("\nPercentage: "+per);
    } 
}

class Multiple_inheritance
{
    public static void main(String s[])
    {
        Result r = new Result();
        r.get_student(10,"ABC");
        r.display_student();
        r.display_test();
        r.display_result();
    }
}
Output

Saturday, 13 September 2014

Applet in Java

//Program For Shapes in Applet
import java.awt.*;
import java.applet.*;
public class ShapesDemo extends Applet
{
    public void paint(Graphics g)
    {
        g.drawLine(10,10,110,110);
        g.drawRect(160,10,100,100);
        g.drawOval(310,10,100,100);
        g.drawArc(10,160,90,50,200,150);
    }
}

/*


*/
Output


//Program for Polygon in Applet
import java.awt.*;
import java.applet.*;
public class PolygonDemo extends Applet
{
    public void paint(Graphics g)
    {
        int x[] = {10,10,110};
        int y[] = {10,110,110};
        int n = 3;

        g.drawPolygon(x,y,n);
    }
}

/*


*/
Output


//Program for Face in Applet
import java.applet.*;
import java.awt.*;

public class Face_Applet extends Applet
{
    public void paint(Graphics g)
    {
        g.drawOval(50,50,200,200);

        g.drawOval(100,100,25,25);
        g.fillOval(100,100,25,25);

        g.drawOval(180,100,25,25);
        g.fillOval(180,100,25,25);

        g.drawArc(110,170,90,50,200,150);
    }
}

/*


*/
Output

Wednesday, 10 September 2014

Multithreading In Java!

//Program for Multiple Threading
class ThreadDemo extends Thread
{
    private Thread t;
    private String thread_name;

    ThreadDemo(String thread_name)
    {
       this.thread_name = thread_name;
       System.out.print("\nCreating "+thread_name);
    }

    public void run()
    {
       System.out.print("\nRunning "+thread_name);

       for(int i=1;i<=9;i++)
       {
            System.out.print("\nSequence Number Series: "+i);
            try
            {
                Thread.sleep(50);
            }
            catch(Exception e)
            {

            }
       }

       System.out.print("\nTerminating "+thread_name);
    }

    public void start()
    {
        System.out.print("\nStarting: "+thread_name);
        if(t == null)
        {
            t = new Thread(this,thread_name);
            t.start();
        }
    }
}

class RunnableDemo implements Runnable
{
    private Thread t1;
    private String thread_name;

    RunnableDemo(String thread_name)
    {
      this.thread_name = thread_name;
      System.out.print("\nCreating "+thread_name);
    }

    public int fibo(int i)
    {
      if(i == 0)
        return 0;
      else if(i == 1)
        return 1;
      else
         return (fibo(i-1)+fibo(i-2));
   }

    public void run()
    {
        System.out.print("\nRunning "+thread_name);

        int i;
        for(i=0;i<=7;i++)
        {
            System.out.print("\nFibonacci Series: "+fibo(i));
            try
            {
                Thread.sleep(100);
            }
            catch(Exception e)
            {

            }
        }

        System.out.println("\nTerminating "+thread_name);
    }

    public void start()
    {
        System.out.print("\nStarting "+thread_name);

        if(t1 == null)
        {
            t1 = new Thread(this,thread_name);
            t1.start();
        }
    }
}

public class TestThread1
{
    public static void main(String s[])
    {
        ThreadDemo thread = new ThreadDemo("Thread-1");
        thread.start();

        RunnableDemo r1 = new RunnableDemo("Thread-2");
        r1.start();
    }
}

//Program for Synchronize Method
class Syncro
{
    synchronized public void print(String s)
    {
      try
      {
         System.out.print("\nHello "+s);
         System.out.print("\n-How Are you???");
         Thread.sleep(100);
      }
      catch(Exception e)
      {

      }
    }
}

class Sample implements Runnable
{
    Syncro syn;
    String s;
    Sample(Syncro syn,String s)
    {
       this.syn = syn;
       this.s = s;
       Thread t = new Thread(this,s);
       t.start();
    }

    public void run()
    {
       synchronized(syn)
       {
          syn.print(s);
       }

       System.out.println();
    }
}

class Synchronize_Demo
{
    public static void main(String s[]) throws Exception
    {
       Syncro syn = new Syncro();

       Sample s1 = new Sample(syn,"ABC");
       Sample s2 = new Sample(syn,"XYZ");
    }
}

Friday, 5 September 2014

Exception Handling

//Program for Exception
class ExceptionDemo
{
    public static void main(String soham[])
    {
         int a = 10,b = 0,c;
         int arr[] = new int[2];

        try
        {
             arr[2] = 2;
        }
        catch(ArrayIndexOutOfBoundsException ae)
        {
            System.out.print("\nException: Array index out of bound");
        }

        finally
       {
           System.out.print("\nFinally statement execution is compulsary");
       }
   }
}
Output


//Program for Exception in Method
class ExceptionDemoMethod
{
    public static void main(String s[])
    {
         ExceptionDemoMethod edm = new ExceptionDemoMethod();

        try
        {
             edm.e_method();
        }
        catch(Exception e)
        {
            System.out.print("\nException: "+e);
        }
    }

    public void e_method() throws Exception
    {
        int a = 10,b = 0,c;
        c = a/b;
    }
}
Output


//Program for Custom Exception
import java.io.*;
class MyCustomException extends Exception
{
   private int age;

   public MyCustomException(int age)
   {
        this.age = age;
   }
  
   public String toString()
   {
        return "My Exception ["+age+"]";
   }
}

public class CustomExceptionDemo
{
   static void Test(int age) throws MyCustomException
   {
        int r = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        if(age <= 18)
        {
            System.out.print("\nYou are NOT eligible for voting!!!");
            throw new MyCustomException(age);
        }

        System.out.print("\n1.BJP \n2.CONGRESS \n3.MNS \n4.OTHERS");

        try
        {
            System.out.print("\nEnter your representative: ");
            r = Integer.parseInt(br.readLine());
        }
        catch (Exception e)
        {
            System.out.print("\n"+e);
        }

        System.out.println("\nYour VOTE has been casted. \nTHANK YOU");
   }

   public static void main(String soham[]) throws Exception
   {
        int age;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nEnter your age:");
        age = Integer.parseInt(br.readLine());

        try
        {
            Test(age);
        }
        catch (MyCustomException mce)
        {
            System.out.println("\n" +mce.toString());
        }
   }
}

Monday, 1 September 2014

Packages In Java

How to save a package ? 
1. Create folder StackStack.
2. Create subfloder stack.
3. Save Sstack.java in subfolder stack.
4. Then, save StackDemo.java in folder StackStack.
//save as Sstack.java
package stack;

interface Instack
{
    void push(int element);
    void pop();
    void display();
}

public class Sstack implements Instack
{
     int n,arr[],top;
     public Sstack(int size)
     {
          n = size;
          arr = new int[n];
          top = -1;
     }

     public void push(int element)
     {
          int temp = 0;
          if(top >= (n-1))
          {
                System.out.println("\nStack is overflow");
          }
          else
          {
                top++;
               arr[top] = element;
               System.out.println("\nElement inserted: "+element);
          }
    }

    public void pop()
    {
        int temp;
        if(top == -1)
        {
            System.out.println("\nStack is underflow");
        }
        else
        {
            temp = arr[top];
            top--;
            System.out.println("\nElement Deleted: "+temp);
        }
    }

    public void display()
    {
        int i;
        if(top == -1)
        {
            System.out.println("\nStack is underflow");
        }

        for(i=0;i<=top;i++)
        {
            System.out.println("\n"+arr[i]);
        }
    }
}
//save as  StackDemo.java
import java.io.*;
import stack.*;             //importing package stack

class StackDemo
{
    public static void main(String s[]) throws Exception
    {
        int n,c,e,i;
        BufferedReader br  = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nEnter number of elements in the array: ");
        n = Integer.parseInt(br.readLine());

        Sstack ss = new Sstack(n);
        for(i=0;i<100 data-blogger-escaped-1="" data-blogger-escaped-2="" data-blogger-escaped-3="" data-blogger-escaped-:="" data-blogger-escaped-break="" data-blogger-escaped-c="" data-blogger-escaped-case="" data-blogger-escaped-choice:="" data-blogger-escaped-default="" data-blogger-escaped-e="" data-blogger-escaped-element="" data-blogger-escaped-i="" data-blogger-escaped-input="" data-blogger-escaped-insert:="" data-blogger-escaped-nenter="" data-blogger-escaped-nwrong="" data-blogger-escaped-pre="" data-blogger-escaped-ss.display="" data-blogger-escaped-ss.pop="" data-blogger-escaped-ss.push="" data-blogger-escaped-switch="" data-blogger-escaped-system.out.print="" data-blogger-escaped-system.out.println="" data-blogger-escaped-to="" data-blogger-escaped-your="">