Showing posts with label Java Lab Programs. Show all posts
Showing posts with label Java Lab Programs. Show all posts

Write a program to implement keyboard events.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class KeyBoard extends Applet implements KeyListener
{
    String msg=" ";
    public void init()
    {
        addKeyListener(this);
        requestFocus();
    }
    public void keyTyped(KeyEvent ke)
    {
        msg=ke.getKeyChar();
        repaint();
    }
    public void paint(Graphics g)
    {
        g.drawString(msg,100,50);
    }
    public void keyReleased(KeyEvent ke)
    {
        msg="mouseReleased";
        repaint();
    }
    public void keyPressed(KeyEvent ke)
    {
        int k=ke.getKeyCode();
        switch(k)
        {
            case KeyEvent.VK_F1:msg="F1";
                break;
            case KeyEvent.VK_F2:msg="F2";
                break;
            case KeyEvent.VK_F3:msg="F3";
                break;
            case KeyEvent.VK_PAGE_UP:msg="Pageup";
                break;
            case KeyEvent.VK_PAGE_DOWN:msg="Pagedown";
                break;
            case KeyEvent.VK_LEFT:msg="Left arrow";
                break;
            case KeyEvent.VK_RIGHT:msg="Right arrow";
                break;
        }
        repaint();
    }
}


HTML Code
<html>
<title>Keyboard Events</title>
<Body><Applet code="KeyBoard.class" height=100 width=700></applet>
</body>
</html>

Output







Write a program to implement mouse events.

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

public class MouseEvents extends Applet implements MouseListener,MouseMotionListener
{
      String msg=" ";
      public void init()
      {
            addMouseListener(this);
            addMouseMotionListener(this);
      }
    public void paint(Graphics g)
      {
            g.drawString(msg,20,20);
      }
      public void mousePressed(MouseEvent me)
      {
            msg=" mouse pressed";
            repaint();
      }
      public void mouseClicked(MouseEvent me)
      {
           msg=" mouse clicked";
            repaint();
      }
    public void mouseEntered(MouseEvent me)
      {
            msg=" mouse entered";
            repaint();
      }
    public void mouseExited(MouseEvent me)
      {
            msg=" mouse exited";
            repaint();
      }
    public void mouseReleased(MouseEvent me)
      {
            msg=" mouse released";
            repaint();
      }
      public void mouseMoved(MouseEvent me)
      {
            msg=" mouse moved";
            repaint();
      }
      public void mouseDropped(MouseEvent me)
      {
            msg=" mouse dropped";
            repaint();
      }
      public void mouseDragged(MouseEvent me)
      {
            msg=" mouse dragged";
        repaint();
      }
}


HTML Code

<html>
<title>Keyboard Events</title>
<Body><Applet code="MouseEvents.class" height=100 width=700></applet>
</body>
</html>


Output




Write a program to implement thread, applets and graphics by implementing animation of ball moving.

import java.awt.*;
import java.applet.*;
public class Movingball extends Applet implements Runnable
{
    int x,y,dx,dy;
    Thread t;
    boolean flag;
    public void init()
    {
        setBackground(Color.blue);
        x=100;
        y=10;
        dx=10;
        dy=10;
    }
    public void start()
    {
        flag=true;
        t=new Thread(this);
        t.start();
    }
    public void paint(Graphics g)
    {
        g.setColor(Color.red);
        g.fillOval(x,y,50,50);
    }
    public void run()
    {
        while(flag)
        {
            Rectangle r=getBounds();
            if((x+dx<=0)||(x+dx>=r.width))
                dx=-dx;
            if((y+dy<=0)||(y+dy>=r.height))
                dy=-dy;
            x+=dx;
            y+=dy;
            repaint();
            try
            {
                Thread.sleep(300);
            }
            catch(InterruptedException e)
            {}
        }
    }   
    public void stop()
    {
        t=null;
        flag=false;
    }
}

HTML Code
<html>
<title>Keyboard Events</title>
<Body><Applet code="Movingball.class" height=100 width=700></applet>
</body>
</html>

Output


Write a program to implement thread priorities.

