java rcd final

Upload: theraig58

Post on 10-Mar-2016

7 views

Category:

Documents


0 download

DESCRIPTION

JAVA-RECord

TRANSCRIPT

  • Object Oriented Programming Lab

    Department Of Computer Application. 1

    CPP PROGRAMS

  • Object Oriented Programming Lab

    Department Of Computer Application. 2

    Program No: 1

    SUM OF TIMES

    AIM:

    Write a CPP program to create a class time that has separate integer data members for

    hours, minutes and seconds .There should be member function to read the data members

    display in the format (H:M:S) .And a member function to add two objects of time passed as

    arguments .Write a main program to create three time objects ,add the two and stored the

    result in the third variable.

    ALGORITHM:

    STEP 01 : START

    STEP 02 : DECLARE THE CLASS WITH VARIABLES

    STEP 03 : TO GET THE PARAMETERS HOUR , MINUTE , SECONDS BY

    USING THE FUNCTION READ()

    STEP 04 : DEFINE THE FUNCTION ADD() TWO OBJECT OF TIME

    STEP 05: DECLARE THE CLASS OBJECTS T1 AND T2

    STEP 06 : CALL THE FUNCTION READ() USING T1 AND T2

    STEP 07 : CALCULATE THE VALUE FOR THE OBJECT RESULT BY

    CALLING T1.ADD(T2)

    STEP 08 : DISPLAY THE RESULTANT TIME AFTER ADDITION

    STEP 09 : STOP

  • Object Oriented Programming Lab

    Department Of Computer Application. 3

    PROGRAM:

    #include

    #include

    class time

    {

    int h,m,s;

    public:

    void read()

    {

    couth>>m>>s;

    }

    void add(time t)

    {

    time hms;

    hms.h=h+t.h;

    hms.m=m+t.m;

    hms.s=s+t.s;

    if(hms.m>60)

    {

    hms.h++;

    hms.m=hms.m-60;

    }

    if(hms.s>60)

    {

    hms.m++;

    hms.s=hms.s-60;

    }

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 4

    OUTPUT:

    Enter the time in Hour Minutes and Seconds

    3

    45

    34

    4

    55

    60

    Hour : 8

    Minute : 41

    Seconds :34

  • Object Oriented Programming Lab

    Department Of Computer Application. 5

    Program No: 2

    ADDITION OF RECTANGULAR COORDINATES

    AIM:

    Write a CPP program to create a class which defines the Rectangular coordinates.

    Write member function to read, display and operator overloaded function to add two

    rectangular points.

    ALGORITHM:

    STEP 01 : START

    STEP 02 : CREATE A CLASS RECTANGLE WITH DATA MEMBERS X ,Y.

    STEP 03 : CREATE TWO OBJECTS OF THE CLASS RECTANGLE R1,R2.

    STEP 04 : READ VALUES FOR THE DATA MEMBERS OF THE OBJECTS.

    STEP 05 : ADD THE VALUES

    R1.X=X+R2.X

    R1.Y=Y+R2.Y

    STEP 06 :.DISPLAY THE RESULT.

    STEP 07 : STOP

    PROGRAM:

    #include

    #include

    #include

    class Rectangle

    {

    float x,y;

    public:

  • Object Oriented Programming Lab

    Department Of Computer Application. 6

    Rectangle()

    {

    x=0;

    y=0;

    }

    Rectangle(float m,float n)

    {

    x=m;

    y=n;

    }

    void read();

    void display();

    Rectangle operator +(Rectangle);

    };

    void Rectangle::read()

    {

    coutx>>y;

    }

    void Rectangle::display()

    {

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 7

    {

    Rectangle r1;

    r1.x=x+r.x;

    r1.y=y+r.y;

    return(r1);

    }

    void main()

    {

    Rectangle r2,r3,r4;

    clrscr();

    r2.read();

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 8

    OUTPUT:

    Enter the values of x and y

    10

    5

    First coordinates

    x:10 y:5

    Enter the values of x and y

    5

    10

    Second coordinates

    x:5 y:10

    Coordinate after addition

    x:15 y:15

  • Object Oriented Programming Lab

    Department Of Computer Application. 9

    Program No: 3

    STRING CONCATENATION USING OPERATOR OVERLOADING

    AIM:

    Write a CPP program using operator overloading to concatenate two string using

    arithmetic addition operator.

    ALGORITHM:

    STEP 01 : START

    STEP 02 : DECLARE THE CLASS

    STEP 03 : DECLARE VARIABLES AND ITS MEMBER FUNCTIONS READ()

    AND DISPLAY()

    STEP 04 : USING THE FUNCTION READ() TO GET THE TWO STRINGS

    STEP 05 : DEFINE THE FUNCTION OPERATOR + () TO

    ADD/CONCATINATE TWO STRINGS

    STEP 6 : DEFINE DISPLAY FUNCTION

    STEP 07 : DECLARE THE CLASS OBJECTS S1,S2,S3.S3 TO STORE THE RESULT.

    STEP 08 : CALL THE FUNCTION READ() USING OBJECT S1 AND S2.

    STEP 09 : CALCULATE THE VALUE FOR THE OBJECT RESULT BY

    CALLING THE FUNCTION

    OPERATOR +()

    STEP 10 : CALL THE DISPLAY FUNCTION USING THIRD OBJECT S3

    STEP 11 : RETURN THE VALUES

    STEP 12 : STOP

  • Object Oriented Programming Lab

    Department Of Computer Application. 10

    PROGRAM:

    #include

    #include

    #include

    class strings_concat

    {

    public:

    char st[10];

    void read();

    void display();

    strings_concat operator +(strings s2);

    };

    void strings_concat::read()

    {

    coutst;

    }

    strings_concat string_concat s::operator+(strings_concat s2)

    {

    strings_concat s;

    strcpy(s.st,st);

    strcat(s.st,s2.st);

    return(s);

    }

    void strings_concat::display()

    {

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 11

    OUTPUT:

    Enter two strings

    Hai

    Welcome

    The concatenated string is

    HaiWelcome

  • Object Oriented Programming Lab

    Department Of Computer Application. 12

    Program No:4

    MARKLIST OF 'N' STUDENTS

    AIM:

    Write a CPP program to create a mark list of 'n' students. The data members are roll

    no, name, course, and semester. Define virtual function read and display in student class

    .Derive class mark from student class data members an array to store marks of five subjects

    and total marks. Write a main program to create an array of pointers to objects of class mark.

    ALGORITHM:

    STEP 01: CREATE A CLASS NAMED STUDENT WITH DATA MEMBERS

    NAME,COURSE,SEM AND VIRTUAL METHODS READ() AND DISPLAY()

    TO INPUT AND OUTPUT VALUES.

    STEP 02: INHERIT CLASS MARKS FROM STUDENT WITH DATA MEMBERS

    IN AND EX FOR STORING INTERNAL AND EXTERNAL MARKS.

    STEP 03: READ STUDENT DETAILS AND COMPUTE TOTAL MARKS.

    STEP 04: IN MAIN, CREATE REFERENCE OBJECT *S[10] FOR STUDENT AND

    ARRAY

    OF OBJECT M[10] FOR MARKS.

    STEP 05: FOR I FROM 0 TO LIMIT

    S[I]=&M[I];

    S[I]->READ();

    STEP 06: FOR I FROM 0 TO LIMIT

    S[I]->DISPLAY();

    STEP 7: STOP.

  • Object Oriented Programming Lab

    Department Of Computer Application. 13

    PROGRAM:

    #include

    #include

    #include

    class student

    {

    public:char name[25];

    char course[10];

    int sem;

    virtual void read()

    {

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 14

    }

    };

    class marks:public student

    {

    public:int in[5];

    int ex[5];

    int i,j;

    int intot,extot;

    void read();

    void display();

    marks()

    {

    intot=0;

    extot=0;

    }

    };

    void marks::read()

    {

    student::read();

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 15

    intot=intot+in[i];

    }

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 16

    for(int i=0;iread();

    }

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 17

    External marks= 46

    48

    49

    50

    47

    -----------------------Student Marklist------------------------------------

    Name= nimmi

    Course= Mtech

    Semester= 3

    Internal marks=45

    46

    47

    48

    49

    External marks= 50

    49

    48

    47

    46

    Name Course Semester Tot_internal Tot_external

    Sree MCA 3 236 240

    Nimmi Mtech 3 235 240

  • Object Oriented Programming Lab

    Department Of Computer Application. 18

    Program No: 5

    COMPLEX NUMBERS USING OPERATOR OVERLOADING

    AIM:

    Write a CPP program to add, sub and multiply two complex numbers using operator

    overloading.

    ALGORITHM:

    STEP 01: CREATE A CLASS COMPLEX WITH DATA MEMBERS REAL

    AND IMAG.

    STEP 02: DECLARE APPROPRIATE CONSTRUCTORS AND METHOD

    NAMED READ() AND DISPLAY() TO READ AND PRINT VALUES.

    STEP 03: DECLARE OPERATOR OVERLOADED FUNCTIONS

    OPERATOR+(COMPLEX C), OPERATOR-(COMPLEX C),

    OPERATOR*(COMPLEX C).

    STEP 04: IN THE FUNCTION OPERATOR+(COMPLEX C)

    C1.REAL=REAL+C.REAL

    C1.IMAG=IMAG+C.IMAG

    RETURN C1.

    STEP 05: IN THE FUNCTION OPERATOR-(COMPLEX C)

    C1.REAL=REAL-C.REAL

    C1.IMAG=IMAG-C.IMAG

    RETURN C1.

    STEP 06: THE FUNCTION OPERATOR+(COMPLEX C)

    C1.REAL=(REAL*C.REAL)-(IMAG*C.IMAG)

    C1.IMAG=(REAL*C.IMAG)+(IMAG*C.REAL)

  • Object Oriented Programming Lab

    Department Of Computer Application. 19

    RETURN C1.

    STEP 07: IN MAIN, CREATE THE OBJECTS C1,C2(2,3),C3

    STEP 08: ENTER C1.

    STEP 09: COMPUTE C3=C1+C2 AND DISPLAY THE RESULT.

    STEP 10: COMPUTE C3=C1-C2 AND DISPLAY THE RESULT.

    STEP 11: COMPUTE C3=C1*C2 AND DISPLAY THE RESULT.

    STEP 12: STOP.

    PROGRAM:

    #include

    #include

    class complex

    {

    int r,i;

    public:

    void read();

    void display();

    complex operator +(complex);

    complex operator -(complex);

    complex operator *(complex);

    complex()

    {

    r=0;

    i=0;

    }

    };

  • Object Oriented Programming Lab

    Department Of Computer Application. 20

    void complex::read()

    {

    coutr>>i;

    }

    void complex::display()

    {

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 21

    {

    complex c1;

    c1.r=(r*c.r)-(i*c.i);

    c1.i=(r*c.i)+(i*c.r);

    return(c1);

    }

    void main()

    {

    complex cc,cc1,cc2;

    clrscr();

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 22

    OUTPUT:

    Enter the 1st complex no:

    Enter the value

    2 5

    Enter the 2nd

    complex no:

    Enter the value

    1 3

    Addition of two complex no:s:

    The result is : 3+i8

    Subtraction of two complex no:s:

    The result is : 1+i2

    Multiplication of two complex no:s:

    The result is : -13+i11

  • Object Oriented Programming Lab

    Department Of Computer Application. 23

    Program No: 6

    MATRIX OPERATION USING INSERTION AND

    EXTRACTION OPERATOR

    AIM:

    Write a CPP program to add, subtract and multiply two matrices using insertion and

    extraction operator.

    ALGORITHM:

    STEP 01: CREATE A CLASS MATRIX WITH DATA MEMBERS A[10],

    ROW,COL.

    STEP 02: DECLARE FUNCTIONS ISTREAM&OPERATOR>>() AND

    OSTREAM&OPERATOR(ISTREAM &IN,MATRIX &M),

    STEP 05: READ THE ORDER OF MATRIX M.ROW, M.COL.

    STEP 06: FOR I FROM 0 TO M.ROW

    FOR J FROM 0 TO M.COL

    IN>>M.A[I][J].

    RETURN IN

    STEP 07: IN FUNCTION

    OSTREAM&OPERATOR

  • Object Oriented Programming Lab

    Department Of Computer Application. 24

    STEP 09:IN FUNCTION OPERATOR+(MATRIX M)

    IF(ROW==M.ROW&&COL==M.COL)

    THEN

    FOR I FROM 0 TO M.ROW

    FOR J FROM 0 TO M.COL

    M1.A[I][J]=A[I][J]+M.A[I][J]

    RETURN M1

    STEP10:IN FUNCTION OPERATOR-(MATRIX M)

    IF(ROW==M.ROW&&COL==M.COL)

    THEN

    FOR I FROM 0 TO M.ROW

    FOR J FROM 0 TO M.COL

    M1.A[I][J]=A[I][J]-M.A[I][J]

    RETURN M1

    STEP11: IN FUNCTION OPERATOR*(MATRIX M)

    IF(COL==M.ROW) THEN

    FOR I FROM 0 TO M.ROW

    FOR J FROM 0 TO M.COL

    M1.A[I][J]=0

    FOR K FROM 0 TO M.ROW

    M1.A[I][J]=M1.A[I][J]+(A[I][K]*C.A[K][J])

    RETURN M1

    STEP12: IN MAIN, CREATE OBJECTS M1,M2,M3(3,3) OF CLASS.

    STEP13: ENTER M1 THROUGH KEYBOARD.

    STEP14: COMPUTE M1=M2+M3 AND DISPLAY THE SUM.

  • Object Oriented Programming Lab

    Department Of Computer Application. 25

    STEP15: COMPUTE M1=M2-M3 AND DISPLAY THE DIFFERENCE.

    STEP15: COMPUTE M1=M2*M3 AND DISPLAY THE RESULT.

    STEP16: STOP.

    PROGRAM:

    #include

    #include

    class matrix

    {

    int r,c,a[10][10];

    public:

    matrix()

    {

    r=0;

    c=0;

    }

    friend ostream & operator > (istream &in,matrix &m);

    matrix operator+(matrix);

    matrix operator-(matrix);

    matrix operator*(matrix);

    };

    ostream & operator

  • Object Oriented Programming Lab

    Department Of Computer Application. 26

    int i,j;

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 27

    }

    return in;

    }

    matrix matrix::operator+(matrix m)

    {

    matrix m1;

    m1.r=r;

    m1.c=c;

    for(int i=0;i

  • Object Oriented Programming Lab

    Department Of Computer Application. 28

    {

    m1.a[i][j]=a[i][j]-m.a[i][j];

    }

    }

    return m1;

    }

    matrix matrix::operator*(matrix m)

    {

    matrix m1;

    m1.r=r;

    m1.c=c;

    if(c!=m.r)

    {

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 29

    }

    }

    }

    }

    return m1;

    }

    void main()

    {

    matrix m1,m2,m3;

    cin>>m2;

    cin>>m3;

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 30

    OUTPUT:

    Enter the row and col:

    2 2

    Enter the elements

    1 2

    3 4

    Enter the row and col:

    2 2

    Enter the elements

    1 2

    3 4

    Addition

    Elements are:

    2 4

    6 8

    Subtraction

    Elements are:

    0 0

    0 0

    Multiplication

    Elements are:

    7 10

    15 22

  • Object Oriented Programming Lab

    Department Of Computer Application. 31

    Program No: 7

    RECTANGULAR TO POLAR COORDINATE CONVERSION

    AIM:

    Write a program to convert rectangular co-ordinate to polar co-ordinate and write a

    main function to test the program.

    ALGORITHM:

    STEP 01 : START

    STEP 02 : DEFINE A CLASS RECTANGLE

    DATA MEMBERS: X AND Y

    METHODS : READ() , DISPLAY() ,GETX(),GETY()

    CONSTRUCTORS : DEFAULT AND PARAMETERISED CONSTRUCTOR

    ALSO DEFINED

    STEP 03 : GETX() RETURNS X VALUE AND GETY() RETURN Y VALUE.

    STEP 04 : DEFINE CLASS POLAR WITH r REPRESENT RADIUS AND a

    REPRESENT ANGLE

    STEP 05 : RADIUS = SQRT(X2+Y2)

    ANGLE = INVERSE TAN(Y/X)

    STEP 06 : IN MAIN FUNCTION CREATE OBJECTS OF RECTANGLE AND ASSIGN

    TO POLAR OBJECTS

    STEP 07 : EXECUTE STATEMENTS

    STEP 08 : STOP

  • Object Oriented Programming Lab

    Department Of Computer Application. 32

    PROGRAM:

    #include

    #include

    #include

    class Rectangle

    {

    float x,y;

    public:

    Rectangle()

    {

    x=0.0;

    y=0.0;

    }

    Rectangle(float k,float j)

    {

    x=k;

    y=j;

    }

    void read()

    {

    coutx;

    cin>>y;

    }

  • Object Oriented Programming Lab

    Department Of Computer Application. 33

    void display()

    {

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 34

    Polar(Rectangle r1)

    {

    r=sqrt((r1.getx()*r1.getx()) +(r1.gety()*r1.gety()));

    a=atan(r1.gety()/r1.getx());

    }

    };

    void main()

    {

    clrscr();

    Polar P1;

    Rectangle R2(5.0,4.0);

    R2.display();

    P1=R2;

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 35

    Program No :8

    FRIEND FUNCTION

    AIM:

    Write a CPP Program to Implement Friend Function.

    ALGORITHM:

    STEP 01: START

    STEP 02: DECLARE TWO CLASSES WITH ONE VARIABLE EACH AS DATA

    MEMBERS AND DEFINE CONSTRUCTOR METHOD FOR EACH.

    STEP 03:DECLARE A FRIEND FUNCTION IN EACH CLASS WITH

    PARAMETERS AS REFERENCES OF OBJECTS OF THESE CLASSES.

    STEP 04: DEFINE THE OBJECTS OF CLASS AS OB1, OB2

    STEP 05: READ TWO VALUES FOR TWO DATA MEMBERS IN THE

    DIFFERENT CLASSES.

    STEP 06: CALL THE SWAP FUNCTION WHICH IS DECLARED OUTSIDE OF

    THESE CLASSES AND TAKES TWO PARAMETERS WHICH ARE

    THE REFERENCES OF THE OBJECTS OF THESE CLASSES.

    STEP 07: PERFORM THE SWAP OPERATION USING A TEMP VARIABLE.

    STEP 08: DISPLAY THE VALUE OF THE DATA MEMBERS WHICH ARE

    SWAPPED USING DISPLAY FUNCTIONS INSIDE EACH CLASSES

    STEP 09: STOP

  • Object Oriented Programming Lab

    Department Of Computer Application. 36

    PROGRAM:

    #include

    #include

    class B;

    class A

    {

    int a;

    public:

    A()

    {

    a=0;

    }

    void read();

    void display();

    friend void swap(A&,B&);

    };

    void A::read()

    {

    couta;

    }

    void A::display()

    {

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 37

    {

    int a;

    public:

    B()

    {

    a=0;

    }

    void read();

    void display();

    friend void swap(A&,B&);

    };

    void B::read()

    {

    couta;

    }

    void B::display()

    {

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 38

    void main()

    {

    A a1;B b2;

    clrscr();

    a1.read();

    b2.read();

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 39

    OUTPUT:

    Enter a number

    15

    Enter a number

    25

    Before swapping

    a1=15

    b2=25

    After swapping

    a1=25

    b2=15

  • Object Oriented Programming Lab

    Department Of Computer Application. 40

    Program No: 9

    STUDENT MARKLIST USING INHERITANCE

    AIM: Write a CPP program to create a student class with data members student name, age,

    sex. Create another class course with data members course name. Create a class branch

    derived from student and course with data members semester, roll no, marks of five subjects

    and total marks. Write constructor in all the classes and appropriate member function to read

    and display the required data members.

    ALGORITHM:

    STEP 01: START

    STEP 02: CREATE A CLASS STUDENT WITH FIELDS NAME AND SEX

    STEP 03: DEFINE READ AND DISPLAY METHOD IN STUDENT CLASS

    STEP 04: CREATE A CLASS COURSE WITH FIELDS COURSE NAME

    STEP 05: DEFINE READ AND DISPLAY METHOD IN COURSE CLASS

    STEP 06: DERIVE A CLASS BRANCH FROM STUDENT AND COURSE WITH

    ADDITIONAL FIELDS RLNO,SEM,5 MARKS AND TOTAL

    STEP 07: REDEFINE READ AND DISPLAY METHODS IN BRANCH CLASS ALSO

    FIND TPTAL MARKS

    STEP 08: DEFINE MAIN METHOD

    STEP 09: CREATE OBJECTS OF BRANCH CLASS AND CALL THE METHODS

    USING OBJECTS

    STEP 10: STOP

  • Object Oriented Programming Lab

    Department Of Computer Application. 41

    PROGRAM:

    #include

    #include

    #include

    class Student

    {

    protected:

    char name[20],sex[1];

    public:

    Student()

    {

    }

    Student(char *a,char *c)

    {

    strcpy(name,a);

    strcpy(sex,c);

    }

    void read()

    {

    coutname;

    coutsex;

    }

    void display()

    {

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 42

    {

    coutcrs;

    }

    void display()

    {

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 43

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 44

    Program No:10

    BANK ACCOUNT

    AIM:

    Write a CPP program to create a base class account with data members accno, name,

    dob derive a class savings with type of account as data members and member function to

    read, deposit ,withdrawal and display .Derive another class called as current having type of

    account as data member function to read deposit withdrawal and display of account. Write a

    main program with options to create an account of type either savings or current account with

    options to deposit, withdraw and display the details of the account holder. Program should be

    menu driven with n customers with option to create any type of account.

    ALGORITHM:

    STEP 01: CREATE A CLASS NAMED ACCOUNT WITH DATA MEMBERS

    ACCNO,NAME,AGE,BAL.

    STEP 02: INHERIT A CLASS SAVINGS FROM ACCOUNT WITH METHODS READ(),

    DISPLAY(), DEPOSIT(), WITHDRAWAL().

    STEP 03: IN FUNCTION DEPOSIT(),

    READ AMOUNT

    BAL=BAL+AMOUNT

    STEP 04: IN FUNCTION WITHDRAWAL(),

    READ AMOUNT

    IF( BAL==0) THEN

    PRINT NO BALANCE ELSE

    BAL=BAL-AMOUNT

    STEP 05: INHERIT A CLASS CURRENT FROM ACCOUNT WITH METHODS READ(),

    DISPLAY(), DEPOSIT(), WITHDRAWAL().

    STEP 06: IN FUNCTION DEPOSIT(),

    READ AMOUNT

  • Object Oriented Programming Lab

    Department Of Computer Application. 45

    BAL=BAL+AMOUNT

    STEP 07: IN FUNCTION WITHDRAWAL(),

    READ AMOUNT

    IF( BAL==0) THEN

    PRINT NO BALANCE

    ELSE

    BAL=BAL-AMOUNT

    STEP 08: IN MAIN,

    SWITCH(CH) DO

    IF CASE 1 THEN

    SWITCH(C) DO

    IF CASE 1 THEN CREATE

    IF CASE 2 THEN DEPOSIT

    IF CASE 3 THEN WITHDRAWAL

    IF CASE 4 THEN DISPLAY

    IF CASE 5 THEN EXIT

    IF CASE 2 THEN

    SWITCH(C) DO

    IF CASE 1 THEN CREATE

    IF CASE 2 THEN DEPOSIT

    IF CASE 3 THEN WITHDRAWAL

    IF CASE 4 THEN DISPLAY

    IF CASE 5 THEN EXIT

    IF CASE 3 THEN EXIT

    STEP 09: STOP

  • Object Oriented Programming Lab

    Department Of Computer Application. 46

    PROGRAM:

    #include

    #include

    #include

    class account

    {

    protected:int accno;

    char name[20];

    int age;

    int bal;

    };

    class saving:public account

    {

    char type;

    public: void create()

    {

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 47

    coutbal;

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 48

    if(bal==0)

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 49

    }

    void display()

    {

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 50

    {

    bal=bal-amt;

    }

    }

    };

    void main()

    {

    int ch,c;

    clrscr();

    saving S;

    current C;

    do

    {

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 51

    cout

  • Object Oriented Programming Lab

    Department Of Computer Application. 52

    coutc;

    switch(c)

    {

    case 1:C.create();

    break;

    case 2:C.deposit();

    break;

    case 3:C.withdrawal();

    break;

    case 4:C.display();

    break;

    //case 5:exit(0);

    }

    }while(c

  • Object Oriented Programming Lab

    Department Of Computer Application. 53

    OUTPUT:

    1. Savings

    2. Current

    3. Exit

    Enter the type of account to create 1

    1. Create

    2. Deposit

    3. Withdrawal

    4. Display

    Enter your choice 1

    Enter details of customer

    Account no= 101\

    Name= Devan

    Age= 20

    Balance Amount= 2000

    Account created.

    1. Create

    2. Deposit

    3. Withdrawal

    4. Display

    Enter your choice 2

    Enter the amount to deposit 3000

    1. Create

    2. Deposit

    3. Withdrawal

  • Object Oriented Programming Lab

    Department Of Computer Application. 54

    4. Display

    Enter your choice 3

    Enter the amount to withdraw 1000

    1. Create

    2. Deposit

    3. Withdrawal

    4. Display

    Enter your choice 4

    ------------------Savings Account Details--------------------

    Account No= 101

    Name = devan

    Age= 20

    Type= Savings

    Balance Amount= 4000

    1. Create

    2. Deposit

    3. Withdrawal

    4. Display

    Enter your choice 5

    1. Savings

    2. Current

    3. Exit

    Enter the type of account to create 3

  • Object Oriented Programming Lab

    Department Of Computer Application. 55

    JAVA PROGRAMS

  • Object Oriented Programming Lab

    Department Of Computer Application. 56

    Program No: 1

    SUM OF DIGITS

    AIM:

    Write a java program to find the sum of digits.

    ALGORITHM:

    STEP 01 : START

    STEP 02 : DECLARE NUM,TEMP AND SUM

    STEP 03 : READ NUM

    STEP 04 : SUM=0 AND TEMP=NUM

    STEP 05 : IF NUM>0 THEN

    STEP 06 : WHILE NUM > 0

    STEP 07 : (SUM=SUM +NUM)%10

    NUM=NUM/10

    STEP 08 : DISPLAY SUM

    STEP 09 : ELSE DISPLAY INVALID NUMBER

    STEP 10 : STOP

  • Object Oriented Programming Lab

    Department Of Computer Application. 57

    PROGRAM:

    import java.util.Scanner;

    public class Sum_of_Digits {

    public static void main(String[] Args)

    {

    sum_didgt obj =new sum_didgt(121);

    obj.find_sum();

    obj.read();

    obj.find_sum();

    }

    }

    class sum_didgt{

    Scanner scan =new Scanner(System.in);

    int num;

    sum_didgt(){

    num=0;

    }

    sum_didgt(int x){

    num=x;

    }

    void read(){

    System.out.println("Enter a the number");

    num=scan.nextInt();

    }

    void find_sum(){

    int sum=0,temp=num;

    if(num>0){

    while(num>0){

    sum=sum+num%10;

  • Object Oriented Programming Lab

    Department Of Computer Application. 58

    num=num/10;

    }

    System.out.println("Sum of digits of number "+temp +" ="+sum);

    }

    else System.out.println("Enter a valid number");

    }

    }

    OUTPUT:

    Sum of digits of number 121 =4

    Enter a the number

    159

    Sum of digits of number 159 =15

  • Object Oriented Programming Lab

    Department Of Computer Application. 59

    Program No: 2

    PATTERN PRINTING

    AIM:

    Write a java program to print pattern.

    ALGORITHM:

    STEP 01 : START

    STEP 02 : DECLARE ROW, C, N, TEMP

    STEP 03 : READ N

    STEP 04 : TEMP=N

    STEP 05 : FOR ROW = 1 TO ROW

  • Object Oriented Programming Lab

    Department Of Computer Application. 60

    PROGRAM:

    import java.util.Scanner;

    public class Pattern {

    public static void main(String[] Args) {

    Printattern obj=new Printattern(10);

    obj.print();

    obj.read();

    obj.print();

    }

    }

    class Printattern{

    int row, c, n, temp;

    Scanner scan =new Scanner(System.in);

    Printattern(){

    n=10;

    }

    Printattern(int x){

    n=x;

    }

    void read(){

    System.out.println("Enter the number of rows in pyramid of stars you wish to see");

    n=scan.nextInt();

    }

    void print(){

    temp = n;

    for ( row = 1 ; row

  • Object Oriented Programming Lab

    Department Of Computer Application. 61

    OUTPUT:

    *

    ***

    *****

    *******

    *********

    ***********

    *************

    ***************

    *****************

    Enter the number of rows in pyramid of stars you wish to see

    12

    *

    ***

    *****

    *******

    *********

    ***********

    *************

    ***************

    *****************

    *******************

    *********************

    ***********************

  • Object Oriented Programming Lab

    Department Of Computer Application. 62

    Program No: 3

    SUM OF ODD AND EVEN NUMBER

    AIM:

    Write a java program to print sum of odd and even numbers.

    ALGORITHM:

    STEP 01 : START.

    STEP 02 : INITIALIZE LIMIT AND SUM VALUE STORING VARIABLES

    STEP 03 : PRINT , ENTER THE NUMBER OF ELEMENTS IN ARRAY:

    STEP 04 : READ LIMIT

    STEP 05 : INITIALIZE ARRAY WITH LIMIT OF SIZE

    STEP 06 : PRINT , THE ELEMENTS OF THE ARRAY:

    STEP 07 : READ ELEMENTS

    STEP 08 : CHECK EACH ELEMENT ODD OR EVEN BY TAKING MODE WITH 2 ,

    STEP 09 : IF LOOP USING CORRESPONDING VARIABLE ADD THE ELEMENTS,

    STEP 10 : PRINT, SUM OF EVEN AND ODD NUMBERS SEPARATE.

    PROGRAM:

    import java.util.Scanner;

    public class Sum_Odd_Even

    {

    public static void main(String[] args)

    { int n, sumE = 0, sumO = 0;

    Scanner s = new Scanner(System.in);

    System.out.print("Enter the number of elements in array:");

    n = s.nextInt();

    int[] a = new int[n];

    System.out.println("Enter the elements of the array:");

  • Object Oriented Programming Lab

    Department Of Computer Application. 63

    for(int i = 0; i < n; i++)

    {

    a[i] = s.nextInt();

    }

    for(int i = 0; i < n; i++)

    {

    if(a[i] % 2 == 0)

    {

    sumE = sumE + a[i];

    }

    else

    {

    sumO = sumO + a[i];

    }

    }

    System.out.println("Sum of Even Numbers:"+sumE);

    System.out.println("Sum of Odd Numbers:"+sumO);

    }

    }

    OUTPUT:

    Enter the number of elements in array:6

    Enter the elements of the array:

    2

    5

    7

    9

    1

    4

    Sum of Even Numbers:6

    Sum of Odd Numbers:22

  • Object Oriented Programming Lab

    Department Of Computer Application. 64

    Program No: 4

    SEARCHING A NUMBER.

    AIM:

    Write a java program to search a number in a given list.

    ALGORITHM:

    STEP 01: START

    STEP 02: DECLARE KEY ARRAY,SEARCH,IN,C

    STEP 03: ASSIGN VALUE TO ARRAY

    STEP 04: PRINT THE LIMIT

    ACCEPT KEY.

    STEP 05: ASSIGN I WITH L

    FOR EACH I IN 1 TO ARRAY LENGTH LOOP

    STEP 06: IF ARRA[I]=KEY

    THEN ASSIGN SEARCH WITH TRUE

    END IF

    END LOOP

    STEP07: IF SEARCH=TRUE

    THENPRINT PRESENT AT LOCATION

    ELSEPRINT NOT PRESENT IN ARRAY

    END IF

    STEP 08: STOP

  • Object Oriented Programming Lab

    Department Of Computer Application. 65

    PROGRAM:

    import java.util.Scanner;

    class j

    {

    public static void main(String args[])

    {

    int c, n, search, array[];

    Scanner in = new Scanner(System.in);

    System.out.println("Enter number of elements");

    n = in.nextInt();

    array = new int[n];

    System.out.println("Enter " + n + " integers");

    for (c = 0; c < n; c++)

    array[c] = in.nextInt();

    System.out.println("Enter value to find");

    search = in.nextInt();

    for (c = 0; c < n; c++)

    {

    if (array[c] == search)

    {

    System.out.println(search + " is present at location " + (c + 1) + ".");

    break;

    }

    }

    if (c == n)

    System.out.println(search + " is not present in array.");

    }

    }

    OUTPUT:

    Enter number of elements

    5

    Enter 5 integers

    3

    9

    2

    7

    1

    Enter value to find

    9

    9 is present at location 2.

  • Object Oriented Programming Lab

    Department Of Computer Application. 66

    Program No: 5

    STACK OPERATIONS

    AIM

    Write a program to implement Stack operations using Array.

    ALGORITHM:

    STEP 01: START.

    STEP 02: TOP=-1.

    STEP 03: PUSH OPERATION.

    IF(TOP==SIZE-1).

    DISPLAY OVERFLOW.

    RETURN.

    STEP 04: TOP=TOP-1.

    STEP 05: STACK[TOP]=ITEM.

    STEP 06: POP OPERATION.

    IF(TOP==-1).

    DISPLAY UNDERFLOW.

    RETURN.

    STEP 07: DATA=STACK[TOP].

    STEP 08: TOP=TOP-1.

    STEP 09: DISPLAY OPERATION.

    FOR I=TOP TO 0.

    DISPLAY STACK[I] DECREMENT I.

    STEP 10: STOP.

  • Object Oriented Programming Lab

    Department Of Computer Application. 67

    PROGRAM:

    import java.util.*;

    import java.io.*;

    class Stack

    {

    int n,top=-1,item;

    int stack[]=new int[10];

    Scanner s=new Scanner(System.in);

    System.out.println(Enter the Size); n=s.nextInt();

    void push( )

    {

    if(top==n-1)

    {

    System.out.println(STACK OVERFLOW!!!!); }

    Else

    {

    System.out.println(Enter an Item); item=s.nextInt();

    top++;

    stack[top]=item;

    System.out.println(( +stack[top] inserted); }

    }

    void pop( )

    {

    if(top==-1)

    {

    System.out.println(STACK UNDERFLOW!!); }

    else

    {

    item=stack[top];

    top--;

    }

    void display( )

    {

    if(top==-1)

    {

    System.out.println(EMPTY STACK!!!); }

    else

    {

    for(i=top;i

  • Object Oriented Programming Lab

    Department Of Computer Application. 68

    {

    System.out.println(The Elements are +stack[top]); }

    }

    class Stackmain

    {

    public static void main(String args[ ])

    {

    Scanner s1=new Scanner(System.in);

    Stack a=new Stack( );

    int ch;

    do

    {

    System.out.println(STACK OPERATIONS); System.out.println(MENU); System.out.println(\n1:PUSH\n2:POP\n3:DISPLAY\n4:EXIT); System.out.println(Enter the Choice); ch=s1.nextInt( );

    switch(ch)

    {

    case 1:

    System.out.println(PUSH); a.push( );

    break;

    case 2:

    System.out.println(POP); a.pop( );

    break;

    case 3:

    System.out.println(Display); a.display( );

    break;

    case 4:

    break;

    default:

    System.out.println(Enter a valid choice!!!); }

    }while(ch!=4);

    }

  • Object Oriented Programming Lab

    Department Of Computer Application. 69

    OUTPUT:

    STACK OPERATIONS

    MENU

    ---------

    1.PUSH

    2.POP

    3.DISPLAY

    4.EXIT

    Enter your choice

    1

    Enter an Item: 10

    10 Inserted

    STACK OPERATIONS

    MENU

    ---------

    1.PUSH

    2.POP

    3.DISPLAY

    4.EXIT

    Enter your choice

    1

    Enter an Item: 20

    20 Inserted

    STACK OPERATIONS

    MENU

    ---------

    1.PUSH

    2.POP

    3.DISPLAY

    4.EXIT

    Enter your choice

    1

    Enter an Item: 30

    30 Inserted

    STACK OPERATIONS

    MENU

    ---------

    1.PUSH

    2.POP

    3.DISPLAY

    4.EXIT

  • Object Oriented Programming Lab

    Department Of Computer Application. 70

    Enter your choice

    3

    STACK ELEMENTS

    30

    20

    10

    STACK OPERATIONS

    MENU

    ----------

    1.PUSH

    2.POP

    3.DISPLAY

    4.EXIT

    Enter your choice

    2

    30 deleted..

    STACK OPERATIONS

    MENU

    ---------

    1.PUSH

    2.POP

    3.DISPLAY

    4.EXIT

    Enter your choice

    3

    STACK ELEMENTS

    20

    10

    STACK OPERATIONS

    MENU

    ---------

    1.PUSH

    2.POP

    3.DISPLAY

    4.EXIT

    Enter your choice

    2

    20 deleted..

  • Object Oriented Programming Lab

    Department Of Computer Application. 71

    STACK OPERATIONS

    MENU

    ---------

    1.PUSH

    2.POP

    3.DISPLAY

    4.EXIT

    Enter your choice

    2

    10 deleted..

    STACK OPERATIONS

    MENU

    ---------

    1.PUSH

    2.POP

    3.DISPLAY

    4.EXIT

    Enter your choice

    3

    EMPTY STACK!!!

    STACK OPERATIONS

    MENU

    ---------

    1.PUSH

    2.POP

    3.DISPLAY

    4.EXIT

    Enter your choice

    4

  • Object Oriented Programming Lab

    Department Of Computer Application. 72

    Program No: 6

    NUMBER SORTING

    AIM:

    Program to sort the given numbers in ascending order

    ALGORITHM:

    STEP 01.: START

    STEP 02 : DECLARE AN ARRAY TO STORE THE GIVEN ELEMENTS

    STEP 03 : COMPARE EACH ELEMENT WITH ONE ANOTHER IN (N-1) TIMES

    STEP 04 : PUT THE ELEMENT IN THE RIGHT PLACE AFTER COMPARISON

    STEP 05 :DISPLAY THE SORTED ELEMENTS

    STEP 06:.STOP

    PROGRAM

    package aravind;

    import java.util.*;

    public class sort {

    public static void main(String args[])

    {

    Scanner s = new Scanner(System.in);

    System.out.println("Enter the no. of elements");

    int n = s.nextInt();

    int arr[]=new int[n];

    System.out.println("Enter the elements");

    for(int i=0;i

  • Object Oriented Programming Lab

    Department Of Computer Application. 73

    {

    for(int j=0;jarr[j+1]){

    int temp=arr[j];

    arr[j]=arr[j+1];

    arr[j+1]=temp;

    }

    }

    }System.out.println("\nsorted array");

    System.out.println("\n");

    for(int i=0;i

  • Object Oriented Programming Lab

    Department Of Computer Application. 74

    Program No : 7

    STRING SORTING

    AIM:

    Write a Java program to sort n Strings.

    ALGORITHM:

    STEP 01: START

    STEP 02: CREATE CLASS, STRSORT

    STEP 03: DEFINE MAIN CLASS

    STEP 04: DECLARE STRINGS ST,STR[]

    STEP 05: READ THE NUMBER OF STRINGS, N

    STEP 06: READ THE STRING IN ARRAY STR[]

    STEP 07: REPEAT THE STEP 8 TO 10 UNTIL ISTR[J+1] THEN INTERCHANGE THEM.

    ST=STR[J]

    STR[J]=STR[J+1]

    STR[J+1]=ST

    STEP 11: PRINT THE SORTED ARRAY STR[]

    STEP 12: STOP

    PROGRAM:

    import java.io.*;

    import java.util.*;

    public class Stringsort {

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

  • Object Oriented Programming Lab

    Department Of Computer Application. 75

    {

    String str[],st;

    str=new String[20];

    Scanner scan=new Scanner(System.in);

    int i,j,n;

    System.out.println("NAME SORTING");

    System.out.println("------------");

    System.out.println("Enter the no: of strings");

    n=scan.nextInt();

    System.out.println("Enter the strings\n");

    for(i=0;i

  • Object Oriented Programming Lab

    Department Of Computer Application. 76

    }

    System.out.println("The sorted list is");

    for(i=0;i

  • Object Oriented Programming Lab

    Department Of Computer Application. 77

    Program No: 8

    PATTERN SEARCHING.

    AIM:

    Write a java program to search a pattern in string, replace it with another string. Use

    string methods.

    ALGORITHM:

    STEP 01: START

    STEP 02: CREATE CLASS, PATTERN

    STEP 03: DEFINE MAIN CLASS

    STEP 04: DECLARE THE STRINGS STR, NEWSTR,REPSTR

    STEP 05: READ THE STRING, STR

    STEP 06: COPY THE STRING STR TO STRING BUFFER S1

    STEP 07: READ THE PATTERN, REPSTR TO BE REPLACED

    STEP 08: READ THE NEW STRING, NEWSTR

    STEP 09: FIND THE POSITION N OF THE REPLACEMENT STRING, REPSTR IN

    STRING, STR

    STEP 10: FIND THE LENGTH N1 OF REPLACEMENT STRING, REPSTR

    STEP 11: PERFORM THE REPLACEMENT OPERATION BY USING REPLACE

    METHOD

    REPLACE THE REPSTR TO NEWSTR

    STEP 12: PRINT THE NEW REPLACED STRING, S1

    STEP 13: STOP

  • Object Oriented Programming Lab

    Department Of Computer Application. 78

    PROGRAM:

    import java.io.*;

    import java.util.*;

    public class pattern {

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

    {

    String str,newstr,repstr;

    StringBuffer s2;

    Scanner ob=new Scanner(System.in);

    System.out.println("Enter the line of text");

    str=ob.nextLine();

    StringBuffer s1=new StringBuffer(str);

    System.out.println("Enter the pattern to be replaced");

    repstr=ob.nextLine();

    System.out.println("Enter the new text");

    newstr=ob.nextLine();

    int n=str.indexOf(repstr);

    int n1=repstr.length();

    s1.replace(n,n+n1,newstr);

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

    System.out.println(s1);

    }

    }

  • Object Oriented Programming Lab

    Department Of Computer Application. 79

    OUTPUT:

    Enter the line of text

    hai, how are you today ?

    Enter the pattern to be replaced

    hai

    Enter the new text

    hello

    The new string is

    hello, how are you today ?

  • Object Oriented Programming Lab

    Department Of Computer Application. 80

    Program No:9

    AREA CALCULATION USING DYNAMIC METHOD

    DISPATCHER

    AIM:

    To find the areas of Rectangle, Triangle and Circle using dynamic method dispatcher.

    ALGORITHM:

    STEP 01 : START

    STEP 02 : DEFINE CLASS SHAPE WITH DATA MEMBERS HEIGHT, WIDTH.

    STEP 03 : DEFINE THE CONSTRUCTOR SHAPE() USED TO INITIALIZE THE

    DATA MEMBERS. HEIGHT AND WIDTH

    STEP 04 : DEFINE AREA() FOR FIND THE AREA OF PARTICULAR SHAPE.

    STEP 05 : DEFINE CLASS RECTANGLE EXTENDS SHAPE.

    STEP 06 : DEFINE THE AREA() WHICH OVERRIDE THE SUPER CLASS

    USED TO DISPLAY THE AREA OF RECTANGLE.

    STEP 07 : DEFINE CLASS TRIANGLE EXTENDS SHAPE.

    STEP 08 : DEFINE THE AREA() WHICH OVERRIDE THE SUPER CLASS

    USED TO DISPLAY THE AREA OF TRIANGLE.

    STEP 09 : DEFINE THE MAIN() TO CREATE OBJECTS FOR SUBCLASSES

    RECTANGLE AND TRIANGLE AND DISPLAY AREA.

    STEP 10 : STOP.

  • Object Oriented Programming Lab

    Department Of Computer Application. 81

    PROGRAM:

    class Shape {

    double height;

    double width;

    Shape(double a, double b) {

    height = a;

    width = b;

    }

    double area() {

    System.out.println("Area for Figure is undefined.");

    return 0;

    }

    }

    class Rectangle extends Shape {

    Rectangle(double a, double b) {

    super(a, b);

    }

    // override area for rectangle

    double area() {

    System.out.println("Inside Area for Rectangle.");

    return height * width;

    }

    }

    class Triangle extends Shape{

    Triangle(double a, double b) {

    super(a, b);

    }

    double area() {

    System.out.println("Inside Area for Triangle.");

    return height * width / 2;

    }

    }

    public class main {

    public static void main(String args[]) {

    Shape f = new Shape(10, 10);

    Rectangle r = new Rectangle(9, 5);

    Triangle t = new Triangle(10, 8);

    Shape figref;

    figref = r;

    System.out.println("Area is " + figref.area());

    figref = t;

  • Object Oriented Programming Lab

    Department Of Computer Application. 82

    System.out.println("Area is " + figref.area());

    figref = f;

    System.out.println("Area is " + figref.area());

    }

    }

    OUTPUT:

    Inside Area for Rectangle.

    Area is 45.0

    Inside Area for Triangle.

    Area is 40.0

    Area for Figure is undefined.

    Area is 0.0

  • Object Oriented Programming Lab

    Department Of Computer Application. 83

    Program No:10

    AREA CALCULATION USING ABSTRACTION

    AIM:

    Write a program to implement area calculation of rectangle, triangle, and circle using

    abstraction.

    ALGORITHM:

    STEP 01: START

    STEP 02: DEFINE AN ABSTRACT CLASS AREA WITH DATA MEMBERS

    LENGTH L AND BREADTH B AND A CONSTRUCTOR OF AREA CLASS.

    STEP 03: EXTEND A SUB CLASS RECTANGLE FROM AREA CLASS WITH

    METHOD TO RETURN AREA OF RECTANGLE.

    STEP 04: EXTEND ANOTHER SUB CLASS TRIANGLE FROM

    AREA WITH ITSMETHOD TO RETURN AREA OF TRIANGLE.

    STEP 05: EXTEND ANOTHER SUB CLASS CIRCLE FROM AREA

    CLASS WITH A METHOD TO RETURN AREA OF CIRCLE.

    STEP 06: DEFINE MAINAREA CLASS

    STEP 07: CREATE A REFERENCE OF THE AREA CLASS.

    STEP 08: CREATE OBJECT FOR RECTANGLE CLASS AND MAKE

    THE REFERENCE VARIABLE NOW REFER TO THE OBJECT CREATED.

    STEP 09: CALL AREA METHOD THAT GIVES AREA OF RECTANGLE;

    STEP 10: CREATE OBJECT FOR TRIANGLE CLASS AND MAKE

  • Object Oriented Programming Lab

    Department Of Computer Application. 84

    THE REFERENCE VARIABLE NOW REFER TO THE OBJECT CREATED.

    STEP 11: CALL AREA METHOD THAT GIVES AREA OF TRIANGLE;

    STEP 12: CREATE OBJECT FOR CIRCLE CLASS AND MAKE THE

    REFERENCE VARIABLE NOW REFER TO THE OBJECT CREATED.

    STEP 13: CALL AREA METHOD THAT GIVES AREA OF CIRCLE;

    STEP 14: STOP

    PROGRAM:

    Import java.util.scanner;

    abstract class Area{

    float l,b;

    Area(float x,float y)

    {

    l=x;

    b=y;

    }

    }

    Class Rectangle extends Area{

    Rectangle(float x,float y){

    Super(x,y);

    }

    float area()

    {

    return x*y;

  • Object Oriented Programming Lab

    Department Of Computer Application. 85

    }

    }

    Class Triangle extends Area{

    Triangle(float x,float y){

    Super(x,y);

    }

    float area()

    {

    return 0.5*x*y;

    }

    }

    Class Circle extends Area{

    Circle(int x){

    Super(x);

    }

    float area()

    {

    return 3.14*x*x;

    }

    }

    Class mainArea{

    Public static void main(String args[]){

    Area A;

    Rectangle R=new Rectangle(8,7);

    A=R;

  • Object Oriented Programming Lab

    Department Of Computer Application. 86

    System.out.println(Area of rectangle:+A.area());

    Triangle T=new Triangle(8,7);

    A=T;

    System.out.println(Area of triangle:+A.area());

    Circle C=new Circle(8);

    A=C;

    System.out.println(Area of circle:+A.area());

    }

    }

    OUTPUT:

    Area of rectangle:56

    Area of triangle:28

    Area of circle:200.96

  • Object Oriented Programming Lab

    Department of Computer Applications 87

    Program No: 11

    COMBINE TWO STRINGS USING USER-DEFINED EXCEPTION.

    AIM:

    Write a program to combine two strings using user-defined exception.

    ALGORITHM:

    STEP1: CREATE A CLASS STRING_EXCEPTION

    STEP2: IN MAIN, DECLARE A STRING VARIABLE S1AND READ S USING

    SCANNER CLASS.

    STEP3:INSIDE TRY BLOCK,

    CHECK WHETHER S1 EQUALS MCA.

    THROW EXCEPTION.

    STEP4: EXCEUTE CATCH STATEMENTS.

    STEP5: EXECUTE FINALLY BLOCK.

    STEP6:CREATE ANOTHER CLASS ST_EXP EXTENDS EXCEPTION

    CALL SUPER (MESSAGE);

    STEP7:STOP.

  • Object Oriented Programming Lab

    Department of Computer Applications 88

    PROGRAM:

    package applet;

    import java.util.Scanner;

    class String_Exception

    {

    public static void main(String args[])

    {

    String s1, s2;

    Scanner in = new Scanner(System.in);

    System.out.println("Enter the first string");

    s1 = in.nextLine();

    try

    {

    if(s1.contentEquals("mca"))

    throw new St_exp("Exception caught");

    }catch(Exception e){

    System.out.println(e.getMessage());

    }finally{

    System.out.println("Finally Block");

    }

    }

    }

  • Object Oriented Programming Lab

    Department of Computer Applications 89

    class St_exp extends Exception{

    St_exp(String message){

    super (message);

    }

    }

    OUTPUT:

    Enter the first string

    mca

    Exception caught

    Finally Block

  • Object Oriented Programming Lab

    Department of Computer Applications 90

    Program No : 12

    DISPLAY NAME USING APPLET

    AIM:

    Create an applet program for Display name using Applet

    ALGORITHM:

    STEP 01: START

    STEP 02: CREATE A NAME CLASS BY EXTENDING APPLET

    STEP 04: SET FOREGROUND COLOUR OF APPLET AS RED

    STEP 05: SET BACKGROUND COLOUR OF APPLET AS BLUE

    STEP 06: CALL DRAWSTRING TO DISPLAY MESSAGE

    STEP 07: END

    PROGRAM:

    /*

    *

    import java.applet.Applet;

    import java.awt.Color;

    import java.awt.Graphics;

    public class Name extends Applet{

    public void paint(Graphics g){

    setBackground(Color.blue);

    setForeground(Color.red);

    g.drawString("Neeraja", 150, 150);

    }

    }

  • Object Oriented Programming Lab

    Department of Computer Applications 91

    OUTPUT:

  • Object Oriented Programming Lab

    Department of Computer Applications 92

    Program No: 13

    STUDENT DETAILS USING APPLET

    AIM:

    Write a program to implement Student details using applet

    ALGORITHM:

    STEP 01:START

    STEP 02:DEFINE STUPANEL EXTENDS APPLET AND

    IMPLEMENT ACTIONLISTENER AND ITEMLISTENER.

    STEP 03:DECLARE THREE STRINGS.

    STEP 04:DECLARE TEXTFIELD ,BUTTON,CHECKBOX,AND PANELS.

    STEP 05:DEFINE THE INIT() TO INITIALIZE

    THE TEXT FIELD,BUTTON,CHECKBOX AND PANELS.

    STEP 06:DEFINE THE PAINT() FOR DISPLAY THE DETAILS OF THE STUDENT.

    STEP 07:DEFINE THE ACTIONPERFORMED() FOR DISPLAY THE

    TOTAL AND AVERAGE MARK OF STUDENTS.

    STEP 8: STOP.

    PROGRAM:

    import java.awt.*;

    import java.applet.*;

    import java.awt.event.*;

    /*

    */

    public class Stupanel extends Applet implements ActionListener,ItemListener

    {

    String s1,s2,s3;

    TextField t3,t4,t5,t6,t7;

    Button tot,avg;

    Checkbox c1,c2,c3,c4,m,f;

    CheckboxGroup cbg;

    Panel p1,p2,p3,p4;

    public void init()

    {

    s3=" ";

    tot=new Button("Total");

    avg=new Button("Average");

    c1=new Checkbox("MCA",true);

  • Object Oriented Programming Lab

    Department of Computer Applications 93

    c2=new Checkbox("Msc Comp");

    c3=new Checkbox("MSIT");

    c4=new Checkbox("MSIS");

    cbg=new CheckboxGroup();

    m=new Checkbox("Male",cbg,false);

    f=new Checkbox("Female",cbg,true);

    p1=new Panel();

    p1.setLayout(new GridLayout(2,2));

    p1.add(new Label("Student Number "));

    p1.add(new TextField(5));

    p1.add(new Label("Student Name "));

    p1.add(new TextField(15));

    add(p1);

    p2=new Panel(); p2.setLayout(new GridLayout(1,3));

    p2.add(new Label("Gender"));

    p2.add(m);

    p2.add(f);

    add(p2);

    p3=new Panel(); p3.setLayout(new GridLayout(1,5));

    p3.add(new Label("Degree"));

    p3.add(c1)

    p3.add(c2);

    p3.add(c3);

    p3.add(c4);

    add(p3);

    p4=new Panel(); p4.setLayout(new GridLayout(6,2));

    p4.add(new Label("Marks in JAVA"));

    t3=new TextField(3);

    p4.add(t3);

    p4.add(new Label("Marks in VB .Net"));

    t4=new TextField(3); p4.add(t4);

    p4.add(new Label("Marks In C"));

    t5=new TextField(3);

    p4.add(t5);

    p4.add(new Label("Total "));

    t6=new TextField(3);

    p4.add(t6);

    p4.add(new Label(" Average"));

    t7=new TextField(3);

    p4.add(t7)

    p4.add(tot);

    p4.add(avg);

  • Object Oriented Programming Lab

    Department of Computer Applications 94

    tot.addActionListener(this);

    avg.addActionListener(this);

    c1.addItemListener(this);

    c2.addItemListener(this);

    c3.addItemListener(this);

    c4.addItemListener(this);

    m.addItemListener(this);

    f.addItemListener(this);

    add(p4);

    }

    public void paint(Graphics g)

    {

    int no,m1,m2,m3,tot;

    float avg=0.0f;

    no=m1=m2=m3=tot=0;

    try

    {

    m1=Integer.parseInt(t3.getText());

    m2=Integer.parseInt(t4.getText());

    m3=Integer.parseInt(t5.getText());

    }

    catch(Exception e)

    {

    }

    tot=m1+m2+m3;

    avg= tot/3;

    s1=String.valueOf(tot);

    s2=String.valueOf(avg);

    }

    public boolean action(Event e,Object o)

    {

    repaint();

    return true;

    }

    public void actionPerformed(ActionEvent e)

    {

    s3=e.getActionCommand();

    if(s3.equals("Total"))

    t6.setText(s1);

    if(s3.equals("Average"))

    t7.setText(s2);

  • Object Oriented Programming Lab

    Department of Computer Applications 95

    repaint();

    }

    public void itemStateChanged(ItemEvent e)

    {

    repaint();

    }

    }

    OUTPUT:

  • Object Oriented Programming Lab

    Department of Computer Applications 96

    Program No: 14

    BACKGROUND COLOR CHANGE USING APPLET

    AIM:

    Write a program to display background color using applet.

    ALGORITHM:

    STEP 01: START

    STEP 02: DEFINE A CLASS BG_CLR_APPLT EXTENDS

    APPLET AND IMPLEMENTS ACTIONLISTENER.

    STEP 03: CREATE TWO BUTTONS NAMED RED

    AND GREEN WITH RED AND

    GREENLABELS RESPECTIVELY.

    STEP 04: DEFINE INIT() FOR SETTING LAYOUT.

    STEP 05 : DEFINE PAINT() TO DISPLAY REDAND

    GREEN BACKGROUNDS WHILE CLICKING RED AND

    GREEN BUTTONS RESPECTIVELY

    STEP 06: STOP

  • Object Oriented Programming Lab

    Department of Computer Applications 97

    PROGRAM:

    import java.applet.Applet;

    import java.awt.Button;

    import java.awt.Color;

    import java.awt.FlowLayout;

    import java.awt.Graphics;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    public class BG_CLR_Applt extends Applet implements ActionListener{

    Button red =new Button("Red");

    Button green =new Button("Green");

    String color=null;

    public void init(){

    setLayout(new FlowLayout(FlowLayout.CENTER));

    add(red);

    add(green);

    red.addActionListener(this);

    green.addActionListener(this);

    }

    public void paint(Graphics G){

  • Object Oriented Programming Lab

    Department of Computer Applications 98

    }

    public void actionPerformed(ActionEvent e) {

    color=e.getActionCommand();

    if(color.contentEquals("Red"))

    setBackground(Color.red);

    else if(color.contentEquals("Green"))

    setBackground(Color.green);

    repaint();

    }

    }

    OUTPUT:

  • Object Oriented Programming Lab

    Department of Computer Applications 99

    Program No: 15

    STACK OPERATIONS USING INTERFACE

    AIM:

    Write a program to implement a stack using interface.

    ALGORITHM:

    STEP 01: DECLARE AN INTERFACE INTSTACK WITH TWO METHODS FOR PUSH

    AND

    POP OPERATIONS

    STEP 02: DEFINE A CLASS FIXEDSTACK THAT IMPLEMENTS INTSTACK

    STEP 03: DECLARE ONE INTEGER ARRAY AND AN INTEGER VARIABLE TOS

    STEP 04: ALLOCATE AND INITIALIZE THE STACK

    STEP 05: DEFINE THE METHOD FOR PUSH

    5.1 : IF(TOS=SIZE-1) PRINT STACK OVERFLOW

    5.2: OTHERWISE INCREMENT TOS BY ONE AND PUSH THE ITEM

    STEP 06: DEFINE THE FUNCTION FOR POP OPERATION

    6.1: IF(TOS

  • Object Oriented Programming Lab

    Department of Computer Applications 100

    PROGRAM:

    interface IntStack

    {

    void push(int item);

    int pop();

    }

    class FixedStack implements IntStack

    {

    private int stck[];

    private int tos;

    FixedStack(int size)

    {

    stck = new int[size];

    tos = -1;

    }

    public void push(int item)

    {

    if(tos==stck.length-1)

    System.out.println("Stack is full.");

    else stck[++tos] = item;

    }

    public int pop()

    {

    if(tos < 0)

    {

    System.out.println("Stack underflow.");

    return 0;

    }

    else return stck[tos--];

    }

    }

    class stack

    {

    public static void main(String args[])

    {

    FixedStack mystack1 = new FixedStack(5);

    FixedStack mystack2 = new FixedStack(8);

    for(int i=0; i

  • Object Oriented Programming Lab

    Department of Computer Applications 101

    System.out.println("Stack in mystack1:");

    for(int i=0; i

  • Object Oriented Programming Lab

    Department of Computer Applications 102

    Program No : 16

    CALCULATOR USING APPLET

    AIM:

    Write a java program to implement Calculator using applet

    ALGORITHM:

    STEP 01: START

    STEP 02: DECLARE TEXT FIELD, ARRAY OF BUTTONS

    STEP 03: DECLARE RESULT VARIABLES

    STEP 04: SET BACKGROUND COLOR

    STEP 05: INITIALIZE TEXT FIELD TO SHOW THE RESULT

    STEP 06: GRID LAYOUT WITH 4 ROWS AND 5 COLUMNS.

    STEP 07: ADD BUTTON TO APPLET

    STEP 08: ADD ACTION EVENT LISTENER.

    STEP 09: DEFINE ACTION PERFORMED.

    STEP 10: CHECK WHETHER THE VALUES IN TEXT FIELD IS A VALID INTEGER

    STEP 11: IF SO PERFORM THE FUNCTION.

    STEP 12: SHOW THE RESULT IN THE TEXT FIELD

    STEP 13: stop

  • Object Oriented Programming Lab

    Department of Computer Applications 103

    PROGRAM:

    import java.awt.*;

    import java.awt.event.*;

    import java.applet.*;

    /*

    */

    public class Cal extends Applet

    implements ActionListener

    {

    String msg=" ";

    int v1,v2,result;

    TextField t1;

    Button b[]=new Button[10];

    Button add,sub,mul,div,clear,mod,EQ;

    char OP;

    public void init()

    {

    Color k=new Color(120,89,90);

    setBackground(k);

    t1=new TextField(10);

    GridLayout gl=new GridLayout(4,5);

    setLayout(gl);

    for(int i=0;i

  • Object Oriented Programming Lab

    Department of Computer Applications 104

    add(mul);

    add(div);

    add(mod);

    add(clear);

    add(EQ);

    for(int i=0;i

  • Object Oriented Programming Lab

    Department of Computer Applications 105

    v1=Integer.parseInt(t1.getText());

    OP='/';

    t1.setText("");

    }

    else if(str.equals("mod"))

    {

    v1=Integer.parseInt(t1.getText());

    OP='%';

    t1.setText("");

    }

    if(str.equals("EQ"))

    {

    v2=Integer.parseInt(t1.getText());

    if(OP=='+')

    result=v1+v2;

    else if(OP=='-')

    result=v1-v2;

    else if(OP=='*')

    result=v1*v2;

    else if(OP=='/')

    result=v1/v2;

    else if(OP=='%')

    result=v1%v2;

    t1.setText(""+result);

    }

    if(str.equals("clear"))

    {

    t1.setText("");

    }

    }

    }

    OUTPUT:

  • Object Oriented Programming Lab

    Department of Computer Applications 106

    Program No : 17

    BOOK DETAILS USING INHERITANCE

    AIM:

    Write a java program to read the book details (title and price)in the first class from this

    derive two classes book which add a page count and cd which add a size using inheritance

    ALGORITHM:

    STEP 01: START

    STEP 02: CREATE A BASE CLASS ITEM WITH FIELDS TITLE AND PRICE

    STEP 03: DEFINE READ AND DISPLAY METHOD IN ITEM CLASS

    STEP 04: DERIVE A CLASS BOOK FROM ITEM WITH ADDITIONAL FIELD

    PAGES

    STEP 05: REDEFINE READ AND DISPLAY METHODS IN BOOK CLASS

    STEP 06: DERIVE A CLASS CD FROM ITEM WITH ADDITIONAL FIELD SIZE

    STEP 07: REDEFINE READ AND DISPLAY METHODS IN CD CLASS

    STEP 08: DEFINE MAIN CLASS MBOOK

    STEP 09: CREATE OBJECTS OF BOOK AND CD AND CALL THE METHODS

    USING OBJECTS

    STEP 10: STOP

    PROGRAM:

    import java.io.*;

    import java.lang.*;

    import java.util.Scanner;

    class Item

    {

    String title;

    float price;

    Scanner sc=new Scanner(System.in);

    Item()

    {

    title="";

    price=0;

    }

    Item(String a,float b)

  • Object Oriented Programming Lab

    Department of Computer Applications 107

    {

    title=a;

    price=b;

    }

    void read()

    {

    System.out.println("Enter Title:");

    title=sc.next();

    System.out.println("Enter Price:");

    price=sc.nextFloat();

    }

    void display()

    {

    System.out.println("Title:"+title);

    System.out.println("Price:"+price);

    }

    }

    class Book extends Item

    {

    int page;

    Book()

    {

    page=0;

    }

    Book(String a,float b,int c)

    {

    super(a,b);

    page=c;

    }

    void read()

    {

    super.read();

    System.out.println("Enter Number of Pages:");

    page=sc.nextInt();

    }

    void display()

    {

    super.display();

    System.out.println("Pages:"+page);

    }

    }

  • Object Oriented Programming Lab

    Department of Computer Applications 108

    class CD extends Item

    {

    float size;

    CD()

    {

    size=0;

    }

    CD(String a,float b,float c)

    {

    super(a,b);

    size=c;

    }

    void read()

    {

    super.read();

    System.out.println("Enter Size of CD:");

    size=sc.nextFloat();

    }

    void display()

    {

    super.display();

    System.out.println("Size"+size);

    }

    }

    public class Mbook

    {

    public static void main(String args[])

    {

    System.out.println("Book");

    System.out.println("----");

    Book b=new Book();

    b.read();

    b.display();

    System.out.println("CD");

    System.out.println("----");

    CD c=new CD();

    c.read();

    c.display();

    }

    }

  • Object Oriented Programming Lab

    Department of Computer Applications 109

    OUTPUT:

    Book

    ----

    Enter Title:

    AnsiC

    Enter Price:

    250

    Enter Number of Pages:

    358

    Title:AnsiC

    Price:250.0

    Pages:358

    CD

    ----

    Enter Title:

    DBMS_Silberschatz

    Enter Price:

    485

    Enter Size of CD:

    2.75

    Title:DBMS_Silberschatz

    Price:485.0

    Size2.75

  • Object Oriented Programming Lab

    Department of Computer Applications 110

    Program No: 18

    MOUSE EVENTS

    AIM:

    Write a java program to perform mouse events.

    ALGORITHM:

    STEP 01. START

    STEP 02. CREATE CLASS HANDLEMOUSELISTENERINWINDOWEXAMPLE WHICH

    EXTENDS FRAME CLASS

    STEP 03. DEFINE A CONSTRUCTOR OF

    HANDLEMOUSELISTENERINWINDOWEXAMPLE TO REGISTER THE OBJECT TO

    RECEIVE MOUSE EVENTS

    STEP 04. CREATE AN OBJECT FOR CLASS MYWINDOWADAPTER CLASS

    STEP 05. WRITE FUNCTIONS TO HANDLE VARIOUS MOUSE EVENTS

    STEP 06. DEFINE PAINT() TO DISPLAY THE MESSAGE CORRESPONDING TO EACH

    MOUSE EVENTS

    STEP 07. CREATE A CLASS MYWINDOWADAPTER WHICH EXTENDS

    WINDOWADAPTER CLASS

    STEP 08. DEFINE CONSTRUCTOR TO ASSIGN

    HANDLEMOUSELISTENERINWINDOWEXAMPLE OBJECT

    STEP 09. STOP.

  • Object Oriented Programming Lab

    Department of Computer Applications 111

    PROGRAM:

    package frombase;

    import java.awt.Frame;

    import java.awt.Graphics;

    import java.awt.event.MouseEvent;

    import java.awt.event.MouseListener;

    import java.awt.event.WindowAdapter;

    import java.awt.event.WindowEvent;

    public class HandleMouseListenerInWindowExample extends Frame implements

    MouseListener{

    int x=0, y=0;

    String strEvent = "";

    HandleMouseListenerInWindowExample(String title){

    //call superclass constructor with window title

    super(title);

    //add window listener

    addWindowListener(new MyWindowAdapter(this));

    //add mouse listener

    addMouseListener(this);

    //set window size

    setSize(300,300);

    //show the window

    setVisible(true);

  • Object Oriented Programming Lab

    Department of Computer Applications 112

    }

    public void mouseClicked(MouseEvent e) {

    strEvent = "MouseClicked";

    x = e.getX();

    y = getY();

    repaint();

    }

    public void mousePressed(MouseEvent e) {

    strEvent = "MousePressed";