NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

Sunday, October 21, 2012

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

Java program to find distance between two points in a plane


import java.io.*;
class distance
{
            int x,y,dist;
            distance()
             {  x=0;
              y=0;
 }
  void calc(distance c,distance d)
  {
     int xd=c.x-d.x;
     int xs=xd*xd;
     int yd=c.y-d.y;
     int ys=yd*yd;
     int sum=xs+ys;
     double dist=Math.sqrt(sum);
     System.out.println("The distance is "+dist);
  }
  void read()throws IOException
  { 
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("Enter the x coordinate");
     x=Integer.parseInt(br.readLine());
     System.out.println("Enter the y coordinate");
     y=Integer.parseInt(br.readLine());
     System.out.println("The entered point is "+"("+x+","+y+")");
  }
  public static void main(String s[])throws IOException
  {
     distance c=new distance();
     distance d=new distance();
     System.out.println("First point");
     c.read();
     System.out.println("Second point");
     d.read();
     d.calc(c,d);
  }
}
Output

Java program to perform complex number addition


import java.io.*;
class complex
{
   int r,i;
   complex()
{
   r =0;
   i=0;
}
  void sum(complex c,complex d)
{
complex a=new complex();
a.r=c.r+d.r;
a.i=c.i+d.i;
     System.out.println("The sum is "+a.r+"+"+a.i+"i");
  }
  void read()throws IOException
  {
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("\nEnter the real part");
   r=Integer.parseInt(br.readLine());
  System.out.println("\nEnter the imaginary part");
    i=Integer.parseInt(br.readLine());
  System.out.println("\nYou have entered : "+r+"+"+i+"i");
  }
  public static void main(String s[])throws IOException
  { 
     complex c=new complex();
     complex d=new complex();
     System.out.println("First number");
     c.read();
     System.out.println("\nSecond number");
     d.read();
     d.sum(c,d);
  }
}
Output