class A extends Thread
{
    public void run()
       {
        for(int i=1;i<=10;i++)
            {
                System.out.println("i="+i);
             }
       } 
}
class B extends Thread
{
       public void run()
       {
              for(int j=1;j<=10;j++)
              {
                 System.out.println("j="+j);
              }
       }
}
class C extends Thread
{
        public void run()
        {
               for(int k=1;k<=10;k++)
               {
                  System.out.println("k="+k);
               }
        }
}
class ThreadPrior
{
        public static void main(String arg[])
        {
               A thA=new A();
               B thB=new B();
               C thC=new C();
               thA.setPriority(1);
               thB.setPriority(3);
               thC.setPriority(9);
               thA.start();
               thB.start();
               thC.start();
               System.out.println("End of main");
        }
}

Output

C:\Users\Jaisha\Desktop\Java>javac ThreadPrior.java

C:\Users\Jaisha\Desktop\Java>java ThreadPrior
End of main
j=1
i=1
k=1
i=2
j=2
i=3
k=2
i=4
j=3
i=5
k=3
i=6
j=4
i=7
k=4
i=8
j=5
i=9
k=5
i=10
j=6
k=6
j=7
k=7
j=8
k=8
j=9
k=9
j=10
k=10

Write a program to calculate bonus for different departments using method overriding.

