SlideShare uma empresa Scribd logo
1 de 41
LAB MANUAL


                                  MAILAMENGINEERING COLLEGE

     CS2312                          OBJECT ORIENTED PROGRAMMING LAB



LIST OF PROGRAMS USING C++ :

  1. Inline Function Overloading Using Default Arguments And User Input.

  2. Complex Number Using Operator Overloading.

  3. To read a value of distance from one object and add with a value in another object using friend function

  4. String concatenation using dynamic memory allocation concept

  5. Type Conversion Using Complex Numbers

  6. Managing bank account using inheritance concept

  7. Stack Class With Exception Handling

  8. Queue Class With Exception Handling Using Templates

LIST OF PROGRAMS USING JAVA :

  9. Writing simple programs in Java.

  10. Palindrome Using Strings In Java

  11. Use of interfaces in Java

  12. Using Inheritance In Java

  13. Exception handling in Java

  14. Design of multithreaded programs in Java.




                                                   1
LAB MANUAL
       EXPT NO:1              Inline Function Overloading Using Default Arguments And User Input.

AIM:

         To write a c++ program for Inline function overloading using default arguments and user input.

ALGORITHM:

         Step 1: start the program

         Step 2: declare three swap inline functions of two variables each in integer, float and character

         Step 3: call the function 1 with defaults arguments.

         Step 4: get user input for function 1

         Step 5: repeat step 3 and 4 for function 2 and 3

         Step 6: stop the program

PROGRAM:

#include<iostream.h>

#include<conio.h>

void swap(int x ,int y)

{

x=x+y;

y=x-y;

x=x-y;

cout<<endl<<"t"<<"CALLING FUNCTION 1 "<<endl;

cout<<endl<<"after swapvalue of x: "<<x<<endl;

cout<<endl<<"after swapvalue of y: "<<y<<endl;

}

void swap(float x ,float y)

{

x=x+y;

y=x-y;

x=x-y;
                                                        2
LAB MANUAL
cout<<endl<<"t"<<"CALLING FUNCTION 2 "<<endl;

cout<<endl<<"after swapvalue of x: "<<x<<endl;

cout<<endl<<"after swapvalue of y: "<<y<<endl;

}

void swap(char x1 ,char y1)

{

char t;

t=x1;

x1=y1;

y1=t;

cout<<endl<<"t"<<"CALLING FUNCTION 3 "<<endl;

cout<<endl<<"after swapvalue of x1: "<<x1<<endl;

cout<<endl<<"after swapvalue of y1: "<<y1<<endl;

}

void main()

{

int a,b;

float c,d;

char e,f,t;

clrscr();

a=10;

b=20;

cout<<endl<<"***FUNCTION OVERLOADING WITH DEFAULT ARGUMENTS***"<<endl;

cout<<endl<<"before swapvalue of a: "<<a<<endl;

cout<<endl<<"before swapvalue of b: "<<b<<endl;

swap(a,b); //swapping with default arguments

cout<<endl<<"enter two integer values"<<endl;
                                                   3
LAB MANUAL
cin>>a>>b;

swap(a,b);//swapping with user input arguments

c=92.7;

d=106.4;

cout<<endl<<"before swapvalue of c: "<<c<<endl;

cout<<endl<<"before swapvalue of d: "<<d<<endl;

swap(c,d);//swapping with default arguments

cout<<endl<<"enter two float values"<<endl;

cin>>c>>d;

swap(c,d);//swapping with user input arguments

e='A';

f='Z';

cout<<endl<<"before swapvalue of e: "<<e<<endl;

cout<<endl<<"before swapvalue of f: "<<f<<endl;

swap(e,f);//swapping with default arguments

cout<<endl<<"enter two characters"<<endl;

cin>>e>>f;

swap(e,f);//swapping with user input arguments

getch();

}



Output:

***FUNCTION OVERLOADING WITH DEFAULT ARGUMENTS***

before swapvalue of a: 10

before swapvalue of b: 20

CALLING FUNCTION 1

after swapvalue of x: 20
                                                  4
LAB MANUAL
after swapvalue of y: 10

enter two integer values

5

7

CALLING FUNCTION 1

after swapvalue of x: 7

after swapvalue of y: 5

before swapvalue of c: 92.7

before swapvalue of d: 106.4

CALLING FUNCTION 2

after swapvalue of x: 106.4

after swapvalue of y: 92.7

enter two integer values

5.55

7.89

CALLING FUNCTION 2

after swapvalue of x: 7.89

after swapvalue of y: 5.55

before swapvalue of e: A

before swapvalue of f: Z

CALLING FUNCTION 3

after swapvalue of x: Z

after swapvalue of y: A

enter two integer values

P

Q

CALLING FUNCTION 3
                                   5
LAB MANUAL
after swapvalue of x: Q

after swapvalue of y: P



RESULT:

 Thus the c++ program for inline function overloading with default arguments and user input has been
executed successfully



----------------------------------------------------
XXX------------------------------------------------------------------------

          EXPT NO:2                          Complex Number Using Operator Overloading.

AIM:

          To write a c++ program for complex numbers using operator overloading

ALGORITHM:

          Step 1: start the program

          Step 2: initialize the complex function of image and real in float

          Step 3: declare the complex add for complex two

          Step 4: return the complex temp of the real and image in float

          Step 5: increment the real and image value

          Step 6: print the output

          Step 7: stop the program

PROGRAM:

#include<iostream.h>

#include<conio.h>

class complex

{

float real;

float imag;

public:
                                                             6
LAB MANUAL
complex(float tempreal=0,float tempimag=0)

{

real=tempreal;

imag=tempimag;

}

complex add(complex comp2)

{

float tempreal;

float tempimag;

tempreal=real+comp2.real;

tempimag=imag+comp2.imag;

return complex(tempreal,tempimag);

}

complex operator+(complex comp2)

{

float tempreal;

float tempimag;

tempreal=real+comp2.real;

tempimag=imag+comp2.imag;

return complex(tempreal,tempimag);

}

complex operator ++()

{

real++;

return complex(real,imag);

}

complex operator++(int dummy)
                                                 7
LAB MANUAL
{

imag++;

return complex(real,imag);

}

void display()

{

cout<<real<<"+"<<imag<<"in";

}

};

void main()

{

getch();

complex comp1(10,20);

complex comp2(20,30);

complex compresult1,compresult2;

compresult1=comp1.add(comp2);

compresult1.display();

compresult2=comp1+comp2;

compresult2.display();

++comp1;

comp1.display();

++comp2;

comp2.display();

}



OUTPUT:

30+50I
                                       8
LAB MANUAL
11+20i

21+30i

RESULT:

    Thus the c++ program for complex numbers using operator overloading has been executed successfully

----------------------------------------------------
XXX------------------------------------------------------------------------



                          To read a value of distance from one object and add with a value in another object
     EXPT NO:3
                                                         using friend function


AIM:
         To write a c++ program to read a value of distance from one object and add with a value in another
object using friend function.
ALGORITHM:

         Step 1: start the program
         Step 2: Create two classes AB and AC and store the value of distances.
         Step 3: Declare friend function.
         Step 4: Read the value from the classes.
         Step 5: Perform addition to add one object of AB with another object of AC.
         Step 6: Display the result of addition.
         Step 7: stop the program




PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<math.h>
class AC;
class AB
{
float d;
                                                             9
LAB MANUAL
public:
AB()
{
cout<<"Enter the first distance(in feet):";
cin>>d;
}
friend void add(AB,AC);
 };
class AC
 {
 float d;
 public:
 AC()
 {
  cout<<"Enter the second distance(in inches):";
  cin>>d;
  }
friend void add(AB a,AC b)
  {
    float total;
    b.d=b.d/12;
    total=a.d+b.d;
    cout<<"Total Distance:"<<total<<"feet";
    }
};
void main()
 {
 clrscr();
 AB A;
 AC B;
 add(A,B);
 getch();
  }
