Monday, 22 December 2014

AdjustmentListener in Java

//save as AdjustmentListenerDemo.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AdjustmentListenerDemo extends Applet implements AdjustmentListener
{
    Scrollbar slider;
    int x = 0;

    public void init()
    {             
        setLayout(null);

        slider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 10, 0, 300);

        slider.setBounds(10,100,150,25);

        add(slider);
        slider.addAdjustmentListener(this);
    }

    public void adjustmentValueChanged(AdjustmentEvent ae)
    {
        x = ae.getValue();
        repaint();
    }

    public void paint(Graphics g)
    {
        g.drawString("Current Value: " + x,10,180);
    }
}


/*


*/
Output

Friday, 19 December 2014

KeyListener in Java

//save as KeyListenerDemo.java 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class KeyListenerDemo extends Applet implements KeyListener
{
    String s = "";

    public void init()
    {
        addKeyListener(this);
        requestFocus();     //request input focus.
    }
  
    //Handle when a key has been pressed...
    public void keyPressed(KeyEvent ke)
    {
        showStatus("Key Pressed...");
        repaint();
    }

    //Handle when a key has been released...
    public void keyReleased(KeyEvent ke)
    {
        showStatus("Key Released...");
        repaint();
    }
  
    //Handle when a key has been pressed...
    public void keyTyped(KeyEvent ke)
    {
        s += ke.getKeyChar();
        repaint();
    }
  
    //display keystrokes.
    public void paint(Graphics g)
    {
        g.drawString(s,x,y);
    }
}

/*


*/
Output

Tuesday, 16 December 2014

ItemListener in Java

//save as ItemListenerJavaDemo.java 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ItemListenerJavaDemo extends Applet implements ItemListener
{
    Checkbox radio1;
    Checkbox radio2;
    Checkbox radio3;
    Checkbox radio4;
    String msg = "";
  
    public void init()
    {
        radio1 = new Checkbox("Window7");
        radio2 = new Checkbox("Window8");
        radio3 = new Checkbox("MacOS");
        radio4 = new Checkbox("Vista");
      
        add(radio1);
        radio1.addItemListener(this);
        add(radio2);
        radio2.addItemListener(this);
        add(radio3);
        radio3.addItemListener(this);
        add(radio4);
        radio4.addItemListener(this);
    }
  
    public void itemStateChanged(ItemEvent ie)
    {
        repaint();
    }
  
    public void paint(Graphics g)
    {
        msg = "Current State: ";
        g.drawString(msg,6,100);
      
        msg = "Windows7:  " + radio1.getState();
        g.drawString(msg,6,120);
      
        msg = "Windows8:  " + radio2.getState();
        g.drawString(msg,6,140);
      
        msg = "MacOS:  " + radio3.getState();
        g.drawString(msg,6,160);
      
        msg = "Vista:  " + radio4.getState();
        g.drawString(msg,6,180);
    }
}

/*


*/
Output

Tuesday, 9 December 2014

ItemListenerDemo in Java


import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class ItemListenerDemo extends Applet implements ItemListener
{
     CheckboxGroup radioGroup;
     Checkbox radio1;
     Checkbox radio2;
     Checkbox radio3;

    public void init()
    {
          radioGroup = new CheckboxGroup();
          radio1 = new Checkbox("Red",radioGroup,false);
          radio2 = new Checkbox("Blue",radioGroup,false);
          radio3 = new Checkbox("Green",radioGroup,false);
         
          add(radio1);
          radio1.addItemListener(this);
          add(radio2);
          radio2.addItemListener(this);
          add(radio3);
          radio3.addItemListener(this);
     }

    public void paint(Graphics g)
    {
        int x = getSize().width;
        int y = getSize().height;

        x = x/2;
        y = y/2;

        if(radioGroup.getSelectedCheckbox() == radio1)
        {
            g.setColor(Color.red);
        }
        else if(radioGroup.getSelectedCheckbox() == radio2)
        {
            g.setColor(Color.blue);
        }
        else
        {
            g.setColor(Color.green);
        }

        g.drawString("Welcome to Java...",x,y);
    }

    public void itemStateChanged(ItemEvent ie)
    {
        repaint();
    }
}

/*


*/
Output

Sunday, 30 November 2014

