java lab manual 1 it

Upload: bhadri-nath

Post on 06-Apr-2018

241 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Java Lab Manual 1 It

    1/57

    HOLY MARY INSTITUTE OF TECHNOLOGY &

    SCIENCE

    (COLLEGE OF ENGINEERING)

    Bogaram(v),Keesara(M),R.R.Dist 501 301

    Department Of Computer Science & Engineering

    Subject : Dbms Lab Manual

    Branch : B.Tech IT

    Class : IIrd Year

    Semester : IInd

    A.Y. : 2009 2010

    Created By M.Monika(Asst.Prof,HITS)

  • 8/3/2019 Java Lab Manual 1 It

    2/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Introduction to Java:

    Java is great programming language for the development of enterprise gradeapplications. This programming Language is evolved from a language named Oak.Oak was developed in the early nineties at Sun Microsystems as a platform-independent language aimed at allowing entertainment appliances such as videogame consoles and VCRs to communicate . Oak was first slated to appear intelevision set-top boxes designed to provide video-on-demand services. Oak wasunsuccessful so in 1995 Sun changed the name to Java and modified the languageto take advantage of the burgeoning World Wide Web.Java is an object-orientedlanguage, and this is very similar to C++. Java Programming Language issimplified to eliminate language features that cause common programming errors.Java source code files are compiled into a format called bytecode, which can then

    be executed by a Java interpreter. What is JDK (Java Development Kit)

    JDK is a software development program provided by sun Microsystems. JavaDevelopment Kit or JDK comes in various version and can be downloaded freefrom the sun Microsystems. JVM compiler, debugger and other tools are used withJDK for developing java based application & java applets. So make sure that yourJVM compiler & JDK versions are same. JDK also known as Java 2 Platform, Thatcomes in three editions J2ME, J2SE & J2EE. If you are beginner or learning Javathen start by downloading J2SE.Acronyms:JDK Java Development KitJVM Java virtual machineDownload JDKYou can download JDK from www.javasoft.com/j2seLatest version of JDK

    jdk1.6.0_15

    http://java.sun.com/j2se/Downloads/index.jsp

    Holymary Institute of Technology & Sciences 2

    http://java.sun.com/j2se/Downloads/index.jsphttp://java.sun.com/j2se/Downloads/index.jsp
  • 8/3/2019 Java Lab Manual 1 It

    3/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Program Strucuture of java in Java Language:

    The fundamental structure of any Java programme should look like:

    [package declarations]

    [import statements]

    [class declaration]

    An example is given below:

    package abc;

    import java.lang;

    class Demo {

    public static void main(String[] args) {

    Sytem.out.println("Hello! World");

    }

    }

    //The file containing this class must be named Demo.java

    How to set the path for Java

    1.Go to MyComputer properties->Advanced ->Environment Variables.

    2.Find the path in SystemVariables and Edit it to your Jdk/bin folder.

    3.Now your path is set and you can run the programs from anywhere.

    Holymary Institute of Technology & Sciences 3

  • 8/3/2019 Java Lab Manual 1 It

    4/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week1 :

    a) Write a Java program that prints all real solutions to the quadratic equation ax2+ bx + c = 0. Read in a, b, c and use the quadratic formula. If the discriminant b2-4ac is negative, display a message stating that there are no real solutions.

    public class Quadratic{public static void main(String args[]){

    double a,b,c,root1,root2;a=3.0;

    b=4.0;

    c=2.0;if((b*b-4*a*c)==0){

    System.out.println("\n ROOTS ARE EQUAL");root1=-b/(2*a);System.out.println("\n root "+root1);

    }if((b*b-4*a*c) > 0){

    root1=-b+Math.sqrt(b*b-4*a*c)/(2*a);root2=-b-Math.sqrt(b*b-4*a*c)/(2*a);System.out.println("\nROOTS ARE REAL:\n");System.out.println("\n root1 "+root1+"\n root2 "+root2);

    } elseSystem.out.println("\n Imaginary Roots.");

    }} // End of Class

    Output

    ROOTS ARE REAL:

    root1 -5.422649730810374root2 -6.577350269189626

    Holymary Institute of Technology & Sciences 4

  • 8/3/2019 Java Lab Manual 1 It

    5/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 1:

    b) The Fibonacci sequence is defined by the following rule: The fist two values inthe sequence are 1 and 1. Every subsequent value is the sum of the two valuespreceding it. Write a Java program that uses both recursive and non recursivefunctions to print the nth value in the Fibonacci sequence.

    public class LabPro2{

    public static void main(String args[]){

    String n1;int f1=1,f2=1,n,i,f3;n1=5;n=Integer.parseInt(n1);System.out.println("series"+f1+f2);for(i=1;i

  • 8/3/2019 Java Lab Manual 1 It

    6/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 2 :

    a) Write a Java program that prompts the user for an integer and then prints outall prime numbers up to that integer.

    import javax.swing.*;public class LabPro3{

    public static void main(String args[]){

    String n1;int n,c=0,i,j;

    n1=JOptionPane.showInputDialog("enter n value");n=Integer.parseInt(n1);outer:

    for(i=2;i

  • 8/3/2019 Java Lab Manual 1 It

    7/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Holymary Institute of Technology & Sciences 7

  • 8/3/2019 Java Lab Manual 1 It

    8/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 2 :

    Holymary Institute of Technology & Sciences 8

  • 8/3/2019 Java Lab Manual 1 It

    9/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    b) Write a Java program to multiply two given matrices.

    import javax.swing.*;public class BTechJavaLab6{

    public static void main(String args[]){String n,l,m=" ";int i,j,k;int a[][]=new int[3][3];int b[][]=new int[3][3];int c[][]=new int[3][3];for(i=0;i

  • 8/3/2019 Java Lab Manual 1 It

    10/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    }

    }

    Output:

    Holymary Institute of Technology & Sciences 10

  • 8/3/2019 Java Lab Manual 1 It

    11/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Holymary Institute of Technology & Sciences 11

  • 8/3/2019 Java Lab Manual 1 It

    12/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Holymary Institute of Technology & Sciences 12

  • 8/3/2019 Java Lab Manual 1 It

    13/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Holymary Institute of Technology & Sciences 13

  • 8/3/2019 Java Lab Manual 1 It

    14/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 2 :

    Holymary Institute of Technology & Sciences 14

  • 8/3/2019 Java Lab Manual 1 It

    15/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    c) Write a Java Program that reads a line of integers, and then displays eachinteger, and the sum of all the integers (Use StringTokenizer class of java.util)

    import javax.swing.*;import java.util.*;import java.io.*;public class LabPro7{public static void main(String args[])throws IOException{

    int sum=0;String line=JOptionPane.showInputDialog("enter the line");String k=JOptionPane.showInputDialog("enter the delemeter");StringTokenizer wd=new StringTokenizer(line,k);while(wd.hasMoreTokens())sum=sum+Integer.parseInt(wd.nextToken());JOptionPane.showMessageDialog(null,"sum is"+sum);

    }}

    Option:

    Holymary Institute of Technology & Sciences 15

  • 8/3/2019 Java Lab Manual 1 It

    16/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Holymary Institute of Technology & Sciences 16

  • 8/3/2019 Java Lab Manual 1 It

    17/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 3 :

    a) Write a Java program that checks whether a given string is a palindrome or not.Ex: MADAM is a palindrome.

    import java.util.*;public class LabPro4{

    public static void main(String args[]){

    StringBuffer sb=new StringBuffer(args[0]);String t=sb.reverse().toString();System.out.println("reverse "+t);System.out.println("boolean results");System.out.println("check with == "+(t==args[0]));System.out.println("check with equals "+(t.equals(args[0])));System.out.println("given string "+args[0]);if(t.equals(args[0]))System.out.println("is a pallindrome");elseSystem.out.println("is not a pallindrome");

    }}

    Output:

    reverse MADAM

    boolean results

    check with == false

    check with equals true

    given string MADAM

    is a palindrome

    Holymary Institute of Technology & Sciences 17

  • 8/3/2019 Java Lab Manual 1 It

    18/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 3 :

    b) Write a Java program for sorting a given list of names in ascending order.

    import java.io.*;

    import javax.swing.JOptionPane;

    class Ascend

    {

    public static void main(String[] args)

    {

    String name[]={"","",""},temp;

    for(int i=0;i

  • 8/3/2019 Java Lab Manual 1 It

    19/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    }

    for(int i=0;i

  • 8/3/2019 Java Lab Manual 1 It

    20/57

  • 8/3/2019 Java Lab Manual 1 It

    21/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 3 :

    c) Write a Java program to make frequency count of words in a given text.

    import java.io.*;

    import javax.swing.JOptionPane;

    import java.util.*;

    class Count

    {

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

    {

    String s;

    int i=0,count=0,l=0;

    s=JOptionPane.showInputDialog("enter a file name");

    FileInputStream f=new FileInputStream(s);

    DataInputStream d=new DataInputStream(f);

    while(d.readLine()!=null)

    {

    i++;

    StringTokenizer s1=new StringTokenizer(d.readLine());

    while(s1.hasMoreTokens())

    {

    String b=s1.nextToken();

    count++;

    l=l+b.length();

    }

    Holymary Institute of Technology & Sciences 21

  • 8/3/2019 Java Lab Manual 1 It

    22/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    }

    JOptionPane.showMessageDialog(null,"words="+count+"\nlines="+i+"\n

    characters="+l,"Message",JOptionPane.INFORMATION_MESSAGE);

    }

    }

    Output:

    Holymary Institute of Technology & Sciences 22

  • 8/3/2019 Java Lab Manual 1 It

    23/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Holymary Institute of Technology & Sciences 23

  • 8/3/2019 Java Lab Manual 1 It

    24/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 4 :

    a) Write a Java program that reads a file name from the user, then displaysinformation about whether the file exists, whether the file is readable, whetherthe file is writable, the type of file and the length of the file in bytes.

    import java.io.*;import java.util.*;public class LabPro8{

    public static void main(String args[]){

    File f=new File(args[0]);System.out.println("Filename " +f.getName());System.out.println("Filepath " +f.getPath());System.out.println("Parent " +f.getName());System.out.println("File size " +f.length()+" bytes");System.out.println("is readable " +f.canRead());

    System.out.println("is Writable " +f.canWrite());System.out.println("is directory " +f.isDirectory());System.out.println("is file " +f.isFile());

    }}

    Output:

    Arguments given: C:\holy.txt

    Filename holy.txt

    Filepath C:\holy.txt

    Parent holy.txt

    File size 18 bytes

    is readable true

    is Writable true

    is directory false

    is file true

    Holymary Institute of Technology & Sciences 24

  • 8/3/2019 Java Lab Manual 1 It

    25/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 4 :

    b) Write a Java program that reads a file and displays the file on the screen, with aline number before each line.

    import java.io.*;import java.util.*;public class LabPro9{public static void main(String args[]){try{

    FileInputStream f=new FileInputStream("C:\\run.txt");LineNumberReader lr=new LineNumberReader(new

    InputStreamReader(f));String data;while((data=lr.readLine())!=null){System.out.println(lr.getLineNumber()+":"+data);}System.out.println("total lines"+lr.getLineNumber());f.close();

    }catch(Exception e){System.out.println("err"+e);}}}

    Output:

    run:

    1:Hello, Welcome to Holy Mary groups

    2:hello,java world

    total lines2

    Holymary Institute of Technology & Sciences 25

  • 8/3/2019 Java Lab Manual 1 It

    26/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 4 :

    c) Write a Java program that displays the number of characters, lines and words ina text file.

    import java.io.*;import java.util.*;public class LabPro10{

    public static void main(String args[]){try{

    FileInputStream f=new FileInputStream("C:\\run.txt");LineNumberReader lr=new LineNumberReader(new

    InputStreamReader(f));String data;StringTokenizer st;int words=0,chars=0;while((data=lr.readLine())!=null){st=new StringTokenizer(data);words+=st.countTokens();

    chars+=data.length();}System.out.println("total words"+words);System.out.println("total chars"+chars);System.out.println("total lines"+lr.getLineNumber());f.close();

    }catch(Exception e){System.out.println("err"+e);}}}

    Output:

    run:

    total words8

    total chars50

    total lines2

    Holymary Institute of Technology & Sciences 26

  • 8/3/2019 Java Lab Manual 1 It

    27/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 5 :

    a) Write a Java program that Implements stack ADT.

    import java.util.*;import javax.swing.JOptionPane;class Stackdemo{

    public static void main(String[] args){

    Stack st=new Stack();int j;do{

    String s=JOptionPane.showInputDialog("enter ur choice\n1->input\n2->delete\n3->exit");

    j=Integer.parseInt(s);if(j==1){

    s=JOptionPane.showInputDialog("enter anelement");

    st.push(Integer.parseInt(s));JOptionPane.showMessageDialog(null,"stack

    elements:"+st,"stack elements",JOptionPane.INFORMATION_MESSAGE);

    }try{

    if(j==2){

    Integer a=(Integer)st.pop();

    JOptionPane.showMessageDialog(null,"stack elements:"+st,"stackelements",JOptionPane.INFORMATION_MESSAGE);

    }}catch(EmptyStackException e){

    JOptionPane.showMessageDialog(null,"STACK ISEMPTY"+e,"stack elements",JOptionPane.INFORMATION_MESSAGE);

    }}while(j

  • 8/3/2019 Java Lab Manual 1 It

    28/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Output:

    Holymary Institute of Technology & Sciences 28

  • 8/3/2019 Java Lab Manual 1 It

    29/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Holymary Institute of Technology & Sciences 29

  • 8/3/2019 Java Lab Manual 1 It

    30/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 5 :

    b) Converts infix expression into Postfix form iii) Evaluates the postfix expression

    import java.io.*;import java.util.*;import javax.swing.JOptionPane;class Intopost{

    public static void main(String[] args){

    String s=JOptionPane.showInputDialog("enter infix expression");Stack st=new Stack();String output="";int i=0,len=s.length();char x;st.push('@');while(len!=0){

    char c=s.charAt(i);if(c=='('){

    st.push(c);}else if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^'||c=='$')

    {check(st,c,output);st.push(c);

    }else if(c==')'){

    while((x=(Character)st.pop())!='('){

    output=output+x;}

    }else{

    output+=s.charAt(i);}

    i=i+1;len--;

    }while((x=(Character)st.pop())!='@'){

    output+=x;}

    Holymary Institute of Technology & Sciences 30

  • 8/3/2019 Java Lab Manual 1 It

    31/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    JOptionPane.showMessageDialog(null,output,"postfix

    expression",JOptionPane.INFORMATION_MESSAGE);}

    static void check(Stack st,char c,String output){

    while(priority(c)

  • 8/3/2019 Java Lab Manual 1 It

    32/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 6 :

    a) Develop an applet that displays a simple message.

    import java.awt.*;import java.applet.Applet;/*< Applet code=LabPro12 width=300 height=300>< /Applet>*/

    public class LabPro12 extends Applet{public void paint(Graphics g)

    {g.drawString ("Hello Worlds",100,100);

    }}

    Output:

    Holymary Institute of Technology & Sciences 32

  • 8/3/2019 Java Lab Manual 1 It

    33/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 6 :

    b) Develop an applet that receives an integer in one text field, and computes itsfactorial Value and returns it in another text field, when the button namedCompute is clicked.

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

    public class factorial extends Appletimplements ActionListener

    {

    Button btn,clearbtn;Label lbl1,lbl2;TextField tf1,tf2;

    public void init(){

    btn=new Button("COMPUTE");btn.addActionListener(this);

    clearbtn=new Button("CLEAR");clearbtn.addActionListener(this);

    tf1=new TextField(30);tf2=new TextField(30);

    lbl1=new Label("NUMBER");

    lbl2=new Label("RESULT");

    setLayout(new GridLayout(3,2));add(lbl1); add(tf1);

    add(lbl2); add(tf2);

    add(btn); add(clearbtn);}

    public void actionPerformed(ActionEvent e){if(e.getSource()==btn){int a=Integer.parseInt(tf1.getText());

    Holymary Institute of Technology & Sciences 33

  • 8/3/2019 Java Lab Manual 1 It

    34/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    int fact=1;

    for(int i=1;i

  • 8/3/2019 Java Lab Manual 1 It

    35/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 7 :

    Write a Java program that works as a simple calculator.Use a grid layout to arrange buttons for the digits andfor the +, -,*, % operations. Add a text field to displaythe result.

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

    public class LabPro14 extends Applet implements ActionListener{

    TextField t;String a;int p=0,tmp=0;Button bo,b1,b2,b3,b4,b5,b6,b7,b8,b9;

    Button badd,bsub,bmul,bdiv,bper,beql,bc;public void init(){t = new TextField(50);bo = new Button("0");b1 = new Button("1");b2 = new Button("2");b3 = new Button("3");b4 = new Button("4");

    b5 = new Button("5");b6 = new Button("6");b7 = new Button("7");b8 = new Button("8");b9 = new Button("9");badd = new Button("+");bsub = new Button("-");bmul = new Button("*");bdiv = new Button("/");bper = new Button("%");bc = new Button("c");beql = new Button("=");add(t);add(bo);add(b1);

    add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);add(b9);add(badd);add(bsub);add(bmul);add(bdiv);add(bper);add(bc);add(beql);bo.addActionListener(this);b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);

    Holymary Institute of Technology & Sciences 35

  • 8/3/2019 Java Lab Manual 1 It

    36/57

  • 8/3/2019 Java Lab Manual 1 It

    37/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    k=k*10+4;

    t.setText(String.valueOf(k));}if(ae.getSource()==b5){int k=Integer.parseInt(t.getText());k=k*10+5;t.setText(String.valueOf(k));}if(ae.getSource()==b6){int k=Integer.parseInt(t.getText());k=k*10+6;t.setText(String.valueOf(k));}if(ae.getSource()==b7){int k=Integer.parseInt(t.getText());k=k*10+7;t.setText(String.valueOf(k));}if(ae.getSource()==b8){int k=Integer.parseInt(t.getText());k=k*10+8;t.setText(String.valueOf(k));

    }if(ae.getSource()==b9){int k=Integer.parseInt(t.getText());k=k*10+9;t.setText(String.valueOf(k));}

    if(ae.getSource()==badd){tmp=Integer.parseInt(t.getText());p=1;t.setText("0");}

    if(ae.getSource()==bsub){tmp=Integer.parseInt(t.getText());p=2;t.setText("0");}if(ae.getSource()==bmul){tmp=Integer.parseInt(t.getText());

    Holymary Institute of Technology & Sciences 37

  • 8/3/2019 Java Lab Manual 1 It

    38/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    p=3;

    t.setText("0");}if(ae.getSource()==bdiv){tmp=Integer.parseInt(t.getText());p=4;t.setText("0");}if(ae.getSource()==bper){tmp=Integer.parseInt(t.getText());p=5;t.setText("0");}if(ae.getSource()==beql){float newval=Integer.parseInt(t.getText());float res=0;switch(p){case 1:res=tmp+newval;break;case 2:res=tmp-newval;

    break;case 3:res=tmp*newval;break;case 4:res=tmp/newval;break;case 5:res=tmp%newval;break;}t.setText(String.valueOf(res));}

    }}

    Holymary Institute of Technology & Sciences 38

  • 8/3/2019 Java Lab Manual 1 It

    39/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Output:

    Holymary Institute of Technology & Sciences 39

  • 8/3/2019 Java Lab Manual 1 It

    40/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 8 :

    a) Write a Java program for handling mouse events.

    import java.awt.*;import java.awt.event.*;import java.applet.Applet;public class LabPro15 extends Applet implements MouseListener{

    String msg="welcome";int x=100,y=100;public void init(){addMouseListener(this);}public void paint(Graphics g){

    g.drawString(msg,x,y);}public void update(Graphics g){

    paint(g);}public void mouseClicked(MouseEvent e){}

    public void mousePressed(MouseEvent e){

    msg="to";x=150;y=150;repaint();

    }public void mouseReleased(MouseEvent e){

    msg="gmrit";x=200;y=200;repaint();

    }public void mouseEntered(MouseEvent e){}

    public void mouseExited(MouseEvent e){}

    }

    Output:

    Holymary Institute of Technology & Sciences 40

  • 8/3/2019 Java Lab Manual 1 It

    41/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Holymary Institute of Technology & Sciences 41

  • 8/3/2019 Java Lab Manual 1 It

    42/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 9 :

    a) Write a Java program that creates three threads. First thread displays GoodMorning every one second, the second thread displays Hello every two secondsand the third thread displays Welcome every three seconds.

    class tst implements Runnable{

    String name;Thread t;tst(String threadname){name = threadname;t = new Thread(this, name);

    System.out.println("newThread : " +t);t.start();}public void run(){try{for(int i=5;i > 0;i--){System.out.println(name + ": " + i);Thread.sleep(1000);

    }}

    catch(InterruptedException e){System.out.println(name + "Interrupted");}System.out.println(name + "exiting");

    }}class LabPro16{

    public static void main(String args[]){

    new tst("Good Morning");new tst("Hello");new tst("Welcome");try{Thread.sleep(10000);

    }catch(InterruptedException e){

    Holymary Institute of Technology & Sciences 42

  • 8/3/2019 Java Lab Manual 1 It

    43/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    System.out.println("Main thread Interrupted");

    }System.out.println("Main thread exiting");

    }}

    Output:

    run:newThread : Thread[Good Morning,5,main]newThread : Thread[Hello,5,main]newThread : Thread[Welcome,5,main]Good Morning: 5Hello: 5Welcome: 5Good Morning: 4Hello: 4Welcome: 4Good Morning: 3Welcome: 3Hello: 3Good Morning: 2

    Hello: 2Welcome: 2Good Morning: 1Welcome: 1Hello: 1Good MorningexitingWelcomeexitingHelloexiting

    Holymary Institute of Technology & Sciences 43

  • 8/3/2019 Java Lab Manual 1 It

    44/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 9 :

    b) Write a Java program that correctly implements producer consumer problemusing the concept of inter thread communication.

    class LabPro17{

    int n;boolean vs = false;synchronized int get(){if(!vs)try{wait();}catch(InterruptedException e){ System.out.println("InterruptedException caught");}System.out.println("got:" + n);vs = false;

    notify();return n;}synchronized int put(int n){if(vs)try{wait();}catch(InterruptedException e)

    { System.out.println("InterruptedException caught");}this.n = n;vs = true;

    System.out.println("put:" + n);notify();

    return n;}

    }class Producer implements Runnable{

    LabPro17 k;Producer(LabPro17 k){

    this.k = k;new Thread(this, "Producer").start();

    }public void run(){int i = 0;while(true){k.put(i++);}}

    Holymary Institute of Technology & Sciences 44

  • 8/3/2019 Java Lab Manual 1 It

    45/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    }

    class Consumer implements Runnable{

    LabPro17 k;Consumer(LabPro17 k){this.k = k;new Thread(this, "Consumer").start();

    }public void run(){while(true)

    {k.get();}}

    }class PCFixed{public static void main(String args[])

    {LabPro17 k = new LabPro17();new Producer(k);new Consumer(k);System.out.println("press control - c to stop. ");

    }}

    Output:

    run:

    put:0

    press control - c to stop.

    got:0

    put:1

    got:1

    put:2

    got:2

    put:3

    got:3

    Holymary Institute of Technology & Sciences 45

  • 8/3/2019 Java Lab Manual 1 It

    46/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    put:4

    got:4

    put:5

    got:5

    put:6

    got:6

    put:7

    got:7

    put:8

    got:8

    put:9

    got:9

    put:10

    got:10

    put:11

    got:11

    put:12

    got:12

    put:13

    got:13

    put:14

    got:14

    put:15

    got:15

    Holymary Institute of Technology & Sciences 46

  • 8/3/2019 Java Lab Manual 1 It

    47/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    put:16

    got:16

    put:17

    got:17

    put:18

    got:18

    put:19

    got:19

    Holymary Institute of Technology & Sciences 47

  • 8/3/2019 Java Lab Manual 1 It

    48/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 10 :

    Write a program that creates a user interface to perform integer divisions. Theuser enters two numbers in the textfields, Num1 and Num2. The division of Num1and Num2 is displayed in the Result field when the Divide button is clicked. IfNum1 or Num2 were not an integer, the program would throw aNumberFormatException. If Num2 were Zero, the program would throw anArithmeticException Display the exception in a message dialog box.

    import javax.swing.*;import java.awt.*;import java.awt.event.*;

    public class Division extends JFrameimplements ActionListener

    {Container c;JButton btn;JLabel lbl1,lbl2,lbl3;JTextField tf1,tf2,tf3;

    JPanel p;SuhritDivision(){

    super("Exception Handler");c=getContentPane();

    c.setBackground(Color.red);btn=new JButton("DIVIDE");btn.addActionListener(this);

    tf1=new JTextField(30);tf2=new JTextField(30);tf3=new JTextField(30);lbl1=new JLabel("NUM 1");lbl2=new JLabel("NUM 2");lbl3=new JLabel("RESULT");

    p=new JPanel();p.setLayout(new GridLayout(3,2));

    p.add(lbl1); p.add(tf1);p.add(lbl2); p.add(tf2);p.add(lbl3); p.add(tf3);

    c.add(new JLabel("Division"),"North");c.add(p,"Center");c.add(btn,"South");

    }

    Holymary Institute of Technology & Sciences 48

  • 8/3/2019 Java Lab Manual 1 It

    49/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    public void actionPerformed(ActionEvent e){if(e.getSource()==btn){try{int a=Integer.parseInt(tf1.getText());int b=Integer.parseInt(tf2.getText());int c=a/b;tf3.setText(""+c);

    }catch(NumberFormatException ex){

    tf3.setText("--");JOptionPane.showMessageDialog(this,"Only Integer Division");

    }catch(ArithmeticException ex){

    tf3.setText("--");JOptionPane.showMessageDialog(this,"Division by zero");

    }catch(Exception ex){

    tf3.setText("--");JOptionPane.showMessageDialog(this,"Other Err "+ex.getMessage());

    }}}

    public static void main(String args[]){

    Division b=new Division();b.setSize(300,300);b.setVisible(true);

    }}

    Holymary Institute of Technology & Sciences 49

  • 8/3/2019 Java Lab Manual 1 It

    50/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Output:

    Holymary Institute of Technology & Sciences 50

  • 8/3/2019 Java Lab Manual 1 It

    51/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 11 :

    Write a Java program that allows the user to draw lines, rectangles and ovals.

    import java.awt.*;import java.awt.event.*;import java.applet.*;public class LabPro19 extends Applet

    implements MouseListener,ActionListener,MouseMotionListener{Button lines,ovals,rect,free;int ch;int x,y,x2,y2,k;

    public void mouseClicked(MouseEvent e){

    }public void mousePressed(MouseEvent e){

    x=e.getX();y=e.getY();

    }

    public void mouseReleased(MouseEvent e){

    x2=e.getX();y2=e.getY();

    repaint();}public void mouseEntered(MouseEvent e){

    }public void mouseExited(MouseEvent e){

    }

    public void mouseDragged(MouseEvent e){}public void mouseMoved(MouseEvent e){if(k==1){x2=e.getX();y2=e.getY();

    Holymary Institute of Technology & Sciences 51

  • 8/3/2019 Java Lab Manual 1 It

    52/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    repaint();}

    }

    public void init(){lines=new Button("line");ovals=new Button("oval");rect =new Button("rect");free=new Button("free hand");

    add(lines);add(ovals);add(rect);add(free);lines.addActionListener(this);ovals.addActionListener(this);rect.addActionListener(this);free.addActionListener(this);addMouseListener(this);addMouseMotionListener(this);}public void actionPerformed(ActionEvent ki){

    if(ki.getSource()==lines){ ch=1; k=0;}else if(ki.getSource()==ovals){ ch=2; k=0;}else if(ki.getSource()==rect)

    {ch=3; k=0;}else if(ki.getSource()==free){ ch=4; k=1;}

    }public void paint(Graphics g){switch (ch){case 1:g.drawLine(x,y,x2,y2);

    break;case 2:g.drawOval(x,y,(x2-x),(y2-y));

    break;

    case 3:g.drawRect(x,y,(x2-x),(y2-y));break;

    case 4:repaint();break;

    }}

    }

    Holymary Institute of Technology & Sciences 52

  • 8/3/2019 Java Lab Manual 1 It

    53/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Output:

    Holymary Institute of Technology & Sciences 53

  • 8/3/2019 Java Lab Manual 1 It

    54/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Holymary Institute of Technology & Sciences 54

  • 8/3/2019 Java Lab Manual 1 It

    55/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week:12

    a) Write a Java program to create Multiple Threads

    class MyThread extends Thread{MyThread(String s){super(s);start();

    }public void run(){for(int i=0;i

  • 8/3/2019 Java Lab Manual 1 It

    56/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Holymary Institute of Technology & Sciences 56

  • 8/3/2019 Java Lab Manual 1 It

    57/57

    HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES

    (college of Engineering)

    Subject:Java Lab Manual Regulation:R07

    Course: II B.Tech. II Sem. Branch: IT

    Week 12:

    b) Write a java program to find the your Host IP Address

    import java.net.*;

    public class Ipaddress{public static void main(String args[]){try{InetAddress local= InetAddress.getLocalHost();System.out.println ("Local IP Address is : " + local);

    }catch (UnknownHostException e){System.err.println ("Can't detect IP Address : " + e);

    }}

    }