OUTPUT:
Enter the first distance(in feet):12
Enter the second distance(in inches):30
Total Distance:14.5feet
RESULT:




                                                   10
LAB MANUAL
 Thus the c++ program for reading a value of distance from one object and add with a value in another
object using friend function has been executed successfully
----------------------------------------------------
XXX------------------------------------------------------------------------



     EXPT NO:4
                                       String concatenation using dynamic memory allocation concept


 AIM:

           To write a c++ program for string concatenation function by using dynamic memory allocation
concept.
ALGORITHM:

        Step 1: start the program
        Step 2: Create class STRING with two constructors.

        Step 3: The first is an empty constructor, which allows declaring an array of strings.

        Step 4: The second constructor initializes the length of the strings, and allocates necessary space for
                 the string to be stored and creates the string itself.

        Step 5: Create a member function to concatenate two strings.

        Step 6: Estimate the combined length of the strings to be joined and allocates memory for the
                 combined string using new operator and then creates the same using the string functions
                 strcpy() and strcat().

        Step 7: Display the concatenated string.

        Step 8: stop the program



PROGRAM:
#include<iostream.h>
#include<string.h>
#include<conio.h>

                                                            11
LAB MANUAL
class string
{
char *name;
int length;
public:
string()
{
length=0;
name=new char[length+1];
}
string(char*s)
{
length=strlen(s);
name=new char[length+1];
strcpy(name,s);
}
void display(void)
{
cout<<name<<"n";
}
void join(string &a,string &b);
};
void string::join(string &a,string &b)
{
length=a.length+b.length;
delete name;
name=new char[length+1];
strcpy(name,a.name);
strcat(name,b.name);

                                             12
LAB MANUAL
};
void main()
{
clrscr();
string name1 ("Object ");
string name2 ("Oriented ");
string name3 ("Programming Lab");
string s1,s2;
s1.join(name1,name2);
s2.join(s1,name3);
name1.display();
name2.display();
name3.display();
s1.display();
s2.display();
getch();
}
OUTPUT:
Object
Oriented
Programming Lab
Object Oriented
Object Oriented Programming Lab



RESULT:

              Thus the c++ program for String concatenation using dynamic memory allocation concept

has been executed successfully
----------------------------------------------------
XXX------------------------------------------------------------------------

                                                            13
LAB MANUAL


    EXPT NO:5                                Type Conversion Using Complex Numbers



AIM:
    To write a c++ program for type conversion using complex numbers.

ALGORITHM:

Step 1: start the program.

Step 2: create the class complex

Step 3: declare the variables x and y.

Step 4: construct the default constructor.

Step 5: define the variables

Step 6: create the object c1, c2, c3 and initialize it.

Step 7: display the output.

Step 8: stop the program.

PROGRAM:

#include<iostream.h>
using namespace std;
class complex
{

int x, y;

public:

complex(){}

complex(float r, float i)

{

x=int(r);

y= int (y);

}

                                                          14
LAB MANUAL
complex (float r, int i)

{

x=int (r);

y=i;

}

complex(int r, int i)

{

x=r;

y=int(i);

}

complex operator +(complex);

void display(void);

};

complex complex :: operator+(complex c)

{

complex t;

t.x=x+c.x;

t.y=y+c.y;

return (t);

}

void complex :: display (void)

{

cout<<x<<”+i”<<y<<”n”;

}

int main()

{

complex c1,c2,c3;
                                              15
LAB MANUAL
c1=complex (2,3.5);

c2=complex(1,2.7);

c3=c1+c2;

cout<<”c1=”;c1.display();

cout<<”c2=”;c2.display();

cout<<”c3=”;c3.display();

c1=complex(6,2.4);

c2=complex(4,3.5);

c3=c1+c2;

cout<<”c1=”;c1.display();

cout<<”c2=”;c2.display();

cout<<”c3=”;c3.display();

c1=complex(6,6.2);

c2=complex(3,2.1);

c3=c1+c2;

cout<<”c1=”;c1.display();

cout<<”c2=”;c2.display();

cout<<”c3=”;c3.display();

return 0;

}

OUTPUT:

C1=2+i3

C2=1+i2

C3=3+i5

C1=6+i2

C2=4+i3

C3=10+i5
                                16
LAB MANUAL
       C1=6+i6

       C2=3+i2

       C3=9+i8

       RESULT:

                Thus the C++ program for type conversion in complex number has been executed successfully.

       ----------------------------------------------------
       XXX------------------------------------------------------------------------




            EXPT NO:6                          Managing bank account using inheritance concept


       AIM:
               To write a c++ program to manage the account information of the customer using inheritance concept.

       ALGORITHM:

       Step 1: start the program.
       Step 2: Create a class with the following member variables. Customer name, account number and account
       type.
       Step 3: Create the derived classes with following member variables.
•   for current account information Balance, Deposit and withdrawal amount
•   for savings account information Balance and Deposit amount
       Step 4: Write a member function to get the Deposit and withdrawal amount and to update the balance
       information for current account.
       Step 5: Write a member function to get the Deposit amount and to update the balance information for saving
       account.
       Step 6: Write a member function to Display the balance information for respective account type.
       Step 7: Stop the program.

       PROGRAM:




                                                                   17
LAB MANUAL
#include<iostream.h>
#include<conio.h>
class bank
{
public:
char*cname;
long int acno;
void display()
{
cout<<cname;
cout<<acno;




                           18
LAB MANUAL
}
};

class savings : public bank
{
public:
int wdraw, dep, bal;
void saves()
{
cout<<"Enter the amount to be deposited=Rs.";
cin>>dep;
bal+=dep;
cout<<"Enter the amount to be withdrawn=Rs.";
cin>>wdraw;
bal-=wdraw;
cout<<"Your A/c Balance is=Rs."<<bal<<endl;
}
};

class current : public savings
{
public:
void info()
{
cout<<"Last amount witdrawn=Rs."<<wdraw<<endl;
cout<<"Last amount deposited=Rs."<<dep<<endl;
cout<<"Your A/c Balance is=Rs."<<bal<<endl;
}
};

void main()
{
int ch;
current ac;
clrscr();
cout<<"Enter customer name:";
cin>>ac.cname;
cout<<endl<<"Enter your A/c no.:";
cin>>ac.acno;
cout<<endl;
while(1)
{
cout<<"Enter the A/c type,1.Savings 2.Current 3.Exitn";
cin>>ch;
switch(ch)
{
case 1:
ac.saves();
break;
case 2:
                                                   19
LAB MANUAL
ac.info();
break;
case 3:
break;
default:
cout<<"Invalid Choice";
break;
}
if (ch==3)
break;
}
getch();
}

OUTPUT:


Enter customer name: XXX

Enter your A/c no.:666

Enter the A/c type: 1.Savings 2.Current 3.Exit
1
Enter the amount to be deposited=Rs.5000
Enter the amount to be withdrawn=Rs.500
Your A/c Balance is=Rs.4500
Enter the A/c type: 1.Savings 2.Current 3.Exit
2
Last amount withdrawn=Rs.500
Last amount deposited=Rs.5000
Your A/c Balance is=Rs.4500
Enter the A/c type: 1.Savings 2.Current 3.Exit
3


RESULT:

 Thus the C++ program for managing bank account has been executed successfully.

----------------------------------------------------
XXX------------------------------------------------------------------------




       EXPT NO:7                                 Stack Class With Exception Handling



                                                            20
LAB MANUAL
AIM:
          To write a c++ program for stack class with exception handling