Caesar Applet Demo in Java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class CaesarApplet extends Applet implements ActionListener
{
    Label enterText;
    TextField text;

    Label enterKey;
    TextField key;

    Button encrypt;
    Button decrypt;

    Label result_text;
    Label result;

    public void init()
    {
            enterText = new Label("Enter Text: ");
            text = new TextField(25);
            enterKey = new Label("Enter key: ");
            key = new TextField(25);
            encrypt = new Button("Encrypt Text");
            decrypt = new Button("Decrypt Text");
            result_text = new Label("Resultant Text: ");
            result = new Label();

            add(enterText);
            add(text);
            add(enterKey);
            add(key);

            add(encrypt);
            encrypt.addActionListener(this);
            add(decrypt);
            decrypt.addActionListener(this);

            add(result_text);
            add(result);
    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource() == encrypt)
        {
            String pt = text.getText();
            String k = key.getText();
            int encrypt_key = Integer.parseInt(k);

            String res = encrypt(pt,encrypt_key);
            result.setText(res);
        }
        else if(ae.getSource() == decrypt)
        {
            String ct = text.getText();
            String k = key.getText();
            int decrypt_key = Integer.parseInt(k);

            String res = decrypt(ct,decrypt_key);
            result.setText(res);
        }
    }

    static public String encrypt(String pt,int encrypt_key)
    {
        char c;
        int num,i;

        StringBuffer sb = new StringBuffer(pt);
        int len = sb.length();

        for(i=0;i

Output:







Saturday, 29 November 2014

ActionListener in Java


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class FormAppletDemo extends Applet implements ActionListener
{
    Label entername;
    TextField name;
    Label enteremail;
    TextField email;
    Button click;
    Label result;
    public void init()
    {
        entername = new Label("Enter your name: ");
        name = new TextField(30);
        enteremail = new Label("Enter your email: ");
        email = new TextField(30);
        click = new Button("Click Here");
        result = new Label();

        add(entername);
        add(name);
        add(enteremail);
        add(email);
        add(click);
        click.addActionListener(this);
        add(result);
    }

    public void actionPerformed(ActionEvent ae)
    {
        String s = name.getText();
        String e = email.getText();
        String n = "";

        if(s.equals(n) || e.equals(n))
        {
            result.setText("Please enter appropriate information...");
        }
        else
        {
            result.setText("Welcome to Blogger's Group");
        }
    }
}

/*


*/
Output

Saturday, 1 November 2014

Caesar Cipher

import java.io.*;
public class Caesar_cipher
{
    public static void main(String soham[]) throws Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        // enter string to encrypt
        System.out.print("\nEnter String to Encrypt: ");
        String pt = br.readLine();

        // enter key to encrypt
        System.out.print("\nEnter key to Encrypt: ");
        int key = Integer.parseInt(br.readLine());

        // passing plain_text and key to method encrypt
        String ct = encrypt(pt,key);

        // printing encrypted text
        System.out.print("\nEncrypted Text: " + ct);

        System.out.println();

        // enter key to decrypt the encrypted text
        System.out.print("\nEnter key to Decrypt: ");
        int dkey = Integer.parseInt(br.readLine());

        // comparing encrypted_key and decrypted_key
        if(key == dkey)
        {
            // if match is found passing cipher_text and key to method decrypt
            String pt1 = decrypt(ct,key);
            System.out.print("\nDecrypted Text: " + pt1);
        }
        else
        {
            // if match is not found cipher_text will not be decrypted...
            System.out.print("\nNot Authorized...");
        }

        System.out.println();
    }

    public static String encrypt(String pt,int key)
    {
        char c;
        int i,num = 0;

        StringBuffer sb = new StringBuffer(pt);

        int len = sb.length();
        for(i = 0; i <= (len-1); i++)
        {
            num = pt.charAt(i) - 65;
            num = num + key;
            c = (char)(num+65);
            sb.setCharAt(i,c);
        }
        return new String(sb);
    }

    public static String decrypt(String ct,int key)
    {
        char c;
        int i,num = 0;

        StringBuffer sb = new StringBuffer(ct);

        int len = sb.length();
        for(i=0;i <= (len-1);i++)
        {
            num = ct.charAt(i) + 65;
            num = num - key;
            c = (char)(num-65);
            sb.setCharAt(i,c);
        }
        return new String(sb);
    }
}
Output

Wednesday, 1 October 2014

Program for taking UserInput in Applet


import java.awt.*;
import java.applet.*;

public class UserInput extends Applet
{
      TextField text1, text2;

      public void init()
      {
           text1 = new TextField("",10);
           text2 = new TextField(8);
           text1.setBounds(100,100,50,50);
           add(text1);
           add(text2);
           text1.setText("");
           text2.setText("");
      }

      public void paint(Graphics g)
      {
            int x = 0,y = 0,z = 0;
            String str1 = "0",str2 = "0",sum = "0";
            g.drawString("Input a number in each box: ",5,20);
            try
            {
                    str1 = text1.getText();
                    x = Integer.parseInt(str1);
                    str2 = text2.getText();
                    y = Integer.parseInt(str2);
            }
            catch(Exception e)
            {
 
            }

            z = x + y;
            sum =  String.valueOf(z);
            g.drawString("The Sum is: ",5,50);
            g.drawString(sum,95,50);
    }

    public boolean action(Event event, Object obj)
    {
          repaint();
          return true;
    }
}

/*


*/
Output

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="">

Thursday, 28 August 2014

Inheritance, Interfaces, Abstract Classes and Access Specifiers!

//Program for Single_Inheritance
import java.io.*;
class Student
{
    private int roll_no;
    private String name;

    void get_data(int roll_no,String name)
    {
        this.roll_no = roll_no;
        this.name = name;
    }

    void display()
    {
        System.out.print("\nThe Student Details are:");
        System.out.print("\nRoll no: "+roll_no);
        System.out.print("\nName: "+name);
    }
}


class Marks extends Student
{
    private int mark1,mark2;

    Marks(int mark1,int mark2)
    {
        this.mark1 = mark1;
        this.mark2 = mark2;
    }

    void display_marks()
    {
        System.out.print("\nMarks1: "+mark1);
        System.out.print("\nMarks2: "+mark2);

          float avg;
          avg = (mark1+mark2)/2;
          System.out.println("\nAverage: "+avg);
    }
}

public class Single_Inheritance
{
    public static void main(String s[]) throws Exception
    {
          int roll_no,mark1,mark2;
          String name;
         
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         
          System.out.print("\nEnter Roll_no: ");
          roll_no = Integer.parseInt(br.readLine());

          System.out.print("\nEnter Name: ");
          name = br.readLine();

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

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

          Marks m = new Marks(mark1,mark2);
          m.get_data(roll_no,name);

          m.display();
          m.display_marks();
    }
}
Output



//Program for Multilevel Inheritance
import java.io.*;
class A
{
     private int a;
    
     void get_a(int a)
     {
        this.a = a;
     }

     void display_a()
     {
        System.out.print("\nA: "+a);
     }
}

class B extends A
{
     private int b;
    
     void get_b(int b)
     {
        this.b = b;
     }

     void display_b()
     {
        System.out.print("\nB: "+b);
     }
}

class C extends B
{
     private int c;
    
     void get_c(int c)
     {
        this.c = c;
     }

     void display_c()
     {
        System.out.println("\nC: "+c);
     }
}

public class Multilevel_inheritance
{
     public static void main(String s[]) throws Exception
     {
          int a,b,c;
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         
          System.out.print("\nEnter a: ");
          a = Integer.parseInt(br.readLine());

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

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

          C abc=new C();

          abc.get_a(a);
          abc.get_b(b);
          abc.get_c(c);

          abc.display_a();
          abc.display_b();
          abc.display_c();
     }
}
Output


//Program for Abstract class
abstract class A
{
     abstract void a();
     abstract void b();
     void terminate()
     {
        System.out.print("\nTerminating.....");
     }
}

class B extends A
{
     void a()
     {
        System.out.print("\nStarting..... A");
     }
     void b()
     {
        System.out.print("\nStarting..... B");
     }
}

class Abstract_Demo
{
     public static void main(String s[]) throws Exception
     {
          B b1=new B();
          b1.a();
          b1.terminate();
          b1.b();
          b1.terminate();
          System.out.println();
     }
}
Output



//Program for Interface
interface A
{
    void get_A(int a);
}

interface B
{
    void get_B(int b);
}

interface C
{
    void get_C(int c);
}

class D implements A,B,C
{
     int a1,b1,c1;
     public void get_A(int a)
     {
        a1 = a;
     }
     public void get_B(int b)
     {
        b1 = b;
     }
     public void get_C(int c)
     {
        c1 = c;
     }
     void display()
     {
          System.out.print("\nA: "+a1);
          System.out.print("\nB: "+b1);
          System.out.println("\nC: "+c1);
     }
}

class InterfaceDemo
{
     public static void main(String soham[]) throws Exception
     {
          D d = new D();
          d.get_A(10);
          d.get_B(20);
          d.get_C(30);
          d.display();
     }
}

Tuesday, 26 August 2014

Method Overriding, Constructor Overloading, Scanner class

//Program for Method Overriding
class Bank
{
    int rate_of_interest()
    {
        return 0;
    }
}

class SBI extends Bank
{
    //override
    int rate_of_interest()
    {
        return 8;
    }
}

class ICICI extends Bank
{
    //override
    int rate_of_interest()
    {
        return 7;
    }
}

class AXIS extends Bank
{
    //override
    int rate_of_interest()
    {
        return 9;
    }
}

public class Method_Overriding
{
    public static void main(String soham[])
    {
        SBI sbi = new SBI();

        ICICI icici = new ICICI();

        AXIS axis = new AXIS();
          
        System.out.print("\nSBI Rate of Interest: " + sbi.rate_of_interest());

        System.out.print("\nICICI Rate of Interest: " + icici.rate_of_interest());

        System.out.print("\nAXIS Rate of Interest: " + axis.rate_of_interest());

        System.out.println();
    }
}
Output

//Program for Constructor Overloading
class Cube
{
    int length;
    int breadth;
    int height;

    Cube()                                                            //constructor 1        
    {
        length = 10;
        breadth = 10;
        height = 10;
    }

    Cube(int length, int breadth, int height)   //constructor 2
    {
        this.length = length;
        this.breadth = breadth;
        this.height = height;
    }

    public int get_volume()
    {
        return (length * breadth * height);
    }
 }

public class Constructor_Demo
{
    public static void main(String soham[]) throws Exception
    {
         Cube cube_obj1 = new Cube();
         Cube cube_obj2 = new Cube(10, 20, 30);
        System.out.println("Volume of Cube1 is : " + cube_obj1.get_volume());
        System.out.println("Volume of Cube1 is : " + cube_obj2.get_volume());
    }
}
Output

//Program for Scanner Class
import java.util.Scanner;
public class Scanner_Demo
{
   public static void main(String soham[]) throws Exception
  {
      Scanner sc = new Scanner(System.in);
  
      System.out.print("\nEnter your rollno: ");
       int rollno = sc.nextInt();

      System.out.print("\nEnter your name: ");
      String name = sc.next();

      System.out.print("\nEnter your fee: ");
      double fee = sc.nextDouble();

      System.out.print("\nStudents Details Are: ");
      System.out.print("\nRoll no: "+rollno);
      System.out.print("\nName: "+name);
      System.out.print("\nFee: "+fee);

       System.out.println();
   }
}
Output

Friday, 22 August 2014

White Spaces, Data Types and Array Of Objects.

//Program for White Space
import java.io.*;
class WhiteSpace
{
    public static void main(String soham[]) throws Exception
    {
        String str;

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nEnter String: ");
        str = br.readLine();

        System.out.print("\nIn this statement, back space is used: "+str+"\b ");
        System.out.print("\nIn this statement, form feed is used: \f "+str+" \f ");
        System.out.print("\nIn this statement, new line is used: \n"+str);
        System.out.print("\nIn this statement, horizontal tab is used: \t"+str);
        System.out.print("\nIn this statement, single quote is used: \'"+str+"\'");
        System.out.print("\nIn this statement, double quote is used: \""+str+"\"");
        System.out.print("\nIn this statement, backslash is used: \\"+str);
        System.out.println();
    }
}
Output

//Program for size of the Datatypes and their Default values

class Datatypes
{
    static int sizeof(byte b)
    {
        return 1;
    }
    static int sizeof(short s)
    {
        return 2;
    }
    static int sizeof(int i)
    {
        return 4;
    }
    static int sizeof(long l)
    {
        return 8;
    }
    static int sizeof(float f)
    {
        return 4;
    }
    static int sizeof(double d)
    {
        return 8;
    }

    public static void main(String soham[]) throws Exception
    {
        byte b = 0;
        short s = 0;
        int i = 0;
        long l = 0;
        float f = 0.0f;
        double d = 0.0d;
     
        System.out.println("\nDatatypes"+" Size"+"\tDefault values");
        System.out.println("byte:\t  "+sizeof(b)+" \t0");
        System.out.println("short:\t  "+sizeof(s)+" \t0");
        System.out.println("int:\t  "+sizeof(i)+" \t0");
        System.out.println("long:\t  "+sizeof(l)+" \t0L");
        System.out.println("float:\t  "+sizeof(f)+" \t0.0f");
        System.out.println("double:\t  "+sizeof(d)+" \t0.0d");

        System.out.println();
    }
}
Output

//Program for Print() And Println() method

class Print_Demo
{
    public static void main(String soham[]) throws Exception
    {
        System.out.println("\nWelcome To");
        System.out.print("Java Programming");
        System.out.println();
    }
}
Output

//Program for Array Of Object

import java.io.*;
class Employee
{
    String name;
    int emp_id;
    int salary;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public void get_data()
    {
        try
        {
           System.out.print("\nEnter name: ");
           name = br.readLine();
           System.out.print("\nEnter emp id: ");
           emp_id = Integer.parseInt(br.readLine());
           System.out.print("\nEnter salary: ");
           salary = Integer.parseInt(br.readLine());
        }
        catch(Exception e)
        {
            System.out.print("\n"+e);
        }
    }

    public void display()
    {
        System.out.print("\nName: "+name);
        System.out.print("\nEmp id: "+emp_id);
        System.out.print("\nSalary: "+salary);
        System.out.println();
    }
}

public class Array_of_object
{
    public static void main(String s[]) throws Exception
    {
        int i;
        Employee emp[] = new Employee[3];
        for(i=0;i<=2;i++)
        {
            emp[i] = new Employee();
        }
 
        for(i=0;i<=2;i++)
        {
           System.out.print("\nEnter Employee Detail for employee "+(i+1));
           emp[i].get_data();
        }

        for(i=0;i<=2;i++)
        {
           System.out.print("\nEmployee Details for employee "+(i+1));
           emp[i].display();
        }

        System.out.println();
    }
}
Output

Wednesday, 20 August 2014

Class Demo

import java.io.*;
class Student
{
    private int roll_no;
    private String name;
    void get_data(String name,int roll_no)
    {
        this.name=name;
        this.roll_no=roll_no;
    }

    void display()
    {
        System.out.print("\nStudent Details: ");
        System.out.print("\nName: "+name);
        System.out.print("\nRoll_no: "+roll_no);
    }
}

class ClassDemo
{
    public static void main(String s[]) throws Exception
    {
        Student s1=new Student();
        Student s2=new Student();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nEnter Name: ");
        String name = br.readLine();

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

        s1.get_data(name,roll_no);
        s1.display();

        System.out.println();
    }
}
Output

Tuesday, 19 August 2014

Continue Statement

//Program for Continue statement

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

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

        System.out.print("\nOdd Numbers: ");
        for(i=0;i<=n;i++)
        {
            if(i%2==0)
            {
                continue;
            }
            else
            {
                System.out.println("\n"+i);
            }
        }
    }
}
Output
Enter n:10

Odd Numbers:
1

3

5

7

9

Random Number Generator

//Program for generating Random number

import java.io.*;
import java.util.Random;
class Random_number
{
    public static void main(String s[]) throws Exception
    {
        int n;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nEnter number between 1 to 10: ");
        n = Integer.parseInt(br.readLine());

        Random random_num = new Random();
        int r = random_num.nextInt(10);   //generating Random number

        System.out.print("\nRandom number: "+r);

        if(n == r)
        {
            System.out.print("\nYou won...");
        }
        else
        {
            System.out.print("\nYou lose!!!");
        }

        System.out.println();
    }
}
OUTPUT 1:-

Enter number between 1 to 10: 5

Random number: 5
You won...

OUTPUT 2:-

Enter number between 1 to 10: 2

Random number: 8
You lose!!!