NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

Sunday, October 21, 2012

Java program to implement single inheritance


import java.io.*;
  class employee
  {
    private String empname;
    private  int empcode;

  void read(String s,int p)
  {
    empname= s;
    empcode=p;
  }
 void display()
  {
    System.out.println("The employee name is :" +empname);
    System.out.println("The employee code is :"+empcode);
  }
 }

 class labourer extends employee
 
  { private String labourtype;
    private int laboursalary;

  void read1()throws IOException
 
    {
       BufferedReader br= new BufferedReader( new InputStreamReader (System.in));
       System.out.println("Enter the employee name:");
       String a=br.readLine();
       System.out.println("Enter the employee code :");
       int p= Integer.parseInt(br.readLine());
       read(a,p);
       System.out.println("Enter the labourtype :");
       labourtype = br.readLine();
       System.out.println("Enter the labour salary :");
       laboursalary=Integer.parseInt(br.readLine());
    }

   void display1()
 
    {
    
      display();
      System.out.println(" The labour type is : "+ labourtype);
      System.out.println(" The labour salary is :"+laboursalary);   }
 }
class single   
    { public static void main (String s[])throws IOException
      {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the number of employees");
        int n = Integer.parseInt(br.readLine());
        labourer l[]= new labourer[n];
        for(int i=0; i<n; i++)
        l[i]= new labourer();
        for(int i=0; i<n; i++)
        l[i] .read1();
        for(int i=0; i<n; i++)
      l[i].display1(); }
    }
Output

Java program to animated text or moving text


import java.applet.*;
import java.awt.*;
/*<applet code="runtext" width=450 height=400>
</applet>*/

public class runtext extends Applet implements Runnable
 {
   Thread t;
   int x,y;
  boolean stopflag;
            public void init()
             {
               setBackground(Color.black);
               setForeground(Color.white);
               x=120;y=130;
              
             }

            public void start()
              {
                stopflag=false;
                t=new Thread(this,"one");
                t.start();
                       
              }

            public void paint(Graphics g)
              {showStatus("name of college");
                g.drawString("SS College",x,y);
              }

            public void stop()
              {
                stopflag=true;
                t=null;
              }

            public void run()
              {
                for(; ;)
                        {
                          try
                            {
                                    y++;x++;
                                    repaint();
                                    Thread.sleep(50);
                                    if(stopflag) break;
                            }
                         catch(InterruptedException e)
                          {
                                    System.out.println(e);
                          }
                        }
             }
}
Output

Java program to illustrate menubar with the help of applet


import java.applet.*;
import java.awt.*;
 class mymenu extends Frame
{
            MenuBar mb;
            Menu filemenu,editmenu,viewmenu;
            MenuItem open,newmi,save,cut,copy;
            CheckboxMenuItem toolbar,layer;

public mymenu()
{
            mb=new MenuBar();
            setMenuBar(mb);
           
            filemenu=new Menu("File");
            editmenu=new Menu("Edit");
            viewmenu=new Menu("View");

            open=new MenuItem("Open");
            newmi=new MenuItem("New");
            save=new MenuItem("Save");
            cut=new MenuItem("Cut");
            copy=new MenuItem("Copy");

            toolbar=new CheckboxMenuItem("Toolbar",true);
            layer=new CheckboxMenuItem("Layer Window",false);

            filemenu.add(open);
            filemenu.add(newmi);
            filemenu.add(save);

            editmenu.add(cut);
            editmenu.add(copy);

            viewmenu.add(toolbar);
            viewmenu.add(layer);

            mb.add(filemenu);
            mb.add(editmenu);
            mb.add(viewmenu);
}

public static void main(String s[])
{
            mymenu mn=new mymenu();
             mn.setSize(new Dimension(500,500));
            mn.setTitle("Menu Bar");
            mn.setVisible(true);
}
}
Output

Java program to create login form using applet


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Loginform extends Frame implements ActionListener
{ Label l1=new Label("user name");
  Label l2=new Label("password");
  Label l3=new Label(" ");
  TextField t1=new TextField();
  TextField t2=new TextField();
  Button b= new Button("Submit");
  public Loginform()
  { add(l1);
    add(t1);
    add(l2);
    add(t2);
    add(b);
    add(l3);
    l1.setBounds(20,45,70,20);
    t1.setBounds(180,45,200,20);
    l2.setBounds(20,95,70,20);
    t2.setBounds(180,95,200,20);
    b.setBounds(310,145,70,20);
    b.addActionListener(this);
    t2.setEchoChar('*');
    addWindowListener(new mwa());
  }
  public void actionPerformed(ActionEvent e)
  { l3.setText("Welcome "+t1.getText());}
  public static void main(String s[])
  { Loginform l=new Loginform();
    l.setSize(new Dimension(600,600));
    l.setTitle("Login");
    l.setVisible(true);
  }
}
class mwa extends WindowAdapter
{ public mwa(){}
  public void windowClosing(WindowEvent e)
  { System.exit(0);
  }
}
Output

Java program to print fibonacci series upto a given number

import java.io.*;
class Fibonacci
{ int next,curr,prev,num;
            Fibonacci(int a)
  { next=1;
     curr=1;
    prev=0;
    num=a;  }
  void calc()
  { while(prev<=num)
    { System.out.println(prev);
      prev=curr;
      curr=next;
      next=prev+curr;  }
  }
  public static void main(String s[])throws IOException
  { BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the limit");
    int a=Integer.parseInt(br.readLine());
    Fibonacci f=new Fibonacci(a);
    f.calc(); }
}
Output


Java program to illustrate exception handling


import java.lang.ArithmeticException;
import java.util.*;                                //import util package
public class ExceptionDemo
{  public static void main(String s[])
  {   Scanner b1=new Scanner(System.in);
    try
     {  System.out.println("Enter the Divident?");
        double a=b1.nextDouble();
        System.out.println("Enter the Divisor?");
        double b=b1.nextDouble();
      if(b==0)
  //Throwing an Exception using throw
       throw new ArithmeticException("Division by zero!");
       double res=a/b;
       System.out.println("Result of"+a+"/"+b+"is"+res);
     }
  //Handles input mismatch exception caused by
 //entering a non-integer value at th point
  catch(InputMismatchException e)
       {  System.out.println("Error:"+e);}
//Handles the divide-by-zero ArithmeticException
  catch(ArithmeticException ae)
      {  System.out.println("Error:"+ae);}
   }
}
Output