ALGORITHM:
          Step 1: start the program
          Step 2: declare the two function (i) PUSH (ii) POP
          Step 3: initialize the stack () constructor top element is to be NULL
          Step 4: check for the condition if(top>=9) throw to exception 8 assign the value
          Step 5: in POP function check for the condition if (top<0)
          Step 6: print the output
          Step 7: stop the program
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<process.h>
class Stack
{
int s[10],top;
public:
class Size{};
Stack()
{
top=-1;
}
~Stack()
{
}
void push(int i)
{
if(top<=9)
{
s[++top]=i;
}
else
                                                        21
LAB MANUAL
cout<<"OverFlow occurs......Size is"<<top<<"> than stack size";
}
int pop()
{
if(top<0)
{
cout<<"UnderFlow occurs......Size is";
return top+1;
}
else
return s[top--];
}
};
void main()
{
Stack a;
int i,ae,size;
clrscr();
cout<<"Enter the Satck size:";
cin>>size;
cout<<"nPush Operation.....n";
for(i=0;i<size;i++)
{
cout<<"Enter the Value:";
cin>>ae;
a.push(ae);
}
cout<<endl<<"Pop Operation.....n";
for(i=0;i<=size;i++)
{
cout<<a.pop()<<endl;
}
getch();
}
OUTPUT:
                                                   22
LAB MANUAL
Enter the stack size:3
Push operation ….
Enter the value : 10
Enter the value: 20
Enter the value: 30
Pop operation …
30
20
10
underflow occurs…size is 0
RESULT:
 Thus the c++ program for stack class with exception handling has been executed successfully.
----------------------------------------------------
XXX------------------------------------------------------------------------

      EXPT NO:8                       QUEUE Class With Exception Handling and Templates



AIM:
     To write a c++ program for queue class with exception handling using templates
ALGORITHM:
Step 1: start the program
Step 2: create the class queue and decleare the front and rear values
Step 4: initialize the front and rear == -1
Step 4: then the condition is true incremented the front value
Step 5: then the condition is flase then display the output the queue is empty
Step 6: after that incremented the rear value
Step 7: the elements are inserted and display the queue
Step 8: print the output
Step 9: stop the program
PROGRAM:
#include <iostream>
# define MAX 4
class queue

                                                            23
