Java Lab 2-1 Manual It & Cse2009-10

download Java Lab 2-1 Manual It & Cse2009-10

of 79

Transcript of Java Lab 2-1 Manual It & Cse2009-10

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    1/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    1. Write a Java program to find the Quardratic roots of the equation ax2+bx+c=0.

    import java.io.*;class Factors

    {

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

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

    System.out.println("enter a,b,c values");int a=Integer.parseInt(br.readLine());

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

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

    int f=((b*b)-(4*a*c));if(f>0)

    {

    double d=(((-b)+Math.sqrt(f))/(2*a));double e=(((-b)-Math.sqrt(f))/(2*a));

    System.out.println(roots are:);System.out.println("root1="+d+"\t"+root2=+e);

    System.out.println("roots are real");

    }else if(f==0)

    {

    System.out.println("roots are equal");

    }else

    {

    System.out.println("roots are imaginary");}

    }

    }

    Output:

    Enter a,b,c Values

    25

    2

    roots are :

    root1=-0.5 root2=-2.0roots are real

    SVEC, Suryapet

    1

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    2/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    2. Write a Java program to print Fibonacii series upto given number.

    import java.io.*;

    public class Fib

    {

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

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));int f0=0,f1=1,fib=1;

    System.out.println("Enter n value");

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

    System.out.println("Fibonacii series is:");System.out.println(f0);

    System.out.println(f1);

    for(int i=0;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    3/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    3. Write a Java program to print prime numbers in a given range.

    import java.util.*;

    class Prime

    {

    public static void main(String args[]){

    int i,j;Scanner br =new Scanner(System.in);

    System.out.println("Enter the starting range");

    int s=br.nextInt();

    System.out.println("Enter the end range");int e=br.nextInt();

    for(i=s;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    4/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    4. Write a Java program to find the total and average of a student marks.

    import java.io.*;class DemoArray

    {

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

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

    System.out.println("Enter how many sub");int n=Integer.parseInt(br.readLine());

    int marks[]=new int[n];

    for(int i=0;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    5/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    5. Write a Java program to display multiplication table.

    import java.io.*;class Multiplication

    {

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

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

    System.out.println("enter n value");int n=Integer.parseInt(br.readLine());

    System.out.println("...Multiplication Table...");

    for(int i=1;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    6/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    6. Write a java program to check whether given number is prime or not.

    import java.util.*;

    import java.lang.*;

    import java.io.*;

    class Prime1{

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

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

    System.out.println("enter n value");

    int n=Integer.parseInt(br.readLine());int count=0;

    for(int i=1;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    7/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    7. Write a Java application on Matrix Addition.

    import java.io.*;class MA

    {

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

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

    //size of mat1System.out.println("Enter no of rows and cols of mat1");

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

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

    //size of mat2System.out.println("Enter no of rows and cols of mat2");

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

    int q=Integer.parseInt(br.readLine());int i,j;

    int mat1[][]=new int[m][n];int mat2[][]=new int[p][q];int mat3[][]=new int[m][q];

    if(m==q&&n==p)

    {

    //first matrixSystem.out.println("Enter elements in mat1:");

    for(i=0;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    8/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    }

    //addition

    for(i=0;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    9/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    8. Write a Java program on Matrix Multiplication.

    // matrix multiplicationimport java.io.*;

    class MM

    {

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

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//size of mat1

    System.out.println("Enter no of rows and cols of mat1");

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

    int n=Integer.parseInt(br.readLine());//size of mat2

    System.out.println("Enter no of rows and cols of mat2");

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

    int i,j;int mat1[][]=new int[m][n];int mat2[][]=new int[p][q];

    int mat3[][]=new int[m][q];

    if(n==p)

    {//first matrix

    System.out.println("Enter elements in mat1:");

    for(i=0;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    10/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    System.out.println();

    }

    //multiplicationfor(i=0;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    11/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    9. Write a java program for array copying method.

    import java.io.*;public class CopyArray

    {

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

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

    System.out.println("Enter array length ");int n=Integer.parseInt(br.readLine());

    String array1[]=new String[n];

    String array2[]=new String[n];

    System.out.println("Enter strings into array1: ");System.out.println("...................................");

    for(int i=0;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    12/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    Output:

    Enter array length5

    Enter strings into array1:

    ...................................

    Enter the string1q

    Enter the string2w

    Enter the string3

    e

    Enter the string4r

    Enter the string5

    tEnter strings into array2:

    ...................................Enter the string1a

    Enter the string2

    s

    Enter the string3d

    Enter the string4

    fEnter the string5

    g

    Array1:[ q w e r t]

    Array2:

    [ a s d f g]AfterCopying:

    [ q w d f g]

    SVEC, Suryapet

    12

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    13/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    10. Write a program on Static method.

    import java.io.*;

    class StaticMethod{

    static int sum(int a,int b)

    {

    int c=a+b;return c;

    }}

    class Demo

    {

    public static void main(String args[]){

    int x=StaticMethod.sum(10,50);

    System.out.println(x);}

    }

    Output:

    sum=60

    SVEC, Suryapet

    13

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    14/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    11. Write a java application on Static variable.

    import java.io.*;class Test

    {

    static int x;

    int y;void inc()

    {x=x+1;

    y=y+1;

    }

    void show(){

    System.out.println("x value is:"+x);

    System.out.println("y value is:"+y);}

    }class Static{

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

    {

    Test t1=new Test();Test t2=new Test();

    System.out.println("First obj values:");

    t1.inc();t1.show();

    System.out.println("Second obj values:");

    t2.inc();t2.show();

    }

    }

    Output:

    First obj values:

    x value is:1y value is:1

    Second obj values:

    x value is:2

    y value is:1

    SVEC, Suryapet

    14

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    15/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    12. Write a program on Constructor.

    class Rectangle{

    int l,w;

    void getData(int x,int y)

    {l=x;

    w=y;}

    int rectArea()

    {

    return(l*w);}

    }

    class RectArea{

    public static void main(String args[ ]){Rectangle r1=new Rectangle( );

    r1.getData(10,15);

    int k=r1.rectArea( );System.out.print("Area is:"+k);

    }

    }

    Output :

    Area is:150

    SVEC, Suryapet

    15

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    16/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    13. Write a java program for This keyword

    import java.io.*;class Test

    {

    int a,b;

    void getData(){

    this.a=10;this.b=20;

    }

    void show()

    {System.out.println("a value is:"+a);

    System.out.println("b value is:"+b);

    }}

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

    {

    Test t=new Test();

    t.getData();t.show();

    }

    }

    Output:

    a value is:10

    b value is:20

    SVEC, Suryapet

    16

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    17/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    14 Write a java program on Command Line Arguments.

    public class GetCommandLine{

    public static void main(String args[]) {

    System.out.println("----->>Getting command line arguments --");

    for(int i=0;ijavac GetCommandLine.java

    >java GetCommandLine 10 20 30 40 50

    ----->>Getting command line arguments --

    Argument [1] is = 10Argument [2] is = 20

    Argument [3] is = 30

    Argument [4] is = 40

    Argument [5] is = 50

    15. Write a program to find sum of given Command Line Arguments.

    SVEC, Suryapet

    17

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    18/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.io.*;

    import java.lang.*;public class Cmd

    {

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

    {int sum=0;

    for(int i=0;ijavac Cmd.java

    >java Cmd 10 15 20 25 30

    sum is=100

    16. Write a program on Method Overloding.

    SVEC, Suryapet

    18

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    19/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.io.*;

    class MethodOverLoad{

    void calValue()

    {

    int x=20;x=x*x;

    System.out.println("Sqrt of x is:"+x);}

    void calValue(int y)

    {

    y=y*y*y;System.out.println("Cube of y is:"+y);

    }

    void calValue(int m,int n){

    int z=m*n;System.out.println("Product of m and n is:"+z);}

    }

    class MOV

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

    {

    MethodOverLoad m=new MethodOverLoad();m.calValue();

    m.calValue(10,20);

    m.calValue(10);}

    }

    Output:

    Sqrt of x is:400

    Product of m and n is:200Cube of y is:1000

    17. Write a program on Method Overriding.

    SVEC, Suryapet

    19

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    20/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.io.*;

    class One{

    int calculate(int n)

    {

    return n;}

    }class Two extends One

    {

    int calculate(int n)

    {return(n*n);

    }

    }class MOR

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

    Two t=new Two();

    int m=t.calculate(3);

    System.out.println("Multiplication is:"+m);}

    }

    Output:

    Multiplication is:9

    18. Write a program on Constructor Overloding.

    SVEC, Suryapet

    20

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    21/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.lang.*;

    class Test{

    int a,b;

    Test()

    {a=1;

    b=2;}

    Test(int x)

    {

    a=b=x;}

    Test(int a, int b)

    {this.a=a;

    this.b=b;}void show()

    {

    System.out.println("a value is:"+a);

    System.out.println("b value is:"+b);}

    }

    class COL{

    public static void main(String args[])

    {Test t1=new Test();

    t1.show();

    Test t2=new Test(10);t2.show();

    Test t3=new Test(15,25);

    t3.show();

    }}

    Output:

    a value is:1

    b value is:2a value is:10

    b value is:10

    a value is:15

    b value is:25

    19. Write a java program to calculate Factorial of a given number using Recursion concept.

    SVEC, Suryapet

    21

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    22/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.io.*;

    import java.util.*;class Factorial

    {

    int fact(int n)

    {int r;

    if(n==1)return 1;

    else

    r=fact(n-1)*n;

    return r;}

    }

    class Recursion{

    public static void main(String args[])throws IOException{Factorial f=new Factorial();

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

    System.out.println("Enter num to find factorial");

    int x=Integer.parseInt(br.readLine());int k=f.fact(x);

    System.out.println("The fatorial of"+x+"is="+k);

    }}

    Output:

    Enter num to find factorial

    5The fatorial of5is=120

    20. Write a program on InnerClass concept.

    SVEC, Suryapet

    22

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    23/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    class Outer

    {Inner in=new Inner();

    class Inner

    {

    public void sayHello(){

    System.out.println("Hello");}

    }

    }

    class DemoInner{

    public static void main(String args[])

    {Outer o=new Outer();

    o.in.sayHello();}}

    Output:

    Hello

    21. Write a java program to check whether string is Palindrome or not

    SVEC, Suryapet

    23

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    24/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.io.*;

    class Polindrome{

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

    {

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));System.out.println("Enter any string");

    String x=new String(br.readLine());StringBuffer y=new StringBuffer(x) ;

    StringBuffer z=y.reverse();

    String s=z.toString();

    if(x.compareTo(s)==0){

    System.out.println("The string is polindrome");

    }else

    {System.out.println("The string is not a polindrome");}

    }

    }

    Output:

    Enter any stringmom

    The string is polindrome

    Enter any string

    god

    The string is not a polindrome

    22. Write a java program to print given strings in alphabetical order.

    SVEC, Suryapet

    24

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    25/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.util.*;

    import java.io.*;class Alpha

    {

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

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

    int i,j;System.out.println("Enter how many no of strings to print in order ");

    String str=br.readLine();

    int n=Integer.parseInt(str);

    String s[]=new String[n];for(i=0;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    26/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.io.*;

    class NumSort{

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

    {

    int i,j;BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter how many numbers print in order: ");int m=Integer.parseInt(br.readLine());

    int num[]=new int[m];

    int n=num.length;

    System.out.println("Enter numbers:");for(i=0;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    27/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    Enter how many numbers print in order:

    5Enter numbers:

    6

    8

    37

    4Before sorting:

    6

    8

    37

    4

    Sorted list is:8

    764

    3

    24. Write a java program on StringTokenizer class.

    SVEC, Suryapet

    27

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    28/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.util.*;

    import java.lang.*;import java.io.*;

    class StringToken

    {

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

    String str=new String("welcome to java lab");StringTokenizer stz=new StringTokenizer(str);

    System.out.println("No.of words are:");

    System.out.println(stz.countTokens());

    System.out.println("Tokens are:");while(stz.hasMoreTokens())

    {

    System.out.println(stz.nextToken());}

    int count=0;for(int i=0;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    29/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.io.*;

    import java.util.*;class AddTokens

    {

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

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

    System.out.println("Enter numbers");String s=br.readLine();

    StringTokenizer str=new StringTokenizer(s ,"+");

    int sum=0;

    while(str.hasMoreTokens()){

    sum=sum+Integer.parseInt(str.nextToken());

    }System.out.println("Addition of tokens="+sum);

    }}

    Output:

    Enter numbers100+100+100

    Addition of tokens=300

    26. Write a java program to display a Peramid.

    SVEC, Suryapet

    29

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    30/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    class Peramid

    {public static void main(String args[])

    {

    int i,j,k,l;

    for(i=1;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    31/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    interface Father

    {int PROP1=500000;

    }

    interface Mother

    {int PROP2=800000;

    }class Child implements Father,Mother

    {

    void property()

    {System.out.println("child property="+(PROP1+PROP2));

    }

    }class MultiInher

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

    Child ch=new Child();

    ch.property();

    }

    Output:

    child property=1300000

    28. Write an example using StringTokenizer class.

    SVEC, Suryapet

    31

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    32/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.io.*;

    import java.util.*;class DiffData

    {

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

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

    System.out.println("enter name,age,salary");String s=br.readLine();

    StringTokenizer str=new StringTokenizer(s,",");

    String s1=str.nextToken();

    String s2=str.nextToken();String s3=str.nextToken();

    s1=s1.trim();

    s2=s2.trim();s3=s3.trim();

    String name=s1;int age=Integer.parseInt(s2);float salary=Float.parseFloat(s3);

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

    System.out.println("Age="+age);

    System.out.println("Salary="+salary);}

    }

    Output:

    enter name,age,salarypuja,24,15000

    name=puja

    Age=24Salary=15000.0

    29. Write a java program to display Employee data.

    SVEC, Suryapet

    32

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    33/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.io.*;

    class Employee{

    int id;

    String name;

    Employee(int i,String n){

    id=i;name=n;

    }

    void displayData()

    {System.out.println(id+"\t"+name);

    }

    }class Group

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

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

    Employee arr[]=new Employee[5];

    for(int i=0;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    34/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    1

    enter namerani

    enter id

    2

    enter namebhavya

    enter id3

    enter name

    madhu

    enter id4

    enter name

    peethienter id

    5enter namesonu

    Employee data

    1 rani

    2 bhavya3 madhu

    4 peethi

    5 sonu

    30. Write a java program using Super keyword.

    SVEC, Suryapet

    34

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    35/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.io.*;

    class A{

    int a;

    A(int a)

    {this.a=a;

    }void showA()

    {

    System.out.println("The value of a is "+a);

    }}

    class B extends A

    {int b;

    B(int a,int b){super(a);

    this.b=b;

    }

    void showB(){

    System.out.println("The value of b is "+b);

    }}

    class SuperKey

    {public static void main(String args[])

    {

    B o=new B(100,200);o.showA();

    o.showB();

    }

    }

    Output:

    The value of a is 100The value of b is 200

    31. Write a java program using Abstract class.

    SVEC, Suryapet

    35

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    36/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    abstract class A

    {abstract void call();

    void callMethod()

    {

    System.out.println("This is concreate method");}

    }class B extends A

    {

    void call()

    {System.out.println("This is implementation of class ");

    }

    }class AbstractDemo

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

    B b=new B();

    b.call();

    b.callMethod();}

    }

    Output:

    This is implementation of class

    This is concreate method

    32. Write a java program on creating package.

    SVEC, Suryapet

    36

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    37/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    //creating own package.

    package pack;

    public class Add

    {

    public int i,j;public Add(int i,int j)

    {this.i=i;

    this.j=j;

    }

    public void add(){

    System.out.println("Details");

    System.out.println(".................");System.out.println("Additon is: "+(i+j));

    }}

    //implementation of a package.

    import pack.Add;

    class PackExe

    {public static void main(String args[])

    {

    Add a=new Add(20,45);a.add();

    }

    }

    Output:

    >javac -d . Add.java

    >javac PackExe.java

    >java PackExe

    Details.................

    Additon is: 65

    33. Write a java program on Interface concept.

    SVEC, Suryapet

    37

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    38/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    interface MyInterface

    {public void sayHello();

    }

    class Subclass implements MyInterface

    {public void sayHello()

    {System.out.println("hello");

    }

    }

    class DemoInterface{

    public static void main(String args[ ])

    {Subclass i=new Subclass();

    i.sayHello();}}

    Output:

    hello

    34. Write a java program on Stack ADT.

    SVEC, Suryapet

    38

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    39/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.io.*;

    import java.util.*;class StackDemo

    {

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

    {Stack st=new Stack();

    int choice=0;int position,element;

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

    while(choice

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    40/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    STACK OPERATION

    1 push an element2 pop an element

    3 search an element

    4 Exit

    your choice:1

    enter elements:11

    stack contents:[11]

    STACK OPERATION

    1 push an element2 pop an element

    3 search an element

    4 Exityour choice:

    1enter elements:22

    stack contents:[11, 22]

    STACK OPERATION

    1 push an element2 pop an element

    3 search an element

    4 Exityour choice:

    3

    which element to search22

    position1

    stack contents:[11, 22]STACK OPERATION

    1 push an element

    2 pop an element

    3 search an element4 Exit

    your choice:

    2

    popped=22stack contents:[11]

    STACK OPERATION1 push an element

    2 pop an element

    3 search an element

    4 Exityour choice:

    4

    35. Write a java program on data into a file.

    SVEC, Suryapet

    40

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    41/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.io.*;

    class CreateFile{

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

    {

    DataInputStream dis=new DataInputStream(System.in);FileOutputStream fout=new FileOutputStream("myfile.txt");

    System.out.println("enter text & enter @ at the end");char ch;

    while((ch=(char)dis.read())!='@')

    fout.write(ch);

    fout.close();}

    }

    Output:

    enter text & enter @ at the endwelcome to svec @

    36. Write a java program on try,catch,finally blocks.

    SVEC, Suryapet

    41

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    42/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    class DemoEx1

    {public static void main(String args[])

    {

    try

    {int a,b,c;

    a=Integer.parseInt(args[0]);b=Integer.parseInt(args[1]);

    c=a/b;

    System.out.println("Result is ="+c);

    }catch (ArithmeticException ae)

    {

    System.out.println("not enter zeros");}

    catch (ArrayIndexOutOfBoundsException e){System.out.println("enter at least 2 no's");

    }

    catch (NumberFormatException ne)

    {System.out.println("enter int values");

    }

    finally{

    System.out.println("This is final");

    }}

    }

    Output:

    >javac DemoEx1.java

    >java DemoEx1

    enter at least 2 no's

    This is final

    >java DemoEx1 12 23

    Result is =0

    This is final

    >java DemoEx1 12 3

    Result is =4This is final

    37. Write a java program to search a file.

    SVEC, Suryapet

    42

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    43/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.io.*;

    class DisplayFile{

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

    {

    int i;FileInputStream fin;

    try{

    fin=new FileInputStream(args[0]);

    }

    catch(FileNotFoundException e){

    System.out.println("File Not Found");

    return;}

    do{i=fin.read();

    if(i!=-1)

    System.out.println((char)i);

    }while(i!=-1);

    fin.close();

    }}

    one.txt:

    hai hello

    Output:

    >javac DisplayFile.java

    >java DisplayFile one.txt

    h

    ai

    h

    e

    l

    lo

    38. Write a java program on creating own exception.

    SVEC, Suryapet

    43

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    44/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    class VException extends Exception

    {VException(String s)

    {

    super(s);

    }}

    class Voting{

    static void displayvote(int age)throws VException

    {

    if(agejavac UserExe.java

    >java UserExe 23

    Candidate is eligible to vote

    >java UserExe 16

    Invalid age

    39. Write a java program to copy a file.

    SVEC, Suryapet

    44

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    45/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.io.*;

    class DisplayFile1{

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

    {

    int i;FileInputStream fin;

    FileOutputStream fout;try

    {

    fin=new FileInputStream(args[0]);

    }catch(FileNotFoundException e)

    {

    System.out.println("Input File Not Found");return;

    }catch(ArrayIndexOutOfBoundsException e){

    System.out.println("Usage:showFile");

    return;

    }//opening o/p file

    try

    {fout=new FileOutputStream(args[1]);

    }

    catch(FileNotFoundException e){

    System.out.println("Error in opening output file");

    return;}

    catch(ArrayIndexOutOfBoundsException e)

    {

    System.out.println("Usage:copy file from to");return;

    }

    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();

    }

    }

    Output:

    SVEC, Suryapet

    45

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    46/79

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    47/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    class MyThread extends Thread

    {public void run()

    {

    try

    {for(int i=1;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    48/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    class MyThread implements Runnable

    {public void run()

    {

    try

    {for(int i=1;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    49/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    class MyThread1 extends Thread

    {public void run()

    {

    try

    {for(int i=0;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    50/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    class MyThread1 extends Thread

    {public void run()

    {

    try

    {for(int i=0;i

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    51/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    class ThreadGroupPro

    {public static void main(String args[])

    {

    ThreadGroup tg=new ThreadGroup("subgroup1");

    Thread t1=new Thread(tg,"Thread1");Thread t2=new Thread(tg,"Thread2");

    Thread t3=new Thread(tg,"Thread3");tg=new ThreadGroup("subgroup2");

    Thread t4=new Thread(tg,"Thread4");

    Thread t5=new Thread(tg,"Thread5");

    tg=Thread.currentThread().getThreadGroup();int age=tg.activeGroupCount();

    System.out.println("No.of sub groups="+age);

    System.out.println("Groups are");tg.list();

    }}

    Output:

    No.of sub groups=2

    Groups are

    java.lang.ThreadGroup[name=main,maxpri=10]Thread[main,5,main]

    java.lang.ThreadGroup[name=subgroup1,maxpri=10]

    java.lang.ThreadGroup[name=subgroup2,maxpri=10]

    45. Write a Java program to draw different shapes using applets

    SVEC, Suryapet

    51

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    52/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    LinRect.java:

    import java.applet.*;

    import java.awt.*;

    public class LinRect extends Applet

    {public void paint(Graphics g)

    {g.drawLine(100,100,500,500);

    g.drawRect(10,60,40,30);

    g.fillRect(60,10,30,80);

    g.drawRoundRect(10,100,80,50,10,10);g.drawLine(100,10,230,140);

    g.drawLine(100,140,230,10);

    }}

    LinRect.html:

    Output:

    >javac LinRect.java

    >appletviewer LinRect.html

    46. Write a java program to pass parameters to applets.

    SVEC, Suryapet

    52

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    53/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    HelloParam.java:

    import java.awt.*;

    import java.applet.*;

    public class HelloParam extends Applet{

    String str,n,x;public void init()

    {

    str=getParameter("s");

    n=getParameter("num");}

    public void paint(Graphics g)

    {g.drawString("Name is"+str,10,20);

    g.drawString("Number is"+n,10,50);}}

    HelloParam.html:

    Output:

    >javac HelloParam.java

    >appletviewer HelloParam.html

    47. Write a java program to get values at runtime by using the applets.

    SVEC, Suryapet

    53

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    54/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    UserIn.java:

    import java.awt.*;

    import java.applet.*;

    import java.awt.event.*;

    public class UserIn extends Applet{

    TextField text1,text2;public void init()

    {

    setBackground(Color.black);

    setForeground(Color.red);text1=new TextField(8);

    text2=new TextField(8);

    add(text1);add(text2);

    text1.setText("0");text2.setText("0");}

    public void paint(Graphics g)

    {

    int x=0,y=0,z=0;String s1,s2,s;

    g.drawString("Enter no in each box",10,50);

    try{

    s1=text1.getText();

    x=Integer.parseInt(s1);s2=text2.getText();

    y=Integer.parseInt(s2);

    }catch(Exception e)

    {

    System.out.println(e);

    }z=x+y;

    s=String.valueOf(z);

    g.drawString("The sum is",10,75);

    g.drawString(s,150,75);}

    public boolean action(Event ent,Object obj){

    repaint();

    return true;

    }}

    UserIn.html:

    Output:

    SVEC, Suryapet

    54

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    55/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    >javac UserIn.java

    >appletviewer UserIn.html

    48. Write a java program to add all components to the container.

    SVEC, Suryapet

    55

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    56/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    ControlDemo.java:

    import java.awt.*;

    import java.applet.*;

    public class ControlDemo extends Applet

    {public void init()

    {setLayout(new FlowLayout(FlowLayout.LEFT,20,20));

    TextField tf=new TextField("TypeHere",20);

    TextArea ta=new TextArea(3,4);

    add(tf);add(ta);

    setForeground(Color.red);

    Label l=new Label("This is mech");add(l);

    Button b=new Button("Login");add(b);Choice c=new Choice();

    c.add(" ");

    c.add("Graduate");

    c.add("Postgraduate");add(c);

    List lt=new List(4,true);

    lt.add(" ");lt.add("INDIA");

    lt.add("USA");

    add(lt);Checkbox ck=new Checkbox("Cinema");

    CheckboxGroup ckg=new CheckboxGroup();add(ck);

    Checkbox rb1=new Checkbox("Female",true,ckg);

    add(rb1);

    Checkbox rb2=new Checkbox("Male",false,ckg);add(rb2);

    Scrollbar s=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,255);

    add(s);

    }}

    ControlDemo.html:

    Output:

    SVEC, Suryapet

    56

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    57/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    >javac ControlDemo.java

    >appletviewer ControlDemo.html

    49. Write a java to create login window user by AWT.

    SVEC, Suryapet

    57

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    58/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.awt.*;

    import java.io.*;class MyWindow3 extends Frame

    {

    MyWindow3()

    {setSize(200,200);

    setLayout(new FlowLayout());setTitle("Login screen");

    Label l1=new Label("username");

    Label l2=new Label("passward");

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

    Button b=new Button("login");

    tf2.setEchoChar('*');add(l1);

    add(tf1);add(l2);add(tf2);

    add(b);

    setVisible(true);

    }}

    class LoginWindow

    {public static void main(String args[ ])

    {

    MyWindow3 mw=new MyWindow3();}

    }

    Output:

    >javac LoginWindow.java

    >java LoginWindow

    50. Write a java program for FlowLayout.

    SVEC, Suryapet

    58

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    59/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.awt.*;

    class DemoFlowLayout extends Frame{

    public DemoFlowLayout()

    {

    setLayout(new FlowLayout());for(int i=1;ijavac DemoFlowLayout.java

    >java DemoFlowLayout

    51. Write a java program for GridLayout.

    SVEC, Suryapet

    59

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    60/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.awt.*;

    import java.applet.*;class DemoGridLayout extends Frame

    {

    public DemoGridLayout()

    {setLayout(new GridLayout(5,4));

    for(int i=1;ijavac DemoGridLayout.java

    >java DemoGridLayout

    52. Write a java program for BorderLayout.

    SVEC, Suryapet

    60

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    61/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.awt.*;

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

    class DemoBoarder extends Frame

    {

    Button b1,b2,b3,b4,b5;public DemoBoarder()

    {setLayout(new BorderLayout());

    b1=new Button("one");

    b2=new Button("two");

    b3=new Button("three");b4=new Button("four");

    b5=new Button("five");

    add(b1,"East");add(b2,"West");

    add(b3,"North");add(b4,"South");add(b5,"Center");

    setSize(300,300);

    setVisible(true);

    }public static void main(String args[])

    {

    DemoBoarder db=new DemoBoarder();}

    }

    Output:

    >javac DemoBoarder.java

    >java DemoBoarder

    53. Write a java program for CardLayout.

    SVEC, Suryapet

    61

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    62/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    CardLayoutDemo.java:

    import java.awt.*;

    import java.applet.*;

    import java.awt.event.*;

    public class CardLayoutDemo extends Applet implements ActionListener{

    CardLayout cl;Panel p;

    public void init()

    {

    p=new Panel();add(p);

    cl=new CardLayout();

    p.setLayout(cl);initialisation();

    }void initialisation(){

    Button b1=new Button("Button1");

    Button b2=new Button("Button2");

    Button b3=new Button("Button3");b1.addActionListener(this);

    b2.addActionListener(this);

    b3.addActionListener(this);p.add(b1,"Button1");

    p.add(b2,"Button2");

    p.add(b3,"Button3");}

    public void actionPerformed(ActionEvent e)

    {cl.next(p);

    }

    }

    CardLayoutDemo.html:

    Output:

    SVEC, Suryapet

    62

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    63/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    >javac CardLayoutDemo.java

    >appletviewer CardLayoutDemo.html

    54. Write a java program using HashSet method.

    SVEC, Suryapet

    63

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    64/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.util.*;

    class HS

    {public static void main(String args[])

    {

    HashSet hs=new HashSet();

    hs.add("india");hs.add("us");

    hs.add("uk");hs.add("japan");

    System.out.println("HashSet ="+hs);

    Iterator it=hs.iterator();

    System.out.println("elements using iterator");while(it.hasNext())

    {

    String s=(String)it.next();System.out.println(s);

    }}}

    Output:

    >javac HS.java

    >java HS

    HashSet =[uk, japan, us, india]elements using iterator

    uk

    japanus

    india

    55. Write a java program on Server and Client

    SVEC, Suryapet

    64

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    65/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    Server1.java:

    import java.io.*;import java.net.*;

    class Server1

    {

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

    ServerSocket ss=new ServerSocket(777);Socket s=ss.accept();

    System.out.println("connection establisted");

    OutputStream obj=s.getOutputStream();

    PrintStream ps=new PrintStream(obj);String str="Hello client";

    ps.println(str);

    ps.println("Good Bye");ps.close();

    ss.close();s.close();}

    }

    Client1.java:

    import java.io.*;

    import java.net.*;class Client1

    {

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

    Socket s=new Socket("localhost",777);

    InputStream obj=s.getInputStream();BufferedReader br=new BufferedReader(new InputStreamReader(obj));

    String str;

    while((str=br.readLine())!=null)

    System.out.println("from Server1:"+str);br.close();

    s.close();

    }

    }

    Output:

    SVEC, Suryapet

    65

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    66/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    From Server:

    >javac Server1.java

    >java Server1

    connection establisted

    From Client:

    >javac Client1.java

    >java Client1

    from Server1:Hello clientfrom Server1:Good Bye

    56. Write a java program on chat between Server and Client.

    SVEC, Suryapet

    66

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    67/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    Server2.java:

    import java.io.*;import java.net.*;

    class Server2

    {

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

    ServerSocket ss=new ServerSocket(777);Socket s=ss.accept();

    System.out.println("...........connection establisted..............");

    PrintStream ps=new PrintStream(s.getOutputStream());

    BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));BufferedReader kb=new BufferedReader(new InputStreamReader(System.in));

    while(true)

    {String str1,str;

    while((str=br.readLine())!=null){System.out.println(str);

    str1=kb.readLine();

    ps.println(str1);

    }ps.close();

    ss.close();

    s.close();br.close();

    kb.close();

    System.exit(0);}

    }

    }

    Client2.java:

    import java.io.*;import java.net.*;

    class Client2

    {

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

    Socket s=new Socket("localhost",777);DataOutputStream dos=new DataOutputStream(s.getOutputStream());

    BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));

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

    String str,str1;while(!(str=kb.readLine()).equals("exit"))

    {

    dos.writeBytes(str+"\n");str1=br.readLine();

    System.out.println(str1);

    }s.close();

    dos.close();

    br.close();

    kb.close();}}

    Output:

    SVEC, Suryapet

    67

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    68/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    From Server:

    >javac Server2.java

    >java Server2

    ...........connection establisted..............hello

    haihow r u

    iam fine

    bye.

    From Client:

    >javac Client2.java

    >java Client2hello

    hai

    how r u

    iam finebye.

    57. Write a java program on Canvas.

    SVEC, Suryapet

    68

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    69/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    CanvasDemo.java:

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

    public class CanvasDemo extends Applet

    {

    public void init(){

    setBackground(Color.red);CanvasExe ce=new CanvasExe();

    ce.setSize(200,200);

    ce.setBackground(Color.blue);

    ce.setVisible(true);add(ce);

    }

    class CanvasExe extends Canvas{

    public void paint(Graphics g){setForeground(Color.green);

    g.fillOval(30,0,80,80);

    g.drawString("Hello",50,100);

    }}

    }

    CanvasDemo.html:

    Output:

    SVEC, Suryapet

    69

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    70/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    >javac CanvasDemo.java

    >appletviewer Canvas.html

    58. Write a java program for EventHandling.

    SVEC, Suryapet

    70

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    71/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    import java.awt.*;

    import java.awt.event.*;

    class Mywindow extends Frame implements ActionListener{

    Button b;

    public Mywindow()

    {super("DemoEvents");

    setLayout(null);b=new Button("Exit");

    b.setBounds(150,200,100,300);

    add(b);

    b.addActionListener(this);}

    public void actionPerformed(ActionEvent e)

    {System.exit(0);

    }}class DemoEvent

    {

    public static void main(String args[])

    {Mywindow f=new Mywindow();

    f.setSize(200,200);

    f.setVisible(true);}

    }

    Output:

    >javac DemoEvent.java

    >java DemoEvent

    SVEC, Suryapet

    71

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    72/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    SVEC, Suryapet

    72

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    73/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    SVEC, Suryapet

    73

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    74/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    SVEC, Suryapet

    74

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    75/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    SVEC, Suryapet

    75

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    76/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    SVEC, Suryapet

    76

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    77/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    SVEC, Suryapet

    77

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    78/79

    OOPS Lab through JAVA, CSE & CSIT Depts.

    SVEC, Suryapet

    78

  • 7/29/2019 Java Lab 2-1 Manual It & Cse2009-10

    79/79

    OOPS Lab through JAVA, CSE & CSIT Depts. 79