Cs2312 OOPS LAB MANUAL

49
LAB MANUAL 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 MAILAMENGINEERING COLLEGE CS2312 OBJECT ORIENTED PROGRAMMING LAB
  • date post

    21-Oct-2014
  • Category

    Education

  • view

    7.200
  • download

    2

description

 

Transcript of Cs2312 OOPS LAB MANUAL

Page 1: Cs2312 OOPS LAB MANUAL

LAB MANUAL

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

MAILAMENGINEERING COLLEGE

CS2312 OBJECT ORIENTED PROGRAMMING LAB

Page 2: Cs2312 OOPS LAB MANUAL

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;

2

Page 3: Cs2312 OOPS LAB MANUAL

LAB MANUAL

y=x-y;

x=x-y;

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;

3

Page 4: Cs2312 OOPS LAB MANUAL

LAB MANUAL

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

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

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

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***

4

Page 5: Cs2312 OOPS LAB MANUAL

LAB MANUAL

before swapvalue of a: 10

before swapvalue of b: 20

CALLING FUNCTION 1

after swapvalue of x: 20

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

5

Page 6: Cs2312 OOPS LAB MANUAL

LAB MANUAL

after swapvalue of y: A

enter two integer values

P

Q

CALLING FUNCTION 3

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>

6

Page 7: Cs2312 OOPS LAB MANUAL

LAB MANUAL

#include<conio.h>

class complex

{

float real;

float imag;

public:

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

7

Page 8: Cs2312 OOPS LAB MANUAL

LAB MANUAL

}

complex operator ++()

{

real++;

return complex(real,imag);

}

complex operator++(int dummy)

{

imag++;

return complex(real,imag);

}

void display()

{

cout<<real<<"+"<<imag<<"i\n";

}

};

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

8

Page 9: Cs2312 OOPS LAB MANUAL

LAB MANUAL

++comp1;

comp1.display();

++comp2;

comp2.display();

}

OUTPUT:

30+50I

11+20i

21+30i

RESULT:

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

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

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

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

9

Page 10: Cs2312 OOPS LAB MANUAL

LAB MANUAL

PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<math.h>

class AC;

class AB

{

float d;

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; }

10

Page 11: Cs2312 OOPS LAB MANUAL

LAB MANUAL

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):12Enter the second distance(in inches):30Total Distance:14.5feet

RESULT:

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.

11

Page 12: Cs2312 OOPS LAB MANUAL

LAB MANUAL

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>

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];

12

Page 13: Cs2312 OOPS LAB MANUAL

LAB MANUAL

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

};

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

13

Page 14: Cs2312 OOPS LAB MANUAL

LAB MANUAL

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------------------------------------------------------------------------

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.

14

Page 15: Cs2312 OOPS LAB MANUAL

LAB MANUAL

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

}

complex (float r, int i)

{

x=int (r);

y=i;

}

complex(int r, int i)

{

x=r;

y=int(i);

}

complex operator +(complex);

15

Page 16: Cs2312 OOPS LAB MANUAL

LAB MANUAL

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;

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

16

Page 17: Cs2312 OOPS LAB MANUAL

LAB MANUAL

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

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

17

Page 18: Cs2312 OOPS LAB MANUAL

LAB MANUAL

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:

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

18

Page 19: Cs2312 OOPS LAB MANUAL

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.Exit\n";cin>>ch;switch(ch){case 1:ac.saves();

19

Page 20: Cs2312 OOPS LAB MANUAL

LAB MANUAL

break;case 2: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.Exit1Enter the amount to be deposited=Rs.5000Enter the amount to be withdrawn=Rs.500Your A/c Balance is=Rs.4500Enter the A/c type: 1.Savings 2.Current 3.Exit2Last amount withdrawn=Rs.500Last amount deposited=Rs.5000Your A/c Balance is=Rs.4500Enter the A/c type: 1.Savings 2.Current 3.Exit3

RESULT:

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

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

EXPT NO:7 Stack Class With Exception Handling

20

Page 21: Cs2312 OOPS LAB MANUAL

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)

21

Page 22: Cs2312 OOPS LAB MANUAL

LAB MANUAL

{

s[++top]=i;

}

else

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++)

{22

Page 23: Cs2312 OOPS LAB MANUAL

LAB MANUAL

cout<<a.pop()<<endl;

}

getch();

}

OUTPUT:

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

23

Page 24: Cs2312 OOPS LAB MANUAL

LAB MANUAL

Step 9: stop the program

PROGRAM:

#include <iostream>

# define MAX 4

class queue

{

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]=1424

Page 25: Cs2312 OOPS LAB MANUAL

LAB MANUAL

arr[rear]=item;

if(front==-1)

front=0;

count<<"\n Item added into queue : <<arr[real];

}int del q()

{

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";

}

25

Page 26: Cs2312 OOPS LAB MANUAL

LAB MANUAL

int i;

try

{ i=a.delq();

cout<<endl<<"Item deleted=<<i;

i=a.del q();

cout<<eendl<<"Item deleted =" <<i;

i=a.del q ();

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

26

Page 27: Cs2312 OOPS LAB MANUAL

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 mtable.

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+" ]");

27

Page 28: Cs2312 OOPS LAB MANUAL

LAB MANUAL

}

}

}

COMPILATION AND EXECUTION:

D:\javaprg>javac mtable.java

D:\javaprg>java mtable

OUTPUT:

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 ]

28

Page 29: Cs2312 OOPS LAB MANUAL

LAB MANUAL

RESULT:

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

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

EXPT NO:10 Palindrome using strings in java

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

29

Page 30: Cs2312 OOPS LAB MANUAL

LAB MANUAL

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:

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.30

Page 31: Cs2312 OOPS LAB MANUAL

LAB MANUAL

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

{

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

Page 32: Cs2312 OOPS LAB MANUAL

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

32

Page 33: Cs2312 OOPS LAB MANUAL

LAB MANUAL

AIM:

To perform multiple inheritance in java.

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[])

{

33

Page 34: Cs2312 OOPS LAB MANUAL

LAB MANUAL

int avg;

b1 b=new b1();

b.s1=70;

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

{

34

Page 35: Cs2312 OOPS LAB MANUAL

LAB MANUAL

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

}

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

{

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: 6835

Page 36: Cs2312 OOPS LAB MANUAL

LAB MANUAL

GRADE IS (61-70): C

RESULT:Thus the programs for are done using java.

EXPT NO:13 Exception handling using java

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

36

Page 37: Cs2312 OOPS LAB MANUAL

LAB MANUAL

{

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

}

catch(NumberFormatException e)

{

invalid++;

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

}

}

}

37

Page 38: Cs2312 OOPS LAB MANUAL

LAB MANUAL

OUTPUT:

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:

38

Page 39: Cs2312 OOPS LAB MANUAL

LAB MANUAL

public class sqr1 extends Thread

{

public void run()

{

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

39

Page 40: Cs2312 OOPS LAB MANUAL

LAB MANUAL

s.sleep(6000);

s.start();

c.start();

}

}

OUTPUT:

E:\D.PRABHU\thrd>javac sqr1.java

E:\D.PRABHU\thrd>javac cub1.java

E:\D.PRABHU\thrd>javac thrd1.java

E:\D.PRABHU\thrd>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

40

Page 41: Cs2312 OOPS LAB MANUAL

LAB MANUAL

3 cube is:: 27

4 cube is:: 64

5 cube is:: 125

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