LAB MANUAL
{
private :
int arr [ MAX] ;
int front, rear ;
public :
class q full
{
};
class q empty
{
};
queue ()
{
front=-1;
rear=-1;
}
void add q(int item)
{
if (rear==MAX-1)
throw q full () ;
rear ++ ;
arr [0]=11
arr[1]=12
arr[2]=13
front=0
arr[3]=14
arr[rear]=item;
if(front==-1)
front=0;
count<<"n Item added into queue : <<arr[real];
}int del q()
{
                                                  24
LAB MANUAL
int data;
if(front==-1)
throw q emty ();
data=arr[front] ;
if(front==rear=-1;
else
front++;
return data ;
}
};
void Main()
{
queue a;
try
{
a.add q (11);
a.add q (12);
a.add q(13);
a.add q (14);
a.add q (15); queue is full
}
catch (queue ::qfull)
{
cout<<endl<<"QUEUE IS FULL";
}
int i;
try
{ i=a.delq();
cout<<endl<<"Item deleted=<<i;
i=a.del q();
cout<<eendl<<"Item deleted =" <<i;
i=a.del q ();
                                         25
LAB MANUAL
cout <<endl<<"Item deleted=" <<i;
i=a,del q ();
cout<<endl<<"Item deleted="<<i;
i=a.del()
cout <<endl<<"Item deleted=<<i;
i=a.delq());
}
OUTPUT:
item added into queue:11
item added into queue:12
item added into queue:13
item added into queue:14
queue is full
item deleted:11
item deleted:12
item deleted:13
item deleted:14
queue is empty
RESULT:
        Thus the c++ program for queue class with exception handling has been executed successfully.
----------------------------------------------------
XXX------------------------------------------------------------------------



       EXPT NO:9                          To Print Multiplication Table Of A Given Number



AIM:

To write a java program to print multiplication table of a given number

Algorithm:

    Step 1: Start the Program

    Step 2: create a class with the name mtable.

                                                            26
LAB MANUAL
        Step 3: Declare two integer variables i and k.

        Step 4: Get input for variable k from the user.

        Step 5: Use for loop to perform multiplication table operation

        Step 6: Print the result.

        Step 7: Stop the program

PROGRAM:

import java.io.*;

class mtable

{

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

    {

         int i,k;

         DataInputStream di= new DataInputStream(System.in);

         System.out.println("enter the number ");

         k=Integer.parseInt(di.readLine());

         System.out.println("***multiplication table *****");

         for(i=1;i<=15;i++)

         {

             System.out.println("[ "+i+" * "+k+" ] "+" [ "+i*k+" ]");

         }

    }

}

COMPILATION AND EXECUTION:

D:javaprg>javac mtable.java
D:javaprg>java mtable

OUTPUT:


                                                          27
LAB MANUAL
enter the number

9

***multiplication table *****

[ 1 * 9 ][ 9 ]

[ 2 * 9 ][ 18 ]

[ 3 * 9 ][ 27 ]

[ 4 * 9 ][ 36 ]

[ 5 * 9 ][ 45 ]

[ 6 * 9 ][ 54 ]

[ 7 * 9 ][ 63 ]

[ 8 * 9 ][ 72 ]

[ 9 * 9 ][ 81 ]

[ 10 * 9 ][ 90 ]

[ 11 * 9 ][ 99 ]

[ 12 * 9 ][ 108 ]

[ 13 * 9 ][ 117 ]

[ 14 * 9 ][ 126 ]

[ 15 * 9 ][ 135 ]



RESULT:
        Thus the program to perform multiplication table is done using java.


----------------------------------------------------
XXX------------------------------------------------------------------------



      EXPT NO:10                                    Palindrome using strings in java



                                                            28
LAB MANUAL
AIM:

To write a java program to print multiplication table of a given number

Algorithm:

    Step 1: Start the Program

    Step 2: create a class with the name palin.

    Step 3: Declare a string and two integer variable.

    Step 4: Assign value for the string.

    Step 5: Store the length of the string in a integer variable.

    Step 6: Reverse the string with charAt() method.

    Step 7: check if the reversed string and input string are equal.

    Step 8: Print the result.

    Step 9: Stop the program.

PROGRAM:

import java.io.*;
class palin
{
public static void main(String args[])
        {
         String s="malayalam";
         int i;
         int n=s.length();
         String str="";
        for(i=n-1;i>=0;i--)
        str=str+s.charAt(i);
         System.out.println("n t GIVEN STRING:: "+s);
        System.out.println("n t REVERSED STRING:: "+str);
        if(str.equals(s))
        System.out.println(s+ " :: IS A PALINDROME");
            else
        System.out.println(s+ " : :IS NOT A PALINDROME");
        }
}
COMPILATION AND EXECUTION:
                                                         29
LAB MANUAL
D:javaprg>javac mtable.java
D:javaprg>java mtable

OUTPUT:

GIVEN STRING: malayalam

REVERSED STRING:: malayalam

Malayalam :: IS A PALINDROME

RESULT:
Thus the program to perform palindrome using java is done.
----------------------------------------------------
XXX------------------------------------------------------------------------



       EXPT NO:11                       To calculate area of rectangle and circle using interfaces



AIM:
        To calculate area of rectangle and circle using interfaces.

ALGORITHM:

    Step 1: Start the Program.

    Step 2: Create an interface with name area.

    Step 3: Create classes rectangle and circle which implements area.

    Step 4: Create a class interface test.

    Step5: Create object for rectangle, circle class and interface area.

    Step6: Print the area of circle and triangle

    Step 7: Stop the Program.

PROGRAM:

import java .io.*;

interface Area

{
                                                            30
LAB MANUAL
       final static float pi=3.14F;

       float compute(float x,float y);

}

class Rectangle implements Area

{

       public float compute(float x,float y)

       {

               return(x*y);

       }

}

class Circle implements Area

{

       public float compute(float x, float y)

       {

               return(pi*x*x);

       }

}




                                                    31
LAB MANUAL
class InterfaceTest
{
        public static void main(String args[])
        {
                 Rectangle rect=new Rectangle();
                 Circle cir=new Circle();
                 Area ar;
                 ar=rect;
                 System.out.println("Area of Rectangle="+ar.compute(10,20));
                 ar=cir;
                 System.out.println("Area of Circle="+ar.compute(10,0));
        }
}


COMPILATION AND EXECUTION:
D:javaprg>javac InterfaceTest.java
D:javaprg>java InterfaceTest


OUTPUT:

Area of Rectangle=200.0
Area of Circle=314.0

RESULT:

Thus the programs for calculating area for circle and rectangle are done using java.

----------------------------------------------------
XXX------------------------------------------------------------------------




      EXPT NO:12                                        To perform multilevel inheritance


AIM:
        To perform multiple inheritance in java.

                                                            32
LAB MANUAL
ALGORITHM:

     Step 1: Start the Program.

     Step 2: Create class a1 and declare 3 variables.

     Step 3: Create class b1 which extends a1.

     Step 4: Class b1 has 3 variables and also derived from base class a1

     Step 5: Create class c1 which is derived from base class b1

    Step 6: Access variables from a1 and b1 and process the data in c1.

    Step 7: Stop the Program.

PROGRAM:

import java.io.*;

class a1

{

    int s1,s2,s3;

}

class b1 extends a1 //simple inheritance

{

    int s4=70, s5=90;

    int total;

}

class c1 extends b1//multilevel inheritance

{

public static void main(String ar[])

{

    int avg;

    b1 b=new b1();

    b.s1=70;
                                                        33
LAB MANUAL
    b.s2 = 50;

    b.s3 = 60;

    b.total = b.s1+b.s2+b.s3+b.s4+b.s5;

    avg = b.total/5;

System.out.println("***** multilevel inheritance*******");

System.out.println("values from class a1: " +b.s1 +"n");

System.out.println("values from class a1: " +b.s2+"n" );

System.out.println("values from class a1: " +b.s3 +"n");

System.out.println("values from class b1: " +b.s4 +"n");

System.out.println("values from class b1: " +b.s5 +"n");

System.out.println(" total value from class bt: " +b.total+"n" );

System.out.println("average of 5 numbers: "+avg+"n");

if ((avg>=91)&&(avg<=100))

{

System.out.println("GRADE IS (91-100) : S ");

}

else if ((avg>=81)&&(avg<=90))

{

System.out.println("GRADE IS (81-90) : A ");

}

else if ((avg>=71)&&(avg<=80))

{

System.out.println("GRADE IS (71-80) : B ");

}

else if ((avg>=61)&&(avg<=70))

{

                                                       34
LAB MANUAL
System.out.println("GRADE IS (61-70) : c ");

}

else if ((avg>=50)&&(avg<=60))

{

System.out.println("GRADE IS (50-60) : D ");

}

else if ((avg>=0)&&(avg<=49))

{

System.out.println("GRADE IS (0-49) : F ");

} } }

COMPILATION AND EXECUTION:
D:javaprg>javac InterfaceTest.java
D:javaprg>java InterfaceTest
OUTPUT:

***** multilevel inheritance*******

values from class a1: 70

values from class a1: 50

values from class a1: 60

values from class b1: 70

values from class b1: 90

total value from class b1: 340

average of 5 numbers: 68

GRADE IS (61-70): C

RESULT:
Thus the programs for are done using java.

     EXPT NO:13                                   Exception handling using java



                                                   35
LAB MANUAL
AIM:
             To perform exception handling using java.

ALGORITHM:

        Step 1: Start the Program.

        Step 2: Create class exph1 and declare 3 variables.

        Step 3: Use Try and Catch block for arithmetic exception.

        Step 4: Use Try and Catch block for number format exception.

        Step 5 Stop the Program.

PROGRAM:

class exph1

{

    public static void main(String ar[])

    {

         int a = 10;

         int b = 0;

         int c;

         int i,n=ar.length,sum=0,invalid=0;

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

         {

             try

             {

                 sum+=Integer.parseInt(ar[i]);

             }

             catch(NumberFormatException e)

             {

                 invalid++;
                                                         36
LAB MANUAL
                System.out.println("n"+"---------NUMBERFORMAT EXCEPTION-----");

                    System.out.println(ar[i]+" :: is not an integer"+"n" );

                }

        }

        System.out.print("n"+"TOTAL NO OF ARGUMENTS : "+n);

        System.out.println("n"+"invalid data:: "+invalid);

        System.out.println("n"+"sumof valid integers:: "+sum);

        try

        {

        c=a/b;

            }

        catch(ArithmeticException e)

        {

                     System.out.println("n"+"--------ARITHMETIC EXCEPTION------ ");

                    System.out.println("n"+"dividend is :: "+a+"t"+"divisor is :: "+b);

                    System.out.println("Dvision by zero");

        }

    }

}

OUTPUT:




                                                                37
LAB MANUAL




RESULT:

Thus the program for exception handling is done using java.

       EXPT NO:14                                        Multithreading using java


AIM:
         To perform multithreading using java.

ALGORITHM:

     Step 1: Start the Program.

     Step 2: Create a class sqr1 which extends Thread.

     Step 3: Create a class cub1 which extends Thread.

     Step 4: Create a class thrd1.

     Step 5: Create objects for a class sqr1 and cub1.

     Step 6: Execute the program

     Step 7: Stop the Program.

PROGRAM:

public class sqr1 extends Thread

{

    public void run()
                                                         38
LAB MANUAL
    {

        for(int i=1;i<=30;i++)

        System.out.println("n"+i+" square is:: "+i*i);

    }

}

public class cub1 extends Thread

{

    public void run()

    {

        for(int i=1;i<=10;i++)

           System.out.println("n"+i+" cube is:: "+i*i*i);

    }

}

public class thrd1

{

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

    {

        sqr1 s;

        cub1 c;

        s = new sqr1();

        c = new cub1();

        s.sleep(6000);

        s.start();

        c.start();

    }

                                                          39
LAB MANUAL
}

OUTPUT:



E:D.PRABHUthrd>javac sqr1.java
E:D.PRABHUthrd>javac cub1.java
E:D.PRABHUthrd>javac thrd1.java
E:D.PRABHUthrd>java thrd1


1 square is:: 1


2 square is:: 4


3 square is:: 9


4 square is:: 16


5 square is:: 25


6 square is:: 36


7 square is:: 49


8 square is:: 64


1 cube is:: 1


2 cube is:: 8


3 cube is:: 27


4 cube is:: 64


5 cube is:: 125


                                        40
LAB MANUAL
6 cube is:: 216


7 cube is:: 343


8 cube is:: 512


9 cube is:: 729


10 cube is:: 1000


9 square is:: 81


10 square is:: 100
RESULT:

Thus the program for multithreading using java is done.




                                                    41

Mais conteúdo relacionado

Mais procurados (20)

Java lab1 manual
Java lab1 manualJava lab1 manual
Java lab1 manual
 
Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
 
Java programming-examples
Java programming-examplesJava programming-examples
Java programming-examples
 
Java ppt
Java pptJava ppt
Java ppt
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Java Fundamentals
Java FundamentalsJava Fundamentals
Java Fundamentals
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
Kotlin
KotlinKotlin
Kotlin
 
SystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummarySystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features Summary
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
 
Ch01 basic-java-programs
Ch01 basic-java-programsCh01 basic-java-programs
Ch01 basic-java-programs
 
Rhino Mocks
Rhino MocksRhino Mocks
Rhino Mocks
 
Volley lab btc_bbit
Volley lab btc_bbitVolley lab btc_bbit
Volley lab btc_bbit
 
Java programs
Java programsJava programs
Java programs
 
Learn Java Part 2
Learn Java Part 2Learn Java Part 2
Learn Java Part 2
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Java Tutorial
Java Tutorial Java Tutorial
Java Tutorial
 

Destaque

Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Abdul Hannan
 
Bca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroductionBca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroductionRai University
 
Programming II LAB 3 - OOP
Programming II LAB 3 - OOPProgramming II LAB 3 - OOP
Programming II LAB 3 - OOPFares Younis
 
Programming II LAB 4 (OOP) inheritance
Programming II LAB 4 (OOP)  inheritanceProgramming II LAB 4 (OOP)  inheritance
Programming II LAB 4 (OOP) inheritanceFares Younis
 
Advanced Java - Praticals
Advanced Java - PraticalsAdvanced Java - Praticals
Advanced Java - PraticalsFahad Shaikh
 
Java Code for Sample Projects Inheritance
Java Code for Sample Projects InheritanceJava Code for Sample Projects Inheritance
Java Code for Sample Projects Inheritancejwjablonski
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manualsameer farooq
 
Advanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceAdvanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceNiraj Bharambe
 
Lab manual of C++
Lab manual of C++Lab manual of C++
Lab manual of C++thesaqib
 
Advanced Java Practical File
Advanced Java Practical FileAdvanced Java Practical File
Advanced Java Practical FileSoumya Behera
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL fileRACHIT_GUPTA
 

Destaque (14)

Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual
 
Bca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroductionBca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroduction
 
Programming II LAB 3 - OOP
Programming II LAB 3 - OOPProgramming II LAB 3 - OOP
Programming II LAB 3 - OOP
 
Programming II LAB 4 (OOP) inheritance
Programming II LAB 4 (OOP)  inheritanceProgramming II LAB 4 (OOP)  inheritance
Programming II LAB 4 (OOP) inheritance
 
C++ file
C++ fileC++ file
C++ file
 
Advanced Java - Praticals
Advanced Java - PraticalsAdvanced Java - Praticals
Advanced Java - Praticals
 
Java Code for Sample Projects Inheritance
Java Code for Sample Projects InheritanceJava Code for Sample Projects Inheritance
Java Code for Sample Projects Inheritance
 
Java Lab Manual
Java Lab ManualJava Lab Manual
Java Lab Manual
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
 
Advanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceAdvanced java practical semester 6_computer science
Advanced java practical semester 6_computer science
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
 
Lab manual of C++
Lab manual of C++Lab manual of C++
Lab manual of C++
 
Advanced Java Practical File
Advanced Java Practical FileAdvanced Java Practical File
Advanced Java Practical File
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
 

Semelhante a Cs2312 OOPS LAB MANUAL

chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfstudy material
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2zindadili
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++msharshitha03s
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answerssheibansari
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaonyash production
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 studrohassanie
 
c++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdfc++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdfayush616992
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++gourav kottawar
 
operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cppgourav kottawar
 

Semelhante a Cs2312 OOPS LAB MANUAL (20)

Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdf
 
C++ Question
C++ QuestionC++ Question
C++ Question
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
Programming in c
Programming in cProgramming in c
Programming in c
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
c++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdfc++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdf
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Labsheet2
Labsheet2Labsheet2
Labsheet2
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cpp
 

Mais de Prabhu D

Network lab manual
Network lab manualNetwork lab manual
Network lab manualPrabhu D
 
Unit v-ooad-notes-revised
Unit v-ooad-notes-revisedUnit v-ooad-notes-revised
Unit v-ooad-notes-revisedPrabhu D
 
Unit i-ooad-notes-revised
Unit i-ooad-notes-revisedUnit i-ooad-notes-revised
Unit i-ooad-notes-revisedPrabhu D
 
Unit iii-ooad-notes-revised
Unit iii-ooad-notes-revisedUnit iii-ooad-notes-revised
Unit iii-ooad-notes-revisedPrabhu D
 
Ooad unit-iv-notes-revised
Ooad unit-iv-notes-revisedOoad unit-iv-notes-revised
Ooad unit-iv-notes-revisedPrabhu D
 
Unit ii-ooad-notes-revision-1
Unit ii-ooad-notes-revision-1Unit ii-ooad-notes-revision-1
Unit ii-ooad-notes-revision-1Prabhu D
 

Mais de Prabhu D (6)

Network lab manual
Network lab manualNetwork lab manual
Network lab manual
 
Unit v-ooad-notes-revised
Unit v-ooad-notes-revisedUnit v-ooad-notes-revised
Unit v-ooad-notes-revised
 
Unit i-ooad-notes-revised
Unit i-ooad-notes-revisedUnit i-ooad-notes-revised
Unit i-ooad-notes-revised
 
Unit iii-ooad-notes-revised
Unit iii-ooad-notes-revisedUnit iii-ooad-notes-revised
Unit iii-ooad-notes-revised
 
Ooad unit-iv-notes-revised
Ooad unit-iv-notes-revisedOoad unit-iv-notes-revised
Ooad unit-iv-notes-revised
 
Unit ii-ooad-notes-revision-1
Unit ii-ooad-notes-revision-1Unit ii-ooad-notes-revision-1
Unit ii-ooad-notes-revision-1
 

Último

Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 

Último (20)

Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 

Cs2312 OOPS LAB MANUAL

  • 1. LAB MANUAL MAILAMENGINEERING COLLEGE CS2312 OBJECT ORIENTED PROGRAMMING LAB LIST OF PROGRAMS USING C++ : 1. Inline Function Overloading Using Default Arguments And User Input. 2. Complex Number Using Operator Overloading. 3. To read a value of distance from one object and add with a value in another object using friend function 4. String concatenation using dynamic memory allocation concept 5. Type Conversion Using Complex Numbers 6. Managing bank account using inheritance concept 7. Stack Class With Exception Handling 8. Queue Class With Exception Handling Using Templates LIST OF PROGRAMS USING JAVA : 9. Writing simple programs in Java. 10. Palindrome Using Strings In Java 11. Use of interfaces in Java 12. Using Inheritance In Java 13. Exception handling in Java 14. Design of multithreaded programs in Java. 1
  • 2. LAB MANUAL EXPT NO:1 Inline Function Overloading Using Default Arguments And User Input. AIM: To write a c++ program for Inline function overloading using default arguments and user input. ALGORITHM: Step 1: start the program Step 2: declare three swap inline functions of two variables each in integer, float and character Step 3: call the function 1 with defaults arguments. Step 4: get user input for function 1 Step 5: repeat step 3 and 4 for function 2 and 3 Step 6: stop the program PROGRAM: #include<iostream.h> #include<conio.h> void swap(int x ,int y) { x=x+y; y=x-y; x=x-y; cout<<endl<<"t"<<"CALLING FUNCTION 1 "<<endl; cout<<endl<<"after swapvalue of x: "<<x<<endl; cout<<endl<<"after swapvalue of y: "<<y<<endl; } void swap(float x ,float y) { x=x+y; y=x-y; x=x-y; 2
  • 3. LAB MANUAL cout<<endl<<"t"<<"CALLING FUNCTION 2 "<<endl; cout<<endl<<"after swapvalue of x: "<<x<<endl; cout<<endl<<"after swapvalue of y: "<<y<<endl; } void swap(char x1 ,char y1) { char t; t=x1; x1=y1; y1=t; cout<<endl<<"t"<<"CALLING FUNCTION 3 "<<endl; cout<<endl<<"after swapvalue of x1: "<<x1<<endl; cout<<endl<<"after swapvalue of y1: "<<y1<<endl; } void main() { int a,b; float c,d; char e,f,t; clrscr(); a=10; b=20; cout<<endl<<"***FUNCTION OVERLOADING WITH DEFAULT ARGUMENTS***"<<endl; cout<<endl<<"before swapvalue of a: "<<a<<endl; cout<<endl<<"before swapvalue of b: "<<b<<endl; swap(a,b); //swapping with default arguments cout<<endl<<"enter two integer values"<<endl; 3
  • 4. LAB MANUAL cin>>a>>b; swap(a,b);//swapping with user input arguments c=92.7; d=106.4; cout<<endl<<"before swapvalue of c: "<<c<<endl; cout<<endl<<"before swapvalue of d: "<<d<<endl; swap(c,d);//swapping with default arguments cout<<endl<<"enter two float values"<<endl; cin>>c>>d; swap(c,d);//swapping with user input arguments e='A'; f='Z'; cout<<endl<<"before swapvalue of e: "<<e<<endl; cout<<endl<<"before swapvalue of f: "<<f<<endl; swap(e,f);//swapping with default arguments cout<<endl<<"enter two characters"<<endl; cin>>e>>f; swap(e,f);//swapping with user input arguments getch(); } Output: ***FUNCTION OVERLOADING WITH DEFAULT ARGUMENTS*** before swapvalue of a: 10 before swapvalue of b: 20 CALLING FUNCTION 1 after swapvalue of x: 20 4
  • 5. LAB MANUAL after swapvalue of y: 10 enter two integer values 5 7 CALLING FUNCTION 1 after swapvalue of x: 7 after swapvalue of y: 5 before swapvalue of c: 92.7 before swapvalue of d: 106.4 CALLING FUNCTION 2 after swapvalue of x: 106.4 after swapvalue of y: 92.7 enter two integer values 5.55 7.89 CALLING FUNCTION 2 after swapvalue of x: 7.89 after swapvalue of y: 5.55 before swapvalue of e: A before swapvalue of f: Z CALLING FUNCTION 3 after swapvalue of x: Z after swapvalue of y: A enter two integer values P Q CALLING FUNCTION 3 5
  • 6. LAB MANUAL after swapvalue of x: Q after swapvalue of y: P RESULT: Thus the c++ program for inline function overloading with default arguments and user input has been executed successfully ---------------------------------------------------- XXX------------------------------------------------------------------------ EXPT NO:2 Complex Number Using Operator Overloading. AIM: To write a c++ program for complex numbers using operator overloading ALGORITHM: Step 1: start the program Step 2: initialize the complex function of image and real in float Step 3: declare the complex add for complex two Step 4: return the complex temp of the real and image in float Step 5: increment the real and image value Step 6: print the output Step 7: stop the program PROGRAM: #include<iostream.h> #include<conio.h> class complex { float real; float imag; public: 6
  • 7. LAB MANUAL complex(float tempreal=0,float tempimag=0) { real=tempreal; imag=tempimag; } complex add(complex comp2) { float tempreal; float tempimag; tempreal=real+comp2.real; tempimag=imag+comp2.imag; return complex(tempreal,tempimag); } complex operator+(complex comp2) { float tempreal; float tempimag; tempreal=real+comp2.real; tempimag=imag+comp2.imag; return complex(tempreal,tempimag); } complex operator ++() { real++; return complex(real,imag); } complex operator++(int dummy) 7
  • 8. LAB MANUAL { imag++; return complex(real,imag); } void display() { cout<<real<<"+"<<imag<<"in"; } }; void main() { getch(); complex comp1(10,20); complex comp2(20,30); complex compresult1,compresult2; compresult1=comp1.add(comp2); compresult1.display(); compresult2=comp1+comp2; compresult2.display(); ++comp1; comp1.display(); ++comp2; comp2.display(); } OUTPUT: 30+50I 8
  • 9. LAB MANUAL 11+20i 21+30i RESULT: Thus the c++ program for complex numbers using operator overloading has been executed successfully ---------------------------------------------------- XXX------------------------------------------------------------------------ To read a value of distance from one object and add with a value in another object EXPT NO:3 using friend function AIM: To write a c++ program to read a value of distance from one object and add with a value in another object using friend function. ALGORITHM: Step 1: start the program Step 2: Create two classes AB and AC and store the value of distances. Step 3: Declare friend function. Step 4: Read the value from the classes. Step 5: Perform addition to add one object of AB with another object of AC. Step 6: Display the result of addition. Step 7: stop the program PROGRAM: #include<iostream.h> #include<conio.h> #include<math.h> class AC; class AB { float d; 9
  • 10. LAB MANUAL public: AB() { cout<<"Enter the first distance(in feet):"; cin>>d; } friend void add(AB,AC); }; class AC { float d; public: AC() { cout<<"Enter the second distance(in inches):"; cin>>d; } friend void add(AB a,AC b) { float total; b.d=b.d/12; total=a.d+b.d; cout<<"Total Distance:"<<total<<"feet"; } }; void main() { clrscr(); AB A; AC B; add(A,B); getch(); } OUTPUT: Enter the first distance(in feet):12 Enter the second distance(in inches):30 Total Distance:14.5feet RESULT: 10
  • 11. LAB MANUAL Thus the c++ program for reading a value of distance from one object and add with a value in another object using friend function has been executed successfully ---------------------------------------------------- XXX------------------------------------------------------------------------ EXPT NO:4 String concatenation using dynamic memory allocation concept AIM: To write a c++ program for string concatenation function by using dynamic memory allocation concept. ALGORITHM: Step 1: start the program Step 2: Create class STRING with two constructors. Step 3: The first is an empty constructor, which allows declaring an array of strings. Step 4: The second constructor initializes the length of the strings, and allocates necessary space for the string to be stored and creates the string itself. Step 5: Create a member function to concatenate two strings. Step 6: Estimate the combined length of the strings to be joined and allocates memory for the combined string using new operator and then creates the same using the string functions strcpy() and strcat(). Step 7: Display the concatenated string. Step 8: stop the program PROGRAM: #include<iostream.h> #include<string.h> #include<conio.h> 11
  • 12. LAB MANUAL class string { char *name; int length; public: string() { length=0; name=new char[length+1]; } string(char*s) { length=strlen(s); name=new char[length+1]; strcpy(name,s); } void display(void) { cout<<name<<"n"; } void join(string &a,string &b); }; void string::join(string &a,string &b) { length=a.length+b.length; delete name; name=new char[length+1]; strcpy(name,a.name); strcat(name,b.name); 12
  • 13. LAB MANUAL }; void main() { clrscr(); string name1 ("Object "); string name2 ("Oriented "); string name3 ("Programming Lab"); string s1,s2; s1.join(name1,name2); s2.join(s1,name3); name1.display(); name2.display(); name3.display(); s1.display(); s2.display(); getch(); } OUTPUT: Object Oriented Programming Lab Object Oriented Object Oriented Programming Lab RESULT: Thus the c++ program for String concatenation using dynamic memory allocation concept has been executed successfully ---------------------------------------------------- XXX------------------------------------------------------------------------ 13
  • 14. LAB MANUAL EXPT NO:5 Type Conversion Using Complex Numbers AIM: To write a c++ program for type conversion using complex numbers. ALGORITHM: Step 1: start the program. Step 2: create the class complex Step 3: declare the variables x and y. Step 4: construct the default constructor. Step 5: define the variables Step 6: create the object c1, c2, c3 and initialize it. Step 7: display the output. Step 8: stop the program. PROGRAM: #include<iostream.h> using namespace std; class complex { int x, y; public: complex(){} complex(float r, float i) { x=int(r); y= int (y); } 14
  • 15. LAB MANUAL complex (float r, int i) { x=int (r); y=i; } complex(int r, int i) { x=r; y=int(i); } complex operator +(complex); void display(void); }; complex complex :: operator+(complex c) { complex t; t.x=x+c.x; t.y=y+c.y; return (t); } void complex :: display (void) { cout<<x<<”+i”<<y<<”n”; } int main() { complex c1,c2,c3; 15
  • 17. LAB MANUAL C1=6+i6 C2=3+i2 C3=9+i8 RESULT: Thus the C++ program for type conversion in complex number has been executed successfully. ---------------------------------------------------- XXX------------------------------------------------------------------------ EXPT NO:6 Managing bank account using inheritance concept AIM: To write a c++ program to manage the account information of the customer using inheritance concept. ALGORITHM: Step 1: start the program. Step 2: Create a class with the following member variables. Customer name, account number and account type. Step 3: Create the derived classes with following member variables. • for current account information Balance, Deposit and withdrawal amount • for savings account information Balance and Deposit amount Step 4: Write a member function to get the Deposit and withdrawal amount and to update the balance information for current account. Step 5: Write a member function to get the Deposit amount and to update the balance information for saving account. Step 6: Write a member function to Display the balance information for respective account type. Step 7: Stop the program. PROGRAM: 17
  • 18. LAB MANUAL #include<iostream.h> #include<conio.h> class bank { public: char*cname; long int acno; void display() { cout<<cname; cout<<acno; 18
  • 19. LAB MANUAL } }; class savings : public bank { public: int wdraw, dep, bal; void saves() { cout<<"Enter the amount to be deposited=Rs."; cin>>dep; bal+=dep; cout<<"Enter the amount to be withdrawn=Rs."; cin>>wdraw; bal-=wdraw; cout<<"Your A/c Balance is=Rs."<<bal<<endl; } }; class current : public savings { public: void info() { cout<<"Last amount witdrawn=Rs."<<wdraw<<endl; cout<<"Last amount deposited=Rs."<<dep<<endl; cout<<"Your A/c Balance is=Rs."<<bal<<endl; } }; void main() { int ch; current ac; clrscr(); cout<<"Enter customer name:"; cin>>ac.cname; cout<<endl<<"Enter your A/c no.:"; cin>>ac.acno; cout<<endl; while(1) { cout<<"Enter the A/c type,1.Savings 2.Current 3.Exitn"; cin>>ch; switch(ch) { case 1: ac.saves(); break; case 2: 19
  • 20. LAB MANUAL ac.info(); break; case 3: break; default: cout<<"Invalid Choice"; break; } if (ch==3) break; } getch(); } OUTPUT: Enter customer name: XXX Enter your A/c no.:666 Enter the A/c type: 1.Savings 2.Current 3.Exit 1 Enter the amount to be deposited=Rs.5000 Enter the amount to be withdrawn=Rs.500 Your A/c Balance is=Rs.4500 Enter the A/c type: 1.Savings 2.Current 3.Exit 2 Last amount withdrawn=Rs.500 Last amount deposited=Rs.5000 Your A/c Balance is=Rs.4500 Enter the A/c type: 1.Savings 2.Current 3.Exit 3 RESULT: Thus the C++ program for managing bank account has been executed successfully. ---------------------------------------------------- XXX------------------------------------------------------------------------ EXPT NO:7 Stack Class With Exception Handling 20
  • 21. LAB MANUAL AIM: To write a c++ program for stack class with exception handling ALGORITHM: Step 1: start the program Step 2: declare the two function (i) PUSH (ii) POP Step 3: initialize the stack () constructor top element is to be NULL Step 4: check for the condition if(top>=9) throw to exception 8 assign the value Step 5: in POP function check for the condition if (top<0) Step 6: print the output Step 7: stop the program PROGRAM: #include<iostream.h> #include<conio.h> #include<process.h> class Stack { int s[10],top; public: class Size{}; Stack() { top=-1; } ~Stack() { } void push(int i) { if(top<=9) { s[++top]=i; } else 21
  • 22. LAB MANUAL cout<<"OverFlow occurs......Size is"<<top<<"> than stack size"; } int pop() { if(top<0) { cout<<"UnderFlow occurs......Size is"; return top+1; } else return s[top--]; } }; void main() { Stack a; int i,ae,size; clrscr(); cout<<"Enter the Satck size:"; cin>>size; cout<<"nPush Operation.....n"; for(i=0;i<size;i++) { cout<<"Enter the Value:"; cin>>ae; a.push(ae); } cout<<endl<<"Pop Operation.....n"; for(i=0;i<=size;i++) { cout<<a.pop()<<endl; } getch(); } OUTPUT: 22
  • 23. LAB MANUAL Enter the stack size:3 Push operation …. Enter the value : 10 Enter the value: 20 Enter the value: 30 Pop operation … 30 20 10 underflow occurs…size is 0 RESULT: Thus the c++ program for stack class with exception handling has been executed successfully. ---------------------------------------------------- XXX------------------------------------------------------------------------ EXPT NO:8 QUEUE Class With Exception Handling and Templates AIM: To write a c++ program for queue class with exception handling using templates ALGORITHM: Step 1: start the program Step 2: create the class queue and decleare the front and rear values Step 4: initialize the front and rear == -1 Step 4: then the condition is true incremented the front value Step 5: then the condition is flase then display the output the queue is empty Step 6: after that incremented the rear value Step 7: the elements are inserted and display the queue Step 8: print the output Step 9: stop the program PROGRAM: #include <iostream> # define MAX 4 class queue 23
  • 24. LAB MANUAL { private : int arr [ MAX] ; int front, rear ; public : class q full { }; class q empty { }; queue () { front=-1; rear=-1; } void add q(int item) { if (rear==MAX-1) throw q full () ; rear ++ ; arr [0]=11 arr[1]=12 arr[2]=13 front=0 arr[3]=14 arr[rear]=item; if(front==-1) front=0; count<<"n Item added into queue : <<arr[real]; }int del q() { 24
  • 25. LAB MANUAL int data; if(front==-1) throw q emty (); data=arr[front] ; if(front==rear=-1; else front++; return data ; } }; void Main() { queue a; try { a.add q (11); a.add q (12); a.add q(13); a.add q (14); a.add q (15); queue is full } catch (queue ::qfull) { cout<<endl<<"QUEUE IS FULL"; } int i; try { i=a.delq(); cout<<endl<<"Item deleted=<<i; i=a.del q(); cout<<eendl<<"Item deleted =" <<i; i=a.del q (); 25
  • 26. LAB MANUAL cout <<endl<<"Item deleted=" <<i; i=a,del q (); cout<<endl<<"Item deleted="<<i; i=a.del() cout <<endl<<"Item deleted=<<i; i=a.delq()); } OUTPUT: item added into queue:11 item added into queue:12 item added into queue:13 item added into queue:14 queue is full item deleted:11 item deleted:12 item deleted:13 item deleted:14 queue is empty RESULT: Thus the c++ program for queue class with exception handling has been executed successfully. ---------------------------------------------------- XXX------------------------------------------------------------------------ EXPT NO:9 To Print Multiplication Table Of A Given Number AIM: To write a java program to print multiplication table of a given number Algorithm: Step 1: Start the Program Step 2: create a class with the name mtable. 26
  • 27. LAB MANUAL Step 3: Declare two integer variables i and k. Step 4: Get input for variable k from the user. Step 5: Use for loop to perform multiplication table operation Step 6: Print the result. Step 7: Stop the program PROGRAM: import java.io.*; class mtable { public static void main(String ar[])throws IOException { int i,k; DataInputStream di= new DataInputStream(System.in); System.out.println("enter the number "); k=Integer.parseInt(di.readLine()); System.out.println("***multiplication table *****"); for(i=1;i<=15;i++) { System.out.println("[ "+i+" * "+k+" ] "+" [ "+i*k+" ]"); } } } COMPILATION AND EXECUTION: D:javaprg>javac mtable.java D:javaprg>java mtable OUTPUT: 27
  • 28. LAB MANUAL enter the number 9 ***multiplication table ***** [ 1 * 9 ][ 9 ] [ 2 * 9 ][ 18 ] [ 3 * 9 ][ 27 ] [ 4 * 9 ][ 36 ] [ 5 * 9 ][ 45 ] [ 6 * 9 ][ 54 ] [ 7 * 9 ][ 63 ] [ 8 * 9 ][ 72 ] [ 9 * 9 ][ 81 ] [ 10 * 9 ][ 90 ] [ 11 * 9 ][ 99 ] [ 12 * 9 ][ 108 ] [ 13 * 9 ][ 117 ] [ 14 * 9 ][ 126 ] [ 15 * 9 ][ 135 ] RESULT: Thus the program to perform multiplication table is done using java. ---------------------------------------------------- XXX------------------------------------------------------------------------ EXPT NO:10 Palindrome using strings in java 28
  • 29. LAB MANUAL AIM: To write a java program to print multiplication table of a given number Algorithm: Step 1: Start the Program Step 2: create a class with the name palin. Step 3: Declare a string and two integer variable. Step 4: Assign value for the string. Step 5: Store the length of the string in a integer variable. Step 6: Reverse the string with charAt() method. Step 7: check if the reversed string and input string are equal. Step 8: Print the result. Step 9: Stop the program. PROGRAM: import java.io.*; class palin { public static void main(String args[]) { String s="malayalam"; int i; int n=s.length(); String str=""; for(i=n-1;i>=0;i--) str=str+s.charAt(i); System.out.println("n t GIVEN STRING:: "+s); System.out.println("n t REVERSED STRING:: "+str); if(str.equals(s)) System.out.println(s+ " :: IS A PALINDROME"); else System.out.println(s+ " : :IS NOT A PALINDROME"); } } COMPILATION AND EXECUTION: 29
  • 30. LAB MANUAL D:javaprg>javac mtable.java D:javaprg>java mtable OUTPUT: GIVEN STRING: malayalam REVERSED STRING:: malayalam Malayalam :: IS A PALINDROME RESULT: Thus the program to perform palindrome using java is done. ---------------------------------------------------- XXX------------------------------------------------------------------------ EXPT NO:11 To calculate area of rectangle and circle using interfaces AIM: To calculate area of rectangle and circle using interfaces. ALGORITHM: Step 1: Start the Program. Step 2: Create an interface with name area. Step 3: Create classes rectangle and circle which implements area. Step 4: Create a class interface test. Step5: Create object for rectangle, circle class and interface area. Step6: Print the area of circle and triangle Step 7: Stop the Program. PROGRAM: import java .io.*; interface Area { 30
  • 31. LAB MANUAL final static float pi=3.14F; float compute(float x,float y); } class Rectangle implements Area { public float compute(float x,float y) { return(x*y); } } class Circle implements Area { public float compute(float x, float y) { return(pi*x*x); } } 31
  • 32. LAB MANUAL class InterfaceTest { public static void main(String args[]) { Rectangle rect=new Rectangle(); Circle cir=new Circle(); Area ar; ar=rect; System.out.println("Area of Rectangle="+ar.compute(10,20)); ar=cir; System.out.println("Area of Circle="+ar.compute(10,0)); } } COMPILATION AND EXECUTION: D:javaprg>javac InterfaceTest.java D:javaprg>java InterfaceTest OUTPUT: Area of Rectangle=200.0 Area of Circle=314.0 RESULT: Thus the programs for calculating area for circle and rectangle are done using java. ---------------------------------------------------- XXX------------------------------------------------------------------------ EXPT NO:12 To perform multilevel inheritance AIM: To perform multiple inheritance in java. 32
  • 33. LAB MANUAL ALGORITHM: Step 1: Start the Program. Step 2: Create class a1 and declare 3 variables. Step 3: Create class b1 which extends a1. Step 4: Class b1 has 3 variables and also derived from base class a1 Step 5: Create class c1 which is derived from base class b1 Step 6: Access variables from a1 and b1 and process the data in c1. Step 7: Stop the Program. PROGRAM: import java.io.*; class a1 { int s1,s2,s3; } class b1 extends a1 //simple inheritance { int s4=70, s5=90; int total; } class c1 extends b1//multilevel inheritance { public static void main(String ar[]) { int avg; b1 b=new b1(); b.s1=70; 33
  • 34. LAB MANUAL b.s2 = 50; b.s3 = 60; b.total = b.s1+b.s2+b.s3+b.s4+b.s5; avg = b.total/5; System.out.println("***** multilevel inheritance*******"); System.out.println("values from class a1: " +b.s1 +"n"); System.out.println("values from class a1: " +b.s2+"n" ); System.out.println("values from class a1: " +b.s3 +"n"); System.out.println("values from class b1: " +b.s4 +"n"); System.out.println("values from class b1: " +b.s5 +"n"); System.out.println(" total value from class bt: " +b.total+"n" ); System.out.println("average of 5 numbers: "+avg+"n"); if ((avg>=91)&&(avg<=100)) { System.out.println("GRADE IS (91-100) : S "); } else if ((avg>=81)&&(avg<=90)) { System.out.println("GRADE IS (81-90) : A "); } else if ((avg>=71)&&(avg<=80)) { System.out.println("GRADE IS (71-80) : B "); } else if ((avg>=61)&&(avg<=70)) { 34
  • 35. LAB MANUAL System.out.println("GRADE IS (61-70) : c "); } else if ((avg>=50)&&(avg<=60)) { System.out.println("GRADE IS (50-60) : D "); } else if ((avg>=0)&&(avg<=49)) { System.out.println("GRADE IS (0-49) : F "); } } } COMPILATION AND EXECUTION: D:javaprg>javac InterfaceTest.java D:javaprg>java InterfaceTest OUTPUT: ***** multilevel inheritance******* values from class a1: 70 values from class a1: 50 values from class a1: 60 values from class b1: 70 values from class b1: 90 total value from class b1: 340 average of 5 numbers: 68 GRADE IS (61-70): C RESULT: Thus the programs for are done using java. EXPT NO:13 Exception handling using java 35
  • 36. LAB MANUAL AIM: To perform exception handling using java. ALGORITHM: Step 1: Start the Program. Step 2: Create class exph1 and declare 3 variables. Step 3: Use Try and Catch block for arithmetic exception. Step 4: Use Try and Catch block for number format exception. Step 5 Stop the Program. PROGRAM: class exph1 { public static void main(String ar[]) { int a = 10; int b = 0; int c; int i,n=ar.length,sum=0,invalid=0; for(i=0;i<n;i++) { try { sum+=Integer.parseInt(ar[i]); } catch(NumberFormatException e) { invalid++; 36
  • 37. LAB MANUAL System.out.println("n"+"---------NUMBERFORMAT EXCEPTION-----"); System.out.println(ar[i]+" :: is not an integer"+"n" ); } } System.out.print("n"+"TOTAL NO OF ARGUMENTS : "+n); System.out.println("n"+"invalid data:: "+invalid); System.out.println("n"+"sumof valid integers:: "+sum); try { c=a/b; } catch(ArithmeticException e) { System.out.println("n"+"--------ARITHMETIC EXCEPTION------ "); System.out.println("n"+"dividend is :: "+a+"t"+"divisor is :: "+b); System.out.println("Dvision by zero"); } } } OUTPUT: 37
  • 38. LAB MANUAL RESULT: Thus the program for exception handling is done using java. EXPT NO:14 Multithreading using java AIM: To perform multithreading using java. ALGORITHM: Step 1: Start the Program. Step 2: Create a class sqr1 which extends Thread. Step 3: Create a class cub1 which extends Thread. Step 4: Create a class thrd1. Step 5: Create objects for a class sqr1 and cub1. Step 6: Execute the program Step 7: Stop the Program. PROGRAM: public class sqr1 extends Thread { public void run() 38
  • 39. LAB MANUAL { for(int i=1;i<=30;i++) System.out.println("n"+i+" square is:: "+i*i); } } public class cub1 extends Thread { public void run() { for(int i=1;i<=10;i++) System.out.println("n"+i+" cube is:: "+i*i*i); } } public class thrd1 { public static void main(String ar[])throws Exception { sqr1 s; cub1 c; s = new sqr1(); c = new cub1(); s.sleep(6000); s.start(); c.start(); } 39
  • 40. LAB MANUAL } OUTPUT: E:D.PRABHUthrd>javac sqr1.java E:D.PRABHUthrd>javac cub1.java E:D.PRABHUthrd>javac thrd1.java E:D.PRABHUthrd>java thrd1 1 square is:: 1 2 square is:: 4 3 square is:: 9 4 square is:: 16 5 square is:: 25 6 square is:: 36 7 square is:: 49 8 square is:: 64 1 cube is:: 1 2 cube is:: 8 3 cube is:: 27 4 cube is:: 64 5 cube is:: 125 40
  • 41. LAB MANUAL 6 cube is:: 216 7 cube is:: 343 8 cube is:: 512 9 cube is:: 729 10 cube is:: 1000 9 square is:: 81 10 square is:: 100 RESULT: Thus the program for multithreading using java is done. 41