ojava

Embed Size (px)

Citation preview

  • 8/2/2019 ojava

    1/36

    EXPERIMENT NO.-1

    Write a program to find the Factorial of a number using Command line

    argument.

    class factorial

    {

    public static void main(String arg[])

    {

    int n, factorial,i:

    factorial=1;

    n=Integer.parseInt(arg[0]);

    for(i=1;i

  • 8/2/2019 ojava

    2/36

  • 8/2/2019 ojava

    3/36

    EXPERIMENT NO.-2

    Write a program to find the Largest of the three numbers using Command

    line argument.

    class largest

    {

    public static void main(String arg[])

    {

    int a = Integer.prseInt(arg[0]);

    int b= Integer.parseInt(arg[1]);

    int c= Integer.parseInt(arg[2]);

    if(a>b && a>c)

    {

    System.out.println(a is greater);

    }

    else

    if(b>a && b>c)

    {

    System.out.println(b is greater);

    }

    else

    {

    System.out.println(c is greater);

    }

    System.out.println(NAME: Siddharth Nair);

  • 8/2/2019 ojava

    4/36

    System.out.println(Enroll No.: A2305309017);

    }

    }

  • 8/2/2019 ojava

    5/36

    EXPERIMENT NO.-3

    Write a program to print the Pyramid as given.

    class pyramid

    {

    public static void main(String arg[])

    {

    int n=6;

    for(int i=1;i

  • 8/2/2019 ojava

    6/36

    System.out.println(NAME: Siddharth Nair);

    System.out.println(Enroll No.: A2305309017);

    }

    }

  • 8/2/2019 ojava

    7/36

    EXPERIMENT NO.-4

    Write a program to find the Smallest of three numbers using BufferedReader

    IO operation.

    import java.io.*;

    class smallest

    {

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

    {

    int a,b,c;

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

    System.out.println(Enter the value of a=);

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

    System.out.println(Enter the value of b=);

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

    System.out.println(Enter the value of c);

    c=integer.parseInt(br.readLina());

    if(a

  • 8/2/2019 ojava

    8/36

    }

    else

    {

    System.out.println(c is smallest);

    }

    System.out.println(NAME: Siddharth Nair);

    System.out.println(Enroll No.: A2305309017);

    }

    }

  • 8/2/2019 ojava

    9/36

    EXPERIMENT NO.-5

    Write a program to show use of copy constructor and the memory allocation

    of reference object.

    class rect

    {

    int l,b;r

    rect();

    {

    l=b=0;

    System.out.println(Case of No Atgument);

    System.out.println(l=l);

    System.out.println(b=b);

    }

    rect(int x)

    {

    l=b=x;

    System.out.pritnln(Case of One Argument);

    System.out.println(l=l);

    System.out.pritnln(b=b);

    }

    rect(int x,int y)

    {

    l=x;

    b=y;

  • 8/2/2019 ojava

    10/36

    System.out.println(Case of Two Argument);

    System.out.println(l=l);

    System.out.println(b=b);

    }

    rect(rect r)

    {

    l=r.l;

    b=r.b;

    System.out.println(Case of Copy Constructor);

    System.out.println(l=l);

    System.out.println(b=b);

    }

    }

    class constructor

    {

    public static void main(String arg[])

    {

    rect r1=new rect();

    rect r2=new rect(5);

    rect r3=new rect(7,9);

    rect r4=new rect(r2);

    System.out.println(NAME: Siddharth Nair);

    System.out.println(Enroll No.: A2305309017);

  • 8/2/2019 ojava

    11/36

    }

    }

  • 8/2/2019 ojava

    12/36

    EXPERIMENT NO.-6

    Write a program to convert number into words.

    import java.io.*;

    class alphabet

    {

    public static void main(String arg[])

    {

    int n;

    String arg1[]=

    ,one,two,three,four,five,six,seven,eight,nine-;

    String arg2[]=

    ,eleven,tweleve,thirteen,fourteen,fifteen,sixteen,seventeen,

    eighteen,nineteen-;

    String arg3[]=,ten,twenty,thirty,fourty,fifty,sixty,seventy,eighty,ninty-;

    String arg4*+= ,hundred-;

    System.out.println(Enter the number:);

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

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

    if(n999)

    System.out.println(WRONG INPUT);

    else

    {

    if(n>99 && n

  • 8/2/2019 ojava

    13/36

    int x=n;

    int d=x/100;

    int r=x%100;

    if(r>10 && r19 && n

  • 8/2/2019 ojava

    14/36

    if(n>10 && n0 && n

  • 8/2/2019 ojava

    15/36

  • 8/2/2019 ojava

    16/36

    EXPERIMENT NO.-7

    Write a program to implement array stack with the help of Abstract class.

    abstract class stack1

    {

    int a[];

    int top;

    final int m=5;

    abstract void push(int x);

    abstract int pop();

    }

    class stack extends stack1

    {

    stack()

    {

    a=new int[m];

    top=-1;

    }

    void push(int x)

    {

    if(top==m-1)

    System.out.println(OVERFLOW);

    else

    a[++top+=x;

  • 8/2/2019 ojava

    17/36

    }

    int pop()

    {

    int n=0;

    if(top==-1)

    System.out.println(UNDERFLOW);

    else

    n=a[top--];

    return(n);

    }

    void display()

    {

    for(int i=0;i

  • 8/2/2019 ojava

    18/36

    s1.push(6);

    System.out.println(s1.pop());

    System.out.println(s1.pop());

    System.out.println(s1.pop());

    System.out.println(s1.pop());

    System.out.println(s1.pop());

    System.out.println(s1.pop());

    System.out.println(NAME: Siddharth Nair);

    System.out.println(Enroll No.: A2305309017);

    }

    }

  • 8/2/2019 ojava

    19/36

    EXPERIMENT NO.-8

    Write a program to implement array queue with the help of Interface.

    interface queue1

    {

    final int m=5;

    void insert(int x);

    int delete();

    }

    class queue implements queue1

    {

    int a[];

    int f,r;

    queue();

    {

    a=new int[m];

    f=-1;

    r=0;

    }

    public void insert(int x)

    {

    if(f==m-1)

    System.out.println(OVERFLOW);

    else

  • 8/2/2019 ojava

    20/36

    a[++f]=x;

    }

    public int delete()

    {

    int t=0;

    if(f==-1)

    System.out.println(UNDERFLOW);

    else

    {

    r++;

    t=a[r];

    if(r>f);

    {

    f=-1;

    r=0;

    }

    }

    return(t);

    }

    public static void main(String arg[])

    {

    queue q1=new queue();

    q1.insert(6);

  • 8/2/2019 ojava

    21/36

    q1.insert(5);

    q1.insert(4);

    q1.insert(3);

    q1.insert(2);

    q1.insert(1);

    System.out.println(q1.delete());

    System.out.println(NAME: Siddharth Nair);

    System.out.println(Enroll no.: A2305309017);

    }

    }

  • 8/2/2019 ojava

    22/36

    EXPERIMENT NO.-9

    Write a program to increment the salary on the basis of Designation:

    MANAGER= 5000; GENERAL MANAGER= 10000; CEO=20,000; WORKER=2000

    class emp1

    {

    String desg;

    int sal,emp_id;

    public emp1(int id, String des, int sa)

    {

    emp_id;

    desg=des;

    sal=sa;

    }

    public int increment(int sal)

    {

    if(desg.equals(MANAGER))

    {

    sal=sal+5000;

    System.out.println(INCREASED SALARY OF MANAGER);

    }

    else

    if(desg.equals(GENERAL MANAGER))

    {

  • 8/2/2019 ojava

    23/36

    sal=sal+10000;

    System.out.pritnln(INCREASED SALARY OF GENERAL MANAGER);

    }

    else

    if(desg.equals(CEO))

    {

    sal=sal+2000;

    }

    return sal;

    }

    public void disp()

    {

    System.out.println(SALARY=sal);

    System.out.println(EMPLOYYES ID=emp_id);

    System.out.println(DESIGNATION=desg);

    }

    }

    class emp

    {

    public static void main(String arg[])

    {

    emp1 e=new emp1(151,GENERAL MANAGER,5245);

    e.disp();

  • 8/2/2019 ojava

    24/36

    e.increment(3000);

    System.out.println(NAME: Siddharth Nair);

    System.out.println(Enroll No.: A2305309017);

    }

    }

  • 8/2/2019 ojava

    25/36

    EXPERIMENT NO.-10

    Create a bank class containing data members: cust_name, acc_type, acc_no,

    bal_amt. Define member functions to assign initial values, deposit theamount, withdraw the amount. After checking, the balance left is greater

    than 1,000.

    class account

    {

    String cust_name;

    String acc_type;

    double bal_amt;

    int acc_no;

    account(String s, String s1, double a, int b)

    {

    cust_name=s;

    acc_type=s1;

    bal_amt=a;

    acc_no=b;

    }

    void deposit(double a,int b)

    {

    if(acc_no==b)

    bal_amt+=a;

    }

    void withdraw(double a)

  • 8/2/2019 ojava

    26/36

    {

    if(bal_amt>1000)

    bal_amt-=a;

    else

    System.out.println(Sorry Process Rejected by Server);

    }

    void display()

    {

    System.out.println(CUSTOMERS NAME: cust_name);

    System.out.println(ACCOUNT TYPE: acc-type);

    System.out.println(NET BALANCE: bal_amt);

    }

    }

    class bank

    {

    public static void main(String arg[])

    {

    account t=new account(Siddharth,saving,4545747.0,548);

    t.display();

    t.deposit(1000,548);

    t.withdraw(5412.0);

    System.out.println(\n\n Customer Information);

    t.display();

  • 8/2/2019 ojava

    27/36

    System.out.println(NAME: Siddharth Nair);

    System.out.println(Enroll No.: A2305309017);

    }

    }

  • 8/2/2019 ojava

    28/36

    Experiment- 11

    Implementing exception without handling.

    class exceptionwdhandle

    {

    public static void main(String s[])

    {

    System.out.println("before exception");

    int x= 10/0;

    System.out.println("After exception");

    }

    }

    Output:

  • 8/2/2019 ojava

    29/36

    Experiment-12

    Implementing exception with handling

    class prga

    {public static void main(String s[])

    {

    System.out.println("Before exception");

    try{

    int x= 10/0;

    }catch(Exception e)

    {

    System.out.println(e);

    int x=0;

    }

    System.out.println("After exception");

    }

    }

    Output:

  • 8/2/2019 ojava

    30/36

    Experiment- 13

    Program to implement a package program.

    package p1;

    class prg13

    {

    public static void main(String s[])

    {

    System.out.println("Package Program");

    }

    }

    Output:

  • 8/2/2019 ojava

    31/36

    Experiment 14

    Implementation of queue using interface

    interface queue

    {

    final int m=5;

    void insert (int x);

    int delete ();

    }

    class intqueue implements queue

    {

    int a[];

    int f, r;

    intqueue()

    {

    a=new int[m];

    f=-1; r=0;

    }

    public void insert(int x)

    {

    if (f==m-1)

    System.out.println("Overflow");

    else

    a[++f]=x;

    }

    public int delete()

  • 8/2/2019 ojava

    32/36

    {

    int y=0;

    if (f==-1 && r==0)

    System.out.println("Underflow");

    else

    {

    y=a[r]; a[r]=0; r++;

    if (r>f)

    {

    f=-1; r=0;

    }

    }

    return (y);

    }

    void display()

    {

    System.out.println("The queue is:");

    for (int i=0; i

  • 8/2/2019 ojava

    33/36

    q1.insert(5);

    q1.insert(13);

    q1.insert(4);

    q1.insert(8);

    q1.insert(1);

    q1.display();

    q1.insert(2);

    q1.delete();

    q1.display();

    q1.delete();

    q1.display();

    System.out.println(Siddharth A2305309017);

    }

    }

    Output:

  • 8/2/2019 ojava

    34/36

    Experiment-15

    Program to implement exception handling using multiple catch blocks.

    class prg30

    {

    public static void main (String s[])

    {

    System.out.println("Before exception");

    try

    {

    int x[]=new int[10];

    // x[11]=15;

    // int a=15/0;

    int a= Integer.parseInt("15I");

    }

    catch (ArithmeticException e)

    {

    System.out.println(e);

    }

    catch (ArrayIndexOutOfBoundsException e)

    {

    System.out.println(e);

    }

    catch (NumberFormatException e)

    {

  • 8/2/2019 ojava

    35/36

    System.out.println(e);

    }

    System.out.println("Siddharth A2305309017");

    }

    }

    Output:

  • 8/2/2019 ojava

    36/36