-->

Tuesday, December 15, 2015

Exception Handling, I/O, Applets



1.       Write a Java Program which takes two file names as command line arguments. The program should copy the contents of first file into the second file. The program should handle FileNotFoundException and ArrayIndexOutofBoundsException. Also provide a generic catch statement which can handle all the types of exceptions. Provide a finally block too.

import java.io.*;
public class Main {
   public static void main(String[] args)
   throws Exception {
      BufferedWriter out1 = new BufferedWriter
      (new FileWriter("srcfile"));
      out1.write("CODEED BY CLINTON \n");
      out1.close();
      InputStream in = new FileInputStream
      (new File("srcfile"));
      OutputStream out = new FileOutputStream
      (new File("destnfile"));
      byte[] buf = new byte[1024];
      int len;
      while ((len = in.read(buf)) > 0) {
         out.write(buf, 0, len);
      }
      in.close();
      out.close();
      BufferedReader in1 = new BufferedReader
      (new FileReader("destnfile"));
      String str;
      while ((str = in1.readLine()) != null) {
         System.out.println(str);
      }
      in.close();
   }
}




2.       Create an applet for the following cases and run it using a Appletviewer and Browser.
a.       Simple applet to display some string.
import java.awt.*;
import java.applet.*;
public class Appl extends Applet {
public void init()
{
}
public void paint(Graphics g) {
g.drawString("A Simple Applet", 30, 30);
}
}
Html display code
<applet code="Appl" width=300 height=70>
</applet>
Output

b.      An applet to draw some shapes.
import java.applet.*;
import java.awt.*;
public class  Rectangle extends Applet{
   public void paint(Graphics g){
      g.drawRect(150,50,200,150);
      g.setColor(Color.BLUE);  
      g.fillRect( 200,100, 300, 150 );
      g.setColor(Color.RED);
      g.drawString("Rectangle",400,200);
   }
}
HTML Code
<applet code="Rect" width=200 height=60>
</applet>
Output

c.       An Applet the display some AWT controls.
Code
import java.applet.Applet;
import java.awt.Button;
public class AWT extends Applet
{
            public void init()
            {
              Button button1 = new Button();
              button1.setLabel("Button A");
              Button button2 = new Button("Button B");
              add(button1);
              add(button2);
        }
}
// <applet code="AWT" width=200 height=60>
</applet>//
Output



JavaScript Free Code