import java.util.*;
abstract class dept
{
    double bp;
    dept(double bpay)
    {   
        bp=bpay;
    }
    void disp()
    {
        System.out.println("basicpay="+bp);
    }
    abstract double bonus();
}
class sales extends dept
{
    sales(double bpay)
    {
        super(bpay);
    }
    public double bonus()
    {
        return(0.20*bp);
    }
}
class marketing extends dept
{
    marketing(double bpay)
    {
        super(bpay);
    }
    public double bonus()
    {
        return(0.30*bp);
    }
}
class hr extends dept
{
    hr(double bpay)
    {
        super(bpay);
    }
    public double bonus()
    {
        return(0.50*bp);
    }
}
class MethodOverload
{
    public static void main(String arg[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("enter basic pay");
        double bp=sc.nextDouble();
        sales s=new sales(bp);
        s.disp();
        System.out.println("bonus for sales dept=" +s.bonus());
        marketing m=new marketing(bp);
        m.disp();
        System.out.println("bonus for marketing dept=" +m.bonus());
        hr h=new hr(bp);
        h.disp();
        System.out.println("bonus for hr dept=" +h.bonus());
    }
}


Output

C:\Users\Jaisha\Desktop\Java>javac MethodOverload.java

C:\Users\Jaisha\Desktop\Java>java MethodOverload
enter basic pay
5000
basicpay=5000.0
bonus for sales dept=1000.0
basicpay=5000.0
bonus for marketing dept=1500.0
basicpay=5000.0
bonus for hr dept=2500.0

Write a program to create student report using applet, read the input using text boxes and display the o/p using buttons.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class StudReport extends Applet implements ActionListener
{
    Label lblTitle,lblRegNo,lblName,lblJava,lblSE,lblCA,lblBI,lblSSPD;
    TextField txtRegNo,txtName,txtJava,txtSE,txtCA,txtBI,txtSSPD;
      Button cmdReport;
      int total;
      float avg;
 
      public void init()
      {
            setLayout(null);
            lblTitle=new Label("Enter Student's Details");
            lblRegNo=new Label("Reg. No:");
            lblName=new Label("Name:");
            lblJava=new Label("Java:");
            lblSE=new Label("SE:");
            lblCA=new Label("CA:");
            lblBI=new Label("BI:");
            lblSSPD=new Label("SSPD:");
        txtRegNo=new TextField(10);
            txtName=new TextField(25);
            txtJava=new TextField(3);
            txtSE=new TextField(3);
            txtCA=new TextField(3);
            txtBI=new TextField(3);
            txtSSPD=new TextField(3);
        cmdReport=new Button("View Student Result");
        lblTitle.setBounds(100,0,200,20);
        lblRegNo.setBounds(0,50,100,20);
            txtRegNo.setBounds(120,50,100,20);
        lblName.setBounds(0,75,100,20);
         txtName.setBounds(120,75,250,20);
        lblJava.setBounds(0,100,100,20);
            txtJava.setBounds(120,100,40,20);
        lblSE.setBounds(0,125,100,20);
            txtSE.setBounds(120,125,40,20);
        lblCA.setBounds(0,150,100,20);
        txtCA.setBounds(120,150,40,20);
        lblBI.setBounds(0,175,100,20);
        txtBI.setBounds(120,175,40,20);
        lblSSPD.setBounds(0,200,100,20);
        txtSSPD.setBounds(120,200,40,20);
        cmdReport.setBounds(100,225,150,30);
        add(lblTitle);
            add(lblRegNo);add(txtRegNo);
            add(lblName);add(txtName);
            add(lblJava);add(txtJava);
            add(lblSE);add(txtSE);
            add(lblCA);add(txtCA);
            add(lblBI);add(txtBI);
            add(lblSSPD);add(txtSSPD);
            add(cmdReport);
        cmdReport.addActionListener(this);
    }
public void actionPerformed(ActionEvent ae)
{
        try
        {
               int java=Integer.parseInt(txtJava.getText());
               int se=Integer.parseInt(txtSE.getText());
               int ca=Integer.parseInt(txtCA.getText());
               int bi=Integer.parseInt(txtBI.getText());
               int sspd=Integer.parseInt(txtSSPD.getText());
        total=(java+se+ca+bi+sspd);
        avg=total/5;
        }
        catch(NumberFormatException e)
        {
        }
        repaint();
}

public void paint(Graphics g)
    {
            g.drawString("STUDENT REPORT",100,275);
            g.drawString("Reg. No.: "+txtRegNo.getText(),0,300);
            g.drawString("Name : "+txtName.getText(),0,325);
            g.drawString("Java:  "+txtJava.getText(),0,350);
            g.drawString("Software Engineering : "+txtSE.getText(),0,375);
            g.drawString("Computer Architecture : "+txtCA.getText(),0,400);
            g.drawString("Banking & Insurance : "+txtBI.getText(),0,425);
            g.drawString("SSPD : "+txtSSPD.getText(),0,450);
            g.drawString("Total: "+total,0,475);
            g.drawString("Average: "+avg,0,500);
      }
}

HTML Code

<html>
<title>Keyboard Events</title>
<Body><Applet code="StudReport.class" height=100 width=700></applet>
</body>
</html> 


Output











   

Write a program to implement constructor overloading by passing different number of parameter of different types.

import java.util.*;
class box
{
double length,breadth,height;
    box()
    {
        length=0.0;
        breadth=0.0;
        height=0.0;
    }
    box(double p)
    {
        length=p;
        breadth=p;
        height=p;
    }
    box(double l,double b,double h)
    {
        length = l;
        breadth = b;
        height = h;
    }
    void disp()
    {
    System.out.println("length=" +length+ "\tbreadth=" +breadth+ "\theidht=" +height);
    }
}
class ConstOverload
{
    public static void main(String arg[])
    {
        Scanner sc = new Scanner(System.in);
        box b1 = new box();
        System.out.println("default constructor is invoked ");
        b1.disp();
        System.out.println("enter a double value");
        double d = sc.nextDouble();
        box b2 = new box(d);
        System.out.println("one parameter constructor is invoked");
        b2.disp();
        System.out.println("enter length");
        double l = sc.nextDouble();
        System.out.println("enter breadth");
        double b = sc.nextDouble();
        System.out.println("enter height");
        double h = sc.nextDouble();
        box b3 = new box(l,b,h);
        System.out.println("three parameter constructor is invoked");
        b3.disp();
    }
}


Output

C:\Users\Jaisha\Desktop\Java>javac ConstOverload.java

C:\Users\Jaisha\Desktop\Java>java ConstOverload
default constructor is invoked
length=0.0      breadth=0.0     heidht=0.0
enter a double value
23.5
one parameter constructor is invoked
length=23.5     breadth=23.5    heidht=23.5
enter length
6
enter breadth
7
enter height
8
three parameter constructor is invoked
length=6.0      breadth=7.0     heidht=8.0

Write a program to find area of geometrical figures using method.

import java.util.*;
class geofig
{
    double area(double r)
    {
        return(3.14*r*r);
    }
    float area(float s)
    {
        return(s*s);
    }
    float area(float l,float b)
    {
        return(l*b);
    }
    double area(double b,double h)
    {
        return(0.5*b*h);
    }
}

class geo
{
    public static  void main(String arg[])
    {
        Scanner sc =new Scanner(System.in);
        geofig g = new geofig();
        System.out.println("enter double value for radius of circle");
        double r =sc.nextDouble();
        System.out.println("area of circle="+g.area(r));
        System.out.println("enter float value for side of a square");
        float s =sc.nextFloat();
        System.out.println("area of square="+g.area(s));
        System.out.println("enter float value for length and braedth of rectangle");
        float l =sc.nextFloat();
        float b =sc.nextFloat();
        System.out.println("area of rectangle="+g.area(l,b));
        System.out.println("enter double value for base & height of triangle");
        double b1 =sc.nextDouble();
        double h =sc.nextDouble();
        System.out.println("area of triangle="+g.area(b1,h));
    }
}

Output

C:\Users\Jaisha\Desktop\Java>javac geo.java

C:\Users\Jaisha\Desktop\Java>java geo
enter double value for radius of circle
3.5
area of circle=38.465
enter float value for side of a square
3
area of square=9.0
enter float value for length and braedth of rectangle
3 5
area of rectangle=15.0
enter double value for base & height of triangle
2 4
area of triangle=4.0

Write a program to implement all string operations.

import java.util.*;
class StringOperation
{
    public static void main(String[] args)
      {
        String first="",second="";
        Scanner sc=new Scanner(System.in);
            System.out.println("String Operation");
            System.out.println();
            System.out.print("Enter the first Sting: ");
             first=sc.nextLine();
        System.out.print("Enter the second Sting: ");
              second=sc.nextLine();
            System.out.println("The strings are: "+first+" , "+second);
         System.out.println("The length of the first string is :"+first.length());
                System.out.println("The length of the second string is :"+second.length());
            System.out.println("The concatenation of first and second string is :"+first.concat(" "+second));
             System.out.println("The first character of " +first+" is: "+first.charAt(0));
            System.out.println("The uppercase of " +first+" is: "+first.toUpperCase());
            System.out.println("The lowercase of " +first+" is: "+first.toLowerCase());
            System.out.print("Enter the occurance of a character in "+first+" : ");
            String str=sc.next();
             char c=str.charAt(0);
            System.out.println("The "+c+" occurs at position " + first.indexOf(c)+ " in " + first);
             System.out.println("The substring of "+first+" starting from index 3 and ending at 6 is: " + first.substring(3,7));
            System.out.println("Replacing 'a' with 'o' in "+first+" is: "+first.replace('a','o'));       
            boolean check=first.equals(second);
            if(!check)
                System.out.println(first + " and " + second + " are not same.");
            else
                System.out.println(first + " and " + second + " are same."); 
    }
}

Output

C:\Users\Jaisha\Desktop\Java>javac StringOperation.java

C:\Users\Jaisha\Desktop\Java>java StringOperation
String Operation

Enter the first Sting: The World is Beautiful.
Enter the second Sting: Learn to enjoy every moment.
The strings are: The World is Beautiful. , Learn to enjoy every moment.
The length of the first string is :23
The length of the second string is :28
The concatenation of first and second string is :The World is Beautiful. Learn to enjoy every moment.
The first character of The World is Beautiful. is: T
The uppercase of The World is Beautiful. is: THE WORLD IS BEAUTIFUL.
The lowercase of The World is Beautiful. is: the world is beautiful.
Enter the occurance of a character in The World is Beautiful. : e
The e occurs at position 2 in The World is Beautiful.
The substring of The World is Beautiful. starting from index 3 and ending at 6 is:  Wor
Replacing 'a' with 'o' in The World is Beautiful. is: The World is Beoutiful.
The World is Beautiful. and Learn to enjoy every moment. are not same.

Write a program to implement Rhombus pattern reading the limit form user.

import java.util.*;
class Rhombus
{   
    public static void main(String[] arg)   
    {
            Scanner sc=new Scanner(System.in);
              System.out.print("Enter the pattern number: ");
            int pat=sc.nextInt();
              System.out.print("Enter the pattern character symbol: ");
              String s=sc.next();
              char c=s.charAt(0);
        for(int i=1;i<=pat;i++)
              {
                for(int j=1;j<=(pat-i);j++)
                      System.out.print(" ");
            for(int k=1;k<=i;k++)
                      System.out.print(c+" ");
                System.out.println();
             }
        for(int i=pat-1;i>=0;i--)
             {
                for(int j=1;j<=(pat-i);j++)
                      System.out.print(" ");
            for(int k=1;k<=i;k++)
                      System.out.print(c+" ");
                System.out.println();
              }
  
      }
}

Output

C:\Users\Jaisha\Desktop\Java>javac SortOrder.java

C:\Users\Jaisha\Desktop\Java>java SortOrder
Enter the array size: 5
Enter Array elements:
Element No. 1: 5
Element No. 2: 3
Element No. 3: 62
Element No. 4: 1
Element No. 5: 52
Before Sorting:  5 3 62 1 52
After Sorting in ascending order:  1 3 5 52 62
After Sorting in descending order:  62 52 5 3 1

Write a program to sort list of elements in ascending and descending order and show the exception handling.

import java.util.*;
class SortOrder
{
  public static void main(String[] args)
  {
    Scanner sc=new java.util.Scanner(System.in);
    System.out.print("Enter the array size: ");
    int size=sc.nextInt();
        int[] arr=new int[size];
        System.out.println("Enter Array elements: ");
        for(int i=0;i<size;i++)
        {
            System.out.print("Element No. "+(i+1)+": ");
            arr[i]=sc.nextInt();
        }
        System.out.print("Before Sorting: ");
        for(int i=0;i<size;i++)
        System.out.print(" "+arr[i]);
        for(int i=0;i<size;i++)
        {
            int temp;
            for(int j=i+1;j<size;j++)
            {
                if(arr[i]>arr[j])
                {
                        temp=arr[i];
                        arr[i]=arr[j];
                        arr[j]=temp;
                  }
            }
        }
        System.out.println();
        System.out.print("After Sorting in ascending order: ");
        for(int i=0;i<size;i++)
    {
            System.out.print(" "+arr[i]);
    }
    System.out.println();
        System.out.print("After Sorting in descending order: ");
    for(int i=size-1;i>=0;i--)
    {
            System.out.print(" "+arr[i]);
        }

  }
}

Output


C:\Users\Jaisha\Desktop\Java>javac SortOrder.java

C:\Users\Jaisha\Desktop\Java>java SortOrder
Enter the array size: 5
Enter Array elements:
Element No. 1: 5
Element No. 2: 3
Element No. 3: 62
Element No. 4: 1
Element No. 5: 52
Before Sorting:  5 3 62 1 52
After Sorting in ascending order:  1 3 5 52 62
After Sorting in descending order:  62 52 5 3 1

Write a program to display all prime numbers between two limits.

import java.io.*;
class Prime
{
public static void main(String arg[])throws IOException
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("enter a positive lower limit");
    int l=Integer.parseInt(br.readLine());
    System.out.println("enter a positive upper limit");
    int u=Integer.parseInt(br.readLine());
    for(int n=l;n<=u;n++)
    {
        byte flag=1;
        for(int i=2;i<=(int)n/2;i++)
        {
            if(n%i==0)
            {
                flag=0;
                break;
            }
        }
        if(flag==1)
            System.out.print("\t" +n);
    }
}
}

Output

enter a positive lower limit
2
enter a positive upper limit
20
        2       3       5       7       11      13      17      19

Write a program to find factorial of list of number reading input as command line argument.

class Factorial
{
    public static void main(String[] arg)
    {
        int[] num=new int[10];
        if(arg.length==0)
        {
            System.out.println("No Command Line argument passed.");
            return;
        }
        for(int i=0;i<arg.length;i++)
        num[i]=Integer.parseInt(arg[i]);
        for(int i=0;i<arg.length;i++)
        {
            int fact=1;
            for(int j=1;j<=num[i];j++)
            fact *=j;
            System.out.println("The factorial of "+arg[i]+" is : "+fact);
        }
    }
}

Output

C:\Users\Jaisha\Desktop\Java>javac Factorial.java

C:\Users\Jaisha\Desktop\Java>java Factorial
No Command Line argument passed.

C:\Users\Jaisha\Desktop\Java>java Factorial 5 6
The factorial of 5 is : 120
The factorial of 6 is : 720