Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Saturday 30 June 2012

program to catch ARITHMATIC EXCEPTION and ARRAYINDEXOUTBONDS EXCEPTION that is demostartion of used of mulitple catch in java


class MultiCatch
{
   
    public static void main(String args[])
     {
        try
        {
          int a = args.length;
          System.out.println("a = " + a);
          int b = 42 / a;
          int c[] = { 1 };
          c[42] = 99;
            }

program to design applet with two Text boxes and a Button.If both Text boxes contain a Number then add two numbers. If both contain String then concate otherwise raise user defined Exception in java.

import java.awt.event.*;
import java..*;
import java.awt.*;
import java.io.*;
/*
<applet code="Exception" width=700 height=300>
</>
*/
public class Exception extends Applet implements ActionListener
{
  String msg=" ",msg1=" ",sum=" ",con=" ";
  String str3;
  Button  Press;
  TextField  T1,T2;
  int n1,n2,ans;
 
    public void init()
     {
       setFont(new Font("Times New Roman",Font.PLAIN,15));
        
       Label  L1=new Label("Enter No. Or String : ",Label.RIGHT);
       T1=new TextField(15);
       Label  L2=new Label("Enter No. Or String : ",Label.RIGHT);
       T2=new TextField(15);
       Press=new Button("ADD/CONCATE");
        
       add(L1);
       add(T1);
       add(L2);
       add(T2);
       add(Press);
       
       T1.addActionListener(this);
       T2.addActionListener(this);
       Press.addActionListener(this);
     }
    public void start()
     {
       msg="NO STRINGS ARE ENTERED....";
       msg1="NO NUMBERS ARE FOUND.....";
     }
       
    public void actionPerformed(ActionEvent ae)
     {

       String s1=ae.getActionCommand();
       String s2=ae.getActionCommand();
       s1=T1.getText();
       s1=s1.trim();
       s2=T2.getText();
       s2=s2.trim();

       try
       {
         n1=Integer.parseInt(s1);
         n2=Integer.parseInt(s2);

         ans=n1+n2;
         con="RESULT FOR CONCATATION";
         sum="RESULT FOR ADDITION";
         msg="THIS TIME ADDITION IS PERFORMED....";
         msg1="YOUR SUM OF ENTERED NO.IS  :  "+ans;
         repaint();
 
       }catch(NumberFormatException nf)

program to demostrate used of FileInputStream and FileOutputStream to copy file in java.

import java.io.*;

class CopyFile {
  public static void main(String args[])
    throws IOException
  {
    int i;
    FileInputStream fin;
    FileOutputStream fout;

    try {
      // open input file
      try {
        fin = new FileInputStream(args[0]);
      } catch(FileNotFoundException e) {
        System.out.println("Input File Not Found");
        return;
      }

      // open output file
      try {
        fout = new FileOutputStream(args[1]);
      } catch(FileNotFoundException e) {
        System.out.println("Error Opening Output File");
        return;
      }
    } catch(ArrayIndexOutOfBoundsException e) {
      System.out.println("Usage: CopyFile From To");
      return;
    }

    // Copy File
    try {
      do {
        i = fin.read();
        if(i != -1) fout.write(i);
      } while(i != -1);
    } catch(IOException e) {
      System.out.println("File Error");
    }

    fin.close();
    fout.close();
  }
}

program to create a two level card deck that allows a user to select an operating system where windows based operating systems are displayed in one card and other programming operating system in other card in java.


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
 /*
  <applet code="clayout" width=300 height=100>
  </applet>
 */
  public class clayout extends Applet implements ActionListener,MouseListener
{
  Checkbox Win98,winNT,solaris,mac;
  Panel osCards;
  CardLayout cardLO;
  Button Win,Other;

  public void init()
  {
    Win = new Button("Windows");
    Other = new Button("Other");
    add(Win);
    add(Other);
    cardLO = new CardLayout();
    osCards = new Panel();
    osCards.setLayout(cardLO); //set panel layout to card layout
    Win98 = new Checkbox("Windows 98/xp",null,true);
    winNT = new Checkbox("Windows NT/2000");
    solaris = new Checkbox("Solaris");
    mac = new Checkbox("MacOS");

program to demostrate used of TextField and TextArea within applet.

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

/* <applet code="texts" width=200 height=300>
   </applet>
*/

public class texts extends Applet
{
      public void init()
      {
            add(new TextField("Sample String ",20));
            setFont(new Font("Times Roman ",Font.ITALIC,8));
            add(new TextField("Hello Word"));
            setFont(new Font("Helvetica ",Font.BOLD,18));
            add(new TextArea("Scratch Pad on Ice ",5,20));
      }
}

program that demostrate GridBagLayout using applet.

import java.awt.*;

/* <applet code = "gridbag" width=200 height=300 >
   </applet>
*/

public class gridbag extends java.applet.Applet
{
      public static void addComponent(Container container,Component component,int gridx,int gridy,int gridwidth,int gridheight,int fill,int anchor) throws AWTException
      {
            LayoutManager lm = container.getLayout();
            if(!(lm instanceof GridBagLayout))
            {
                  throw new AWTException("Invalid Layout"+lm);
            }
            else
            {

program that demostrate example of simple menu using applet.


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/* <applet code="simplemenu" height=300 width=200>
   </applet>
*/
public class simplemenu extends Frame
{
      simplemenu()
      {
            super("Menu Example");
            Menu m = new Menu("File",true);
            m.add(new MenuItem("New Web Browser ",new MenuShortcut('n')));
            m.add(new MenuItem("New Mail Message ",new MenuShortcut('n',true)));
            m.add(new MenuItem("New Folder"));
            m.addSeparator();
            m.add(new MenuItem("Close"));
            m.add(new MenuItem("Quit"));
           
            MenuBar mb = new MenuBar();
            mb.add(m);
            setMenuBar(mb);
            resize(200,200);
       }
       public static void main(String args[])
       {
             simplemenu f = new simplemenu();
             f.show();
       }
}
      

program to demostrate used of choice box using applet.

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

/* <applet code="choicebox" width=200 height=300 >
   </applet>
*/

public class choicebox extends Applet
{
      public void init()
      {
            String [] fonts;
            fonts = Toolkit.getDefaultToolkit().getFontList();

            Choice c = new Choice();           
            Choice b = new Choice();
           
            for ( int i=1 ; i<fonts.length ; i++)
            {
                  c.addItem(fonts[i]);
                  b.addItem(fonts[i]);
            }
            add(c);
            add(b);
       }
}

program to display banner using applet.

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

/* <applet code="banner" width=300 height=200>
   </applet> */

 public class banner extends Applet implements Runnable 
   {
     Font f;
    String mgs="welcome to applet programming language...";
    Thread t=null;
    int state;
    boolean stopflag;

program that performed various operation +,-,*,/ (calculator type) using applet.

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

/* <applet code = "appcalc" width=100 height=200 >
   </applet> */

public class appcalc extends Applet implements ActionListener
  {
   TextField no1,no2,op;
   int ino1,ino2;
   float result=0.0f;

Friday 29 June 2012

program that display child Frame Window within an parent in applet.



import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="AppletFrame" width=300 height=50>
</applet>
*/
// Create a subclass of Frame
class SampleFrame extends Frame {
SampleFrame(String title) {
super(title);
// Create an object to handle window events
 MyWindowAdapter adapter = new MyWindowAdapter(this);
 // register it to receive those events
 addWindowListener(adapter);
 }
 public void paint(Graphics g) {
 g.drawString("This is in Frmae Window", 10, 40);
 } }

java program that demostrate use of static method and static variable.

class staticdemo
{
    static int a =42;
    static int b = 99;
    static void callme()
        {
        System.out.println("A = "+a);
        }
}
class static_example
{
    public static void main(String args[])
        {
            final int d =1987;
            System.out.println("final d= "+d);
            //CALL STATIC METHOD
        staticdemo.callme();
        System.out.println("B ="+staticdemo.b);
        }
}

java program that demostrate the key event handlers.

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

 /*
  <applet code="SimpleKey" width=300 height=100>
  </applet>
 */
 public class SimpleKey extends Applet
 implements KeyListener {
  String msg="";
 int X=10, Y=20; // output coordinates
 public void init() {
 addKeyListener(this);
 requestFocus(); //request input focus
 }
 public void keyPressed(KeyEvent ke) {
 showStatus("Key Down");
 }

java program that performed pattern matching in input string.

import java.io.*;

class paturn_match
{
       public static void main(String args[]) throws IOException
       {
             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
             char test[] = new char[100];
             char given_str[] = new char[15];
             String temp;
             int i,j,lentext,lenp,flag=0,count=0;
            
             System.out.print("Enter Text  =  ");
             temp = br.readLine();
             test = temp.toCharArray();
             lentext = temp.length();
            
             System.out.print("Enter Paturn  =  ");
             temp = br.readLine();
             given_str = temp.toCharArray();
             lenp = temp.length();

             for(i=0;i!=lentext-1;i++)
             {
                   j=i;
                   int k=0;
                   flag=0;
                         while((test[i] == given_str[k]) && (flag !=1))
                         {
                               j=j+1;
                               k=k+1;
                                    if(k>=(lenp-1))
                                    {
                                          flag=1;
                                          break;
                                    }
                         } //WHILE
                        
                         if((k>0) && (flag==1))
                         {
                               System.out.println(" Paturn found at  " + i);
                               count++;
                         }
            }  // FOR LOOP
           
            System.out.println(" count = " + count);
    }
} //paturn_match.java

java program that take input of student details and subject marks and generate marksheet(result) for student.

import java.io.*;
class marks
    {
        int sub1,sub2,sub3,sub4,sub5,sub6,tot;
        float percentage;
        String stud_name,sid,g;
        public marks(String stud_name,String sid,int sub1,int sub2,int sub3,int sub4,int sub5,int sub6,int tot,float percentage)
            {
                this.stud_name=stud_name;
                this.sid=sid;
                this.sub1=sub1;
                this.sub2=sub2;
                this.sub3=sub3;   
                this.sub4=sub4;
                this.sub5=sub5;
                this.sub6=sub6;
                this.tot=tot;
                this.percentage=percentage;
            }

java program to create Thread by using Runnable interface

// controlling the main thread
class mynewthread implements Runnable
{
  
      Thread t;
      mynewthread()
      {
         // create new thread
         t= new Thread(this,"demo thread");
         System.out.println("child thread :"+t);
         t.start();
      }
      public void run()
       {
         try
         {

java program to display star using applet.

import java.awt.*;

/* <applet code ="star" height=300 width=200>
   </applet>
*/

public class star extends java.applet.Applet
{
      public void paint(Graphics g)
      {
            g.setFont ( new Font ("Helvetica",Font.BOLD,20));
            g.drawString("HI",112,112);
            g.setColor(Color.red);
            g.drawLine(50,75,200,75);
        

java program to display and performed operation on scroll bar using applet.


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code = "scroll" height=150 width=150>
   </applet>
*/

public class scroll extends Applet implements ActionListener,ContainerListener
{
      ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
      public void init()
      {
        

java program to display paint brush that is to display current mouse coursor that is used of varoius method of MouseLIstener and MouseMotionListerner interface using applet

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="paintbrush" width=500 height=400>
</applet>
*/
public class paintbrush extends Applet implements MouseListener,MouseMotionListener
{
String msg=" ";
int mouseX,  mouseY,x,y,a,b,x1,y1;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}

java program to display list box using applet.

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

/*<applet code = "listbox" width=200 height=300>
   </applet>
*/

public class listbox extends Applet
{
      public void init()
      {