Java Pratical File

Embed Size (px)

DESCRIPTION

Java Pratical File

Citation preview

  • CONTENTS PgNo.

    1. Write a program that implements the Concept of Encapsulation. 22. Write a Program to demonstrate concept of Polymorphism(overriding & overloading).

    3

    3. WAP the use boolean data type and print the Prime number Series upto 50. 44. Write a Program for matrix multiplication and transpose of matrix using IO Stream.

    5

    5. WAP to add the elements of Vector as arguments of main method (Run time) and rearrange them, and copy it into an Array.

    8

    6. WAP to check that the given String is palindrome or not. 97. WAP to to arrange the string in alphabetical order. 108. WAP for StringBuffer class which perform the all methods of that class. 129. WAP to calculate Simple Interest using the Wrapper Class. 1410. WAP to calculate Area of various geometrical figures using the abstract class.

    16

    11. WAP where Single class implements more than one interfaces and with the help of interface reference variable user call the methods.

    17

    12. WAP that use the multiple catch statements within the try-catch mechanism.

    18

    13.WAP where user will create a self-Exception using the "throw" keyword. 1914. WAP for multithread using the Alive(),Join() & Synchronized() methods of Thread class.

    20

    15. WAP to create a package using command & one package will import the another package.

    23

    16. WAP for AWT to create Menu and Popup Menu for Frame. 2417. WAP in java for Applet that handle the KeyBoard Event. 2518. WAP which support the TCP/IP protocol, where client gives the message and server will be, receives the message.

    26

    19. WAP to illustrate the use of all methods of URL class. 2820. WAP for JDBC to insert the values into the existing table by using prepared Statement.

    29

    21. WAP for JDBC to display the records from the existing table. 3022. WAP to demonstrate the Border Layout using applet. 3124. WAP for Applet who generate the MouseMotionListner Event. 3225. WAP for display the Checkboxes,Labels and TextFields on an AWT. 3427. WAP to create a file and to store data into the file(using FileWriterIOStream).

    36

    28. WAP to display your file in DOS console use the IO stream.29. WAP to create an Applet using HTML file, where Parameter passes for font size & font type and Applet message will change to corresponding parameters.

  • Q1. Write a program that implements the Concept of Encapsulation.

    class Emp{private int empid;private String name;public Emp( int x,String y){empid=x;name=y;}public void show(){System.out.println("empid="+empid);System.out.println("name="+name);}}class Encap{public static void main(String arg[]){int eid=Integer.parseInt(arg[0]);String ename=arg[1];Emp e1=new Emp(eid,ename);e1.show();}}Output:-

  • Q2. WAP to demonstrate concept of Polymorphism (Overloading).

    class Overloaddemo{ void test() {

    System.out.println("No Parameters"); }//Overload test for one integer parameter void test(int a) {

    System.out.println("a:"+a); } //Overload test for two integer parameter void test(int a, int b) {

    System.out.println("a & b:"+a +" "+b); } //overload test for double parameter double test(double a) {

    System.out.println("Double a:"+a);return a*a;

    } } class Overload {

    public static void main(String args[]) { Overloaddemo ob= new Overloaddemo(); double result;

    //call all version of test()ob.test();ob.test(10);ob.test(10,20);result=ob.test(123.25);System.out.println("Result of ob.test(123.25):"+result);

    }}

  • Output:-

  • Q2(a) Write a Program to demonstrate concept of Polymorphism (Overriden).

    class Funcover{public void calc(int x,int y){int z;z=x*y;System.out.println("multiplication="+z);}}class Override extends Funcover{public void calc(int x,int y){int z;z=x/y;System.out.print("division="+z);}}class Overrideex{public static void main(String arg[]){Funcover f1=new Funcover();f1.calc(7,6);Override f2=new Override();f2.calc(14,2);}}

    Output:-

  • Q3 Write a Program the use boolean data type and print the Prime number Series upto 50.

    class Primetest

    {

    public static void main(String args[])

    {

    int num,i;

    boolean prime=true;

    for(num=3;num

  • }}}

    Output:-

  • Q4 Write a Program for Matrix Multiplication and Transpose using Input/Output Stream.

    import java.io.*;

    class Mat

    {

    public static void main(String args[]) throws Exception

    {

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    System.out.println("1. Transpose of Matrix");

    System.out.println("2. Matrix multiplication");

    System.out.println("Any other choice for exit the prg");

    System.out.print("\n\nEnter Your choice");

    int ch=Integer.parseInt(br.readLine());

    switch(ch)

    {

  • case 1:

    System.out.println("Enter the order of matrix");

    int n,m;

    n=Integer.parseInt(br.readLine());

    m=Integer.parseInt(br.readLine());

    int a[][]=new int[10][10];

    System.out.print("enter the "+m*n+" elements of matrix");

    for(int i=0;i

  • System.out.print(a[j][i]+" ");

    }

    System.out.println();

    }

    break;

    case 2:

    int i,j,s;

    int m1,n1,p,q;

    int a1[][]=new int[10][10];

    int b[][]=new int[10][10];

    int c[][]=new int[10][10];

    System.out.print("Enter no. of Row of First Matrix->");

    m1=Integer.parseInt(br.readLine());

    System.out.print("Enter no. of Column of First Matrix->");

    n1=Integer.parseInt(br.readLine());

    System.out.print("Enter no. of Row of Second Matrix->");

    p=Integer.parseInt(br.readLine());

    System.out.print("Enter no. of Column of Second Matrix->");

    q=Integer.parseInt(br.readLine());

  • if(n1!=p)

    {

    System.out.println("\n\nMatrix Multyply Fail!!---Matrix should be Identical");

    break;

    }

    System.out.println("Enter First Matrix");

    for(i=0;i

  • System.out.print("Enter Element at " + (i+1) + " Row and " + (j+1) + " Column ->");

    b[i][j]=Integer.parseInt(br.readLine());

    }

    }

    for(i=0;i

  • break;

    default:

    }

    }

    public static void show(int n,int m,int a[][])

    {

    for(int i=0;i

  • OUTPUT:-

    Q5. Write a Program for Vector class when input elements as argument and copy the element into an array.

    import java.util.*;

    import java.io.*;

  • class Vectordemo

    {

    public static void main(String args[])throws Exception

    {

    Vector v = new Vector();

    int n=Integer.parseInt(args[0]);

    for(int i=1;i

  • System.out.println(array[i]);

    }

    }

    }

    Output:-

    Program-6

    wap a program to check given string is palindrom or not

    import java.io.*;

  • class Palindromedemo

    {

    public static void main(String []args)throws Exception

    {

    int i,j;

    InputStreamReader isr=new InputStreamReader(System.in);

    BufferedReader br=new BufferedReader(isr);

    System.out.println("Enter the string to check");

    String str=br.readLine();

    for(i=0,j=str.length();i

  • output:

    Q7 WAP in java to arrange the string in alphabetical order.

    class Stringordering

    {

    static String name[]={"Ram","Pooja","Monu","Kiran"};

    public static void main(String arg[])

    {

    int size=name.length;

    String temp=null;

    for(int i=0;i

  • {if(name[i].compareTo(name[j])>0)

    {

    //swap the string

    temp=name[i];

    name[i]=name[j];

    name[j]=temp;

    }

    }

    }

    for(int i=0;i

  • PROGRAM-8

    Write a program for string Buffer class which performs the all method of that class.

    import java.io.*;

    class Bufferstring

    {

    public static void main(String args[])throws Exception

    {

    InputStreamReader isr=new InputStreamReader(System.in);

    BufferedReader br=new BufferedReader(isr);

  • System.out.print("\n Enter the String :");

    StringBuffer str=new StringBuffer(br.readLine());

    System.out.println("Enter the second string :");

    StringBuffer st=new StringBuffer(br.readLine());

    System.out.println("\n String Entered 1 : "+str);

    System.out.println("\n String Entered 2 : "+st);

    System.out.println("length of string1 : "+str.length());

    System.out.println("Capacity of string1 : "+str.capacity());

    System.out.println("Enter the String to Insert :");

    String str1=br.readLine();

    System.out.println("Enter the String to Insert :");

    //String str2=br.readLine();

    System.out.println("\n After Insertion :"+str.insert(2,str1));

    System.out.println("charAt(1) "+str.charAt(1));

    System.out.println("Append : "+str.append(st));

    }

    }

    OUTPUT:-

  • Q9. Write a program to calculate simple interest using the wrapper class

    class Wrap

    {

    public static void main(String args[])

    {

    float p=Float.parseFloat(args[0]);

    System.out.println("Principle amount is: "+p);

    float r= Float.parseFloat(args[1]);

    System.out.println("rate is: "+r);

    int t=Integer.parseInt(args[2]);

    System.out.println("Enter Time: "+t);

    float SI=(p*r*t)/100;

    System.out.println("Simple Interest is: "+SI);

    }

    }

    Output

  • Q.10 WAP to calculate Area of various geometrical figures using the abstract class.

    CODE:-

    import java.io.*;

    abstract class Area

    {

    final static float pi=3.14F;

    abstract float compute(float x, float y);

    }

    class Rectangle extends Area

    {

    float compute(float x,float y)

    {

    return(x*y);

    }

    }

    class Triangle extends Area

    {

    float compute(float x,float y)

    {

    return(x*y/2);

    }

    }

    class Geomatric

  • { public static void main(String args[])throws Exception

    {

    float r,s;

    Rectangle rect=new Rectangle();

    Triangle tri = new Triangle();

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("1. Area of Rectangle ");

    System.out.println("2. Area of Triangle\n ");

    System.out.print("Enter your choice : ");

    int ch=Integer.parseInt(br.readLine());

    switch(ch)

    {

    case 1:

    {

    System.out.println("Enter the side of Rectangle");

    r=Float.parseFloat(br.readLine());

    s=Float.parseFloat(br.readLine());

    System.out.println("Area is = "+rect.compute(r,s));

    break;

    }

    case 2:

    {

    System.out.println("Enter the base and height of traingle");

    r=Float.parseFloat(br.readLine());

  • s=Float.parseFloat(br.readLine());

    System.out.println("Area is = "+tri.compute(r,s));

    break;

    }

    }

    }

    }

    OUTPUT:

  • Q11. Write a program where single class implements more than one interface and with help of interface reference variable user call the method.

    CODE:-

    import java.io.*;

    interface First

    {

    void addition(int a,int b);

    }

    interface Second

    {

    void multiplication(int a, int b );

    }

    class IDemo implements First,Second

    {

    public void addition(int a,int b)

    {

    System.out.println("Addition of two number is: "+(a+b) );

    }

    public void multiplication(int c,int d )

    {

    System.out.println("Addition of two number is: "+(c*d) );

    }

  • }class ID

    {

    public static void main(String args[]) throws Exception

    {

    IDemo ii=new IDemo();

    First f;

    Second s;

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter Numbers for Addition and Multiplication");

    System.out.println("Enter First number:");

    int a=Integer.parseInt(br.readLine());

    System.out.println("Enter Second number:");

    int b=Integer.parseInt(br.readLine());

    f=ii;

    f.addition(a,b);

    s=ii;

    s.multiplication(a,b);

    }

    }

    OUTPUT:-

  • Program-12

    Write a program that uses multiple catch statement with try-catch mechanism.

    import java.io.*;

    class MultiDemo

    {

    public static void main(String args[])

    {

    int a,b;

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    try

    {

    System.out.println("****Arithmetic Operation****");

    System.out.println("Enter First number:" );

    a=Integer.parseInt(br.readLine());

    System.out.println("Enter Second number:" );

    b=Integer.parseInt(br.readLine());

    System.out.println("Sum of the number is:"+(a+b));

    System.out.println("Subtraction of the number is:"+(a-b));

    System.out.println("Multiplication of the number is:"+(a*b));

    System.out.println("Division of the number is:"+(a/b));

  • }catch(IOException ioe)

    {

    System.out.println(ioe);

    }

    catch(ArithmeticException ae)

    {

    System.out.println(ae);

    }

    catch(NumberFormatException ne)

    {

    System.out.println(ne);

    }

    }

    }

    Output:

  • Q13 Write a Program where user will create a self-Exception using the "throw" keyword.

    import java.lang.Exception;class SelfException extends Exception{SelfException(String message){super(message);}}class TestSelfException{public static void main(String args[]){int x=5, y=1000;try{float z=(float)x /(float) y;if(z

  • Q14. Write a program for multithread using the isAlive(), join(), and synchronized method of thread class.

    CODE:-

    class CallMe

    {

    synchronized void call(String msg)

    {

    System.out.print("["+msg);

    try

    {

    Thread.sleep(100);

    }

    catch(InterruptedException e)

    {

    System.out.println("Interrupted");

    }

    System.out.println("]");

    }

  • }class Caller implements Runnable

    {

    String msg;

    CallMe target;

    Thread t;

    public Caller(CallMe targ,String s)

    {

    target=targ;

    msg=s;

    t=new Thread(this);

    t.start();

    }

    public void run()

    {

    target.call(msg);

    }

    }

    class SynchronizeTest

    {

    public static void main(String args[])

    {

    CallMe target=new CallMe();

    Caller ob1=new Caller(target,"Hi");

    Caller ob2=new Caller(target,"I Am");

    Caller ob3=new Caller(target,"Synchronized");

    System.out.println("Thread one is alive:"+ob1.t.isAlive());

    System.out.println("Thread Two is alive:"+ob2.t.isAlive());

  • System.out.println("Thread Three is alive:"+ob3.t.isAlive());

    try

    {

    ob1.t.join();

    ob2.t.join();

    ob3.t.join();

    }

    catch(InterruptedException ie)

    {

    System.out.println("Interrupted");

    }

    System.out.println("Thread one is alive:"+ob1.t.isAlive());

    System.out.println("Thread Two is alive:"+ob2.t.isAlive());

    System.out.println("Thread Three is alive:"+ob3.t.isAlive());

    }

    }

    OUTPUT:-

  • Q15 WAP to create a package using command & one package will import the another package.

    import package1.ClassA; import package2.*; Class PackageTest2 { Public static void main(String args[] ) {

    ClassA objectA = new ClassA(); ClassB objectB= new ClassB();

    objectA.display();

  • objectB.display(); } }

    Output:-

    Class AClass B

    m = 10

    Program-16

    Write a program for AWT to create menu and popup menu for Frame.

    Code:

    import java.awt.*;

    import java.awt.event.*;

    class MenuDemo extends Frame

    {

    MenuBar mbr;

    Menu m1,m2,sm;

    MenuItem mi1,mi2,mi3,mi4,mi5,mi6,mi7,mi8,mi9,mi10,pmi6,pmi7,pmi8,pmi9,pmi10;

  • PopupMenu p;

    MenuShortcut msc1;

    MenuDemo()

    {

    setTitle("Menu");

    mbr=new MenuBar();

    setMenuBar(mbr);

    m1=new Menu("File");

    msc1=new MenuShortcut('F');

    mi1=new MenuItem("New",msc1);

    mi2=new MenuItem("Open");

    mi3=new MenuItem("Save");

    mi4=new MenuItem("SaveAs");

    mi5=new MenuItem("Exit");

    m1.add(mi1);

    m1.add(mi2);

    m1.addSeparator();

    m1.add(mi3);

    m1.add(mi4);

    m1.add(mi5);

    mbr.add(m1);

    m2=new Menu("Edit");

    mi6=new MenuItem("Cut");

    mi7=new MenuItem("Copy");

  • mi8=new MenuItem("paste");

    m2.add(mi6);

    m2.add(mi7);

    m2.add(mi8);

    sm=new Menu("Tools");

    m2.add(sm);

    mi9=new MenuItem("Undo");

    mi10=new MenuItem("Redo");

    sm.add(mi9);

    sm.add(mi10);

    mbr.add(m2);

    p=new PopupMenu("Floating Menu");

    pmi6=new MenuItem("Cut");

    pmi7=new MenuItem("Copy");

    pmi8=new MenuItem("paste");

    pmi9=new MenuItem("Undo");

    pmi10=new MenuItem("Redo");

    p.add(pmi6);

    p.add(pmi7);

    p.add(pmi8);

    p.add(pmi9);

    p.add(pmi10);

    add(p);

    addMouseListener(new MouseAdapter()

    {

  • public void mousePressed(MouseEvent me)

    {

    display(me);

    }

    public void mouseReleased(MouseEvent me)

    {

    display(me);

    }

    public void mouseClicked(MouseEvent me)

    {

    display(me);

    }

    });

    setVisible(true);

    validate();

    setLocation(100,10);

    setSize(200,200);

    }

    public void display(MouseEvent me)

    {

    if(me.isPopupTrigger())

    p.show(this,me.getX(),me.getY());

    }

    public static void main(String agrs[])

    {

  • MenuDemo fr=new MenuDemo();

    }

    }

    Output:

    Z:\jaya>javac menuDemo.java

    Z:\jaya>java menuDemo

  • Q17 WAP in java for Applet that handle the KeyBoard Event.

    import java.awt.*;

    import java.awt.event.*;

    import java.applet.*;

    /*

    */

    public class prg17 extends Applet implements KeyListener

    {

    String msg=" ";

    int x=30,y=40;

    public void init()

    {

    addKeyListener(this);

    requestFocus();

    }

    public void KeyPressed(KeyEvent ke)

    {

    showStatus("Key Pressed");

    }

    public void KeyRealsed(KeyEvent ke)

    {

  • showStatus("Key UP");

    }

    public void KeyTyped(KeyEvent ke)

    {

    msg+=ke.getKeyChar();

    repaint();

    }

    public void paint(Graphics g)

    {

    g.drawString(msg,x,y);

    }

    }

    Output:-

  • PROGRAM-18

    Write a program which support the TCP/IP protocol, where client gives the message and server will be, receives the message.

    Code:-

    // clientdemo.java

    import java.net.*;

    import java.io.*;

    class Clientdemo

    {

    public static void main(String args[]) throws Exception

    {

    InetAddress add=InetAddress.getByName("disha-8");

    System.out.println("Cnnecting clint is="+add);

    Socket s=new Socket(add,9000);

    OutputStream os=s.getOutputStream();

  • PrintWriter pw=new PrintWriter(os);

    InputStreamReader isr=new InputStreamReader(System.in);

    BufferedReader br=new BufferedReader(isr);

    String str=br.readLine();

    while(str.length()!=0)

    {

    pw.println(str);

    str=br.readLine();

    }

    pw.close();

    br.close();

    isr.close();

    s.close();

    os.close();

    }

    }

  • //serverdemo.java

    import java.net.*;

    import java.io.*;

    class Serverdemo

    {

    public static void main(String args[]) throws Exception

    {

    InetAddress seradd=InetAddress.getLocalHost();

    System.out.println("server address is="+seradd);

    System.out.println("server is up and waiting for the client");

    ServerSocket ss=new ServerSocket(9000);

  • Socket s1=ss.accept();

    System.out.println("Connection is established===");

    try{

    InputStream is=s1.getInputStream();

    InputStreamReader isr1=new InputStreamReader(is);

    BufferedReader br1=new BufferedReader(isr1);

    String str1=br1.readLine();

    while(str1.length()!=0)

    {

    System.out.println(str1);

    str1=br1.readLine();

    }

    br1.close();

    isr1.close();

  • s1.close();

    }

    catch(Exception e){}

    }

    }

    Output:-

    Z:\vinu\corejava\net>javac serverdemo.java

    Z:\vinu\corejava\net>java serverdemo

    server address is=Disha-7/10.0.0.7

    server is up and waiting for the client

    Connection is established===

    hai hello.

    Z:\vinu\corejava\net>

  • Z:\vinu\corejava\net>javac clientdemo.java

    Z:\vinu\corejava\net>java clientdemo

    Cnnecting clint is=Disha-7/10.0.0.7

    hai hello.

    Z:\vinu\corejava\net>

  • Q19. Write a Program to illustrate the use of all the method of URL class.

    CODE:-

    //URL Class

    // ParseURL.java

    import java.net.*;

    import java.io.*;

    public class ParseURL

    {

    public static void main(String[] args) throws Exception

    {

    URL aURL = new URL("http://java.sun.com:80/docs/books/"

    + "tutorial/index.html#DOWNLOADING");

    System.out.println("protocol = " + aURL.getProtocol());

    System.out.println("host = " + aURL.getHost());

    System.out.println("filename = " + aURL.getFile());

    System.out.println("port = " + aURL.getPort());

    System.out.println("ref = " + aURL.getRef());

    }

    }

    OUTPUT:-

  • E:\Java Prac>javac ParseURL.java

    E:\Java Prac>java ParseURL

    protocol = http

    host = java.sun.com

    filename = /docs/books/tutorial/index.html

    port = 80

    ref = DOWNLOADING

    E:\Java Prac>

  • Q20. Write a program for JDBC to insert the values into the existing table by using prepared statement.

    CODE:-

    import java.sql.*;

    import java.io.*;

    class InsertDemo

    {

    public static void main(String[] args)

    {

    try

    {

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    Connection conn=DriverManager.getConnection("jdbc:odbc:stuDSN");

    PreparedStatement pst=conn.prepareStatement("Insert into student values(?,?,?)");

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter ROll no");

    String a=br.readLine();

    System.out.println("Enter Name");

    String b=br.readLine();

    System.out.println("Enter Marks");

    String c=br.readLine();

    int x=Integer.parseInt(a);

    int y=Integer.parseInt(c);

  • pst.setInt(1,x);

    pst.setString(2,b);

    pst.setInt(3,y);

    pst.executeUpdate();

    System.out.println("One row inserted");

    conn.close();

    pst.close();

    br.close();

    }catch(ClassNotFoundException cnfe)

    {

    }catch(IOException cnfe)

    {

    } catch(SQLException e)

    {

    }

    }

    }

    OUTPUT:-

    E:\Java Prac>javac InsertDemo.java

  • E:\Java Prac>java InsertDemo

    Enter ROll no

    3

    Enter Name

    mohan

    Enter Marks

    78

    One row inserted

    E:\Java Prac>

  • Q21.Write a program for jdbc to display the records from existing table .

    CODE:-

    import java.sql.*;

    class Show

    {

    public static void main(String[] args) throws SQLException

    {

    try

    {

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    Connection conn=DriverManager.getConnection("jdbc:odbc:stuDSN");

    Statement st=conn.createStatement();

    String str="Select * from student ";

    ResultSet rs=st.executeQuery(str);

    if(rs.next())

    {

    while(rs.next())

    {

    System.out.print(rs.getInt("rollno")+"\t");

    System.out.print(rs.getString("Name")+"\t");

    System.out.print(rs.getInt("marks")+"\n");

    System.out.println("________________");

    }

  • }}catch(ClassNotFoundException cnfe)

    {

    }

    }

    }

    OUTPUT:-

    E:\Java Prac>javac Show.java

    E:\Java Prac>java Show

    2 disha 88

    ________________

    3 mohan 78

    ________________

    4 ram 66

    ________________

    Q22. Write a Program to demonstrate Border Layout.

  • CODE:-

    import java.awt.*;

    import java.awt.event.*;

    class BorderDemo extends Frame

    {

    public BorderDemo()

    {

    Label lbl1 = new Label("NORTH");

    Label lbl2=new Label("SOUTH");

    Button btn1 = new Button("EAST");

    Button btn2=new Button("WEST");

    TextArea ta = new TextArea(5,5);

    ta.append("CENTER");

    setLayout(new BorderLayout());

    add(lbl1,BorderLayout.NORTH);

    add(lbl2,BorderLayout.SOUTH);

    add(btn1,BorderLayout.EAST);

    add(btn2,BorderLayout.WEST);

    add(ta,BorderLayout.CENTER);

    addWindowListener(new WindowAdapter()

    {

    public void windowClosing(WindowEvent e)

    {

    System.exit(0);

  • }

    } );

    }

    public static void main(String args[])

    {

    BorderDemo bd = new BorderDemo();

    bd.setTitle("BorderLayout");

    bd.setSize(300,200);

    bd.setVisible(true);

    }

    }

    OUTPUT:-

    E:\Java Prac>javac BorderDemo.java

    E:\Java Prac>java BorderDemo

  • Q24. WAP for Applet who generate the MouseMotionListener event.

    class emp

    {

    private int empid;

    private String name;

    public emp( int x,String y)

    {

    empid=x;

    name=y;

    }

    public void show()

    {

    System.out.println("empid="+empid);

    System.out.println("name="+name);

    }

    }

    class encap

    {

    public static void main(String arg[])

    {

  • int eid=Integer.parseInt(arg[0]);

    String ename=arg[1];

    emp e1=new emp(eid,ename);

    e1.show();

    }

    }

    Output:-

  • Q25. Write a program for display chexkBox,labels and TextFields on an AWT.

    CODE:-

    import java.awt.*;

    import java.awt.event.*;

    public class AwtFrame

    {

    public static void main(String[] args)

    {

    Frame frame=new Frame("Frame");

    Button button = new Button("Button");

    Label label=new Label("Label");

    TextArea txtArea=new TextArea(30,100);

    txtArea.append("This is Text Area");

    TextField text=new TextField("TextBox",20);

    frame.setSize(300,200);

    frame.setLocation(100,100);

    frame.setLayout(new FlowLayout());

    frame.setVisible(true);

    frame.add(label);

    frame.add(text);

    frame.add(button);

    frame.add(txtArea);

    frame.setVisible(true);

  • frame.addWindowListener(new WindowAdapter()

    {

    public void windowClosing(WindowEvent e)

    {

    System.exit(0);

    }

    });

    }

    }

    OUTPUT:-

    E:\Java Prac>javac AwtFrame.java

    E:\Java Prac>java AwtFrame

  • Q 27 WAP for creating a file and to store data into that file (using FileWriterIOStream).

    import java.io.*;public classFileWriter{

    public static void main(String[] args) throws IOException{BufferedReader in = new BufferedReader(new

    InputStreamReader(System.in));System.out.print("Please enter the file name to create : ");String file_name = in.readLine();File file = new File(file_name);boolean exist = file.createNewFile();if (!exist){

    System.out.println("File already exists.");System.exit(0);

    }else{

    FileWriter fstream = new FileWriter(file_name);BufferedWriter out = new BufferedWriter(fstream);out.write(in.readLine());out.close();System.out.println("File created successfully.");

    }}}

    Output:-

    C:\Suyash>javac CreateFile.javaC:\Suyash>java CreateFilePlease enter the file name to create : nande.txtThis is my nameFile created successfully.

  • Q28 WAP to display your file in DOS console use the IO stream.

    import java.io.*;class display {

    public static void main(String[] args) {

    FileInputStream file1=null;int b;try{

    file1 =new FileInputStream(args[0]);while((b=file1.read())!=-1){

    System.out.print((char)b);}file1.close(); }

    catch (IOException ioe){

    System.out.println(ioe);}}}

    Output:-

    Q29 WAP to create an Applet using HTML file, where Parameter passes for Font size & font type and Applet message will change to corresponding Parameters.

  • import java.awt.*;

    import java.applet.*;

    /*

    */

    public class prg29 extends Applet

    {

    int n;

    String style;

    public void init()

    {

    style=getParameter("t1");

    String s=getParameter("t2");

    n=Integer.parseInt(s);

    Font f=new Font(style,Font.BOLD,n);

    setFont(f);

    setBackground(Color.red);

    setVisible(true);

    }

    public void paint(Graphics g)

    {

  • g.drawString("disha college",20,100);

    }

    }

    Output:-