You are on page 1of 62

Ex.

no:1: OBJECTS, CLASSES, CONSTRUCTORS &DESTRUCTORS USING C++


AIM: To write a program to implement the concept of objects, classes, Constructors and Destructor.

ALGORITHM: Step I Step II Define a class for bank account Member data present in the class are a) Name of the depositor b) Account type c) Account number d) Balance Member functions present in the class are: i) for initializing the balance while the customer is introduced (Constructors) ii) for depositing amount for the customer iii) for withdrawing amount for the customer after checking the balance iv) for displaying the details of the customer v) Removing and account if a customer leaves the bank Declare objects for the class Exercise the routines Terminate the program

Step III

Step IV Step V Step VI

SOURCE CODE: # include<iostream.h> # include<conio.h> class bank { private: char name[20]; char acc_no[10]; int acc_type; float bal,d,w;

public: void assign(); void deposit(); void display(); void withdraw(); }; void bank::display() { cout<<"ENTER DEPOSITOR NAME:"; cin>>name; cout<<"ENTER ACCOUNT NUMBER:"; cin>>acc_no; cout<<"ENTER ACCOUNT TYPE:"; cin>>acc_type; cout<<"ENTER THE INITIAL AMOUNT:"; cin>>bal; if(bal<=500) cout<<"NO TRANSACTION IS POSSIBLE"; else { cout<<"CURRENT STATUS OF YOUR ACCOUNT::\n"; cout<<"NAME:"<<name<<"\n"; cout<<"ACC_NO:"<<acc_no<<"\n"; cout<<"BALANCE:"<<bal<<"\n"; } } void bank::deposit() { cout<<"ENTER DEPOSITOR NAME:"; cin>>name; cout<<"ENTER ACCOUNT NUMBER:"; cin>>acc_no; cout<<"ENTER ACCOUNT TYPE:"; cin>>acc_type; cout<<"ENTER THE INITIAL AMOUNT:"; cin>>bal; cout<<"CURRENT STATUS OF YOUR ACCOUNT::\n"; cout<<"NAME:"<<name<<"\n"; cout<<"ACC_NO:"<<acc_no<<"\n";

cout<<"BALANCE:"<<bal<<"\n"; cout<<"ENTER THE AMOUNT TO BE DEPOSITED:"; cin>>d; bal=bal+d; cout<<"TOTAL AMOUNT:"<<bal<<"\n"; } void bank::withdraw() { cout<<"ENTER DEPOSITOR NAME:"; cin>>name; cout<<"ENTER ACCOUNT NUMBER:"; cin>>acc_no; cout<<"ENTER ACCOUNT TYPE:"; cin>>acc_type; cout<<"ENTER THE INITIAL AMOUNT:"; cin>>bal; cout<<"CURRENT STATUS OF YOUR ACCOUNT::\n"; cout<<"NAME:"<<name<<"\n"; cout<<"ACC_NO:"<<acc_no<<"\n"; cout<<"BALANCE:"<<bal<<"\n"; if(bal<=500) cout<<"AMOUNT CANNOT BE WITHDRAWN"; else { cout<<"ENTER THE AMOUNT TO BE WITHDRAWN:"; cin>>w; if((w>bal) || ((bal-w)<=500)) { cout<<"WITHDRAWAL IS NOT POSSIBLE\n"; } else { bal=bal-w; cout<<"TOTAL AMOUNT:"<<bal<<"\n"; }}} void main() { int ch;

clrscr(); bank c; cout<<"ENTER THE TYPE OF TRANSACTION:"; cout<<"\n0-NO TRANSACTION"; cout<<"\n 1-DEPOSIT"; cout<<"\n 2-WITHDRAW"; cin>>ch; if(ch==0) c.display(); else if(ch==1) c.deposit(); else if(ch==2) c.withdraw(); getch(); }

OUTPUT: ENTER THE TYPE OF TRANSACTION: 0-NO TRANSACTION 1-DEPOSIT 2-WITHDRAW1 ENTER DEPOSITOR NAME:HARI ENTER ACCOUNT NUMBER:987654321 ENTER ACCOUNT TYPE:1 ENTER THE INITIAL AMOUNT:8000 CURRENT STATUS OF YOUR ACCOUNT:: NAME:HARI ACC_NO:987654321 BALANCE:8000 ENTER THE AMOUNT TO BE DEPOSITED:800 TOTAL AMOUNT:8800

RESULT: Thus the program is executed and output is verified using C++.

Ex. no.2 a)
AIM:

CLASSES WITH STATIC MEMBERS

To write a program to implement classes with static members. ALGORITHM: Step I - Define a class item Step II - Initialize the static data member count and integer member Step III - Member functions are get data and get count Step IV - Define the static data member outside the class Step V - Terminate the program SOURCE CODE: #include<iostream.h> #include<conio.h> class item { static int count; int no; public: void getdata(int a) { no-a; count++; } void getcount() { cout<<count <<count; } }; int item::count; void main() { item a; clrscr(); a.getcount(); a.getdata(100);

item b; b.getcount(); b.getdata(200); getch(); }

OUTPUT:

count 1 count 2

RESULT: Thus the program is executed and output is verified using C++.

Ex.no.2 b) DEFAULT ARGUMENT

AIM: To write a program to implement classes with default argument. ALGORITHM: Step I Step II Step III Step IV Step V Step VI Declare the default argument - Define the function with default argument - Write the main function - Give the function with arguments - Obtain the volume Terminate the program

SOURCE CODE: #include<iostrem.h> #include<conio.h> void vol(float l,float b=2.3,float h=4.2) { float c=l*b*h; cout<<volume: <<c<<endl; } void main() {clrscr(); vol(1.2,5.1,1.1); vol(2.8,3.9); vol(4.0); getch(); }

OUTPUT: volume:6.732 volume:45.863998 volume:38.639996

RESULT: Thus the program is executed and output is verified using C++.

Ex. no. 2 c) FRIEND FUNCTIONS

AIM: To write a program to implement the concept of Friend Function.

ALGORITHM: Step I - Define a class named DM and DB DM distance in meter and centimeter DB distance in feet and inches Step II - Member data present in the class DM are i) meter ii) centimeter Step III - Member functions present in class DB are i) for initializing the DM object properly ii) for reading a DM object iii) for displaying the DM object Step IV - Member data present in the DB class are i) feet ii) inches Step V - Member functions present in class DB are i) for initializing the DB object properly ii) for reading a DB object iii) for displaying the DB object Step VI Step VII Step VIII Step IX - Include a function that adds a DM and DB object - Declare objects for class DB and DM - Exercise the routines - Terminate the program

SOURCE CODE: #include<iostream.h> #include<conio.h> class tom { int x,y; public:

void read() {cout<<Enter X value:; cin>>x; cout<<Enter Y value:; cin>>y; } void display() { cout<<"THE VALUE OF X IS "<<x; cout<<"\n THE VALUE OF Y IS "<<y; } friend jerry; }; class jerry { public: int s; void sum(tom a) { s=a.x+a.y; cout<<"\n THE SUM IS "<< s; } }; void main() { clrscr(); tom a; jerry c; a.read(); a.display(); c.sum(a); getch(); }

OUTPUT: Enter X value: 3 Enter Y value: 4 THE VALUE OF X IS 3 THE VALUE OF Y IS 4 THE SUM IS 7

RESULT: Thus the program is executed and output is verified using C++.

Ex.no. 3

COMPLEX NUMBERS OPERATOR OVERLOADING

AIM: To write a program to implement the concept of Complex numbers in Operator Overloading

ALGORITHM: Step I - Define a class complex Step II - Member data present in the class are real and image Step III - Member functions present in class are i) add data ii) operator overloading Step IV Get the data of complex nos c1 and c2 Step V - Then the operation of complex addition c3=c1+c2 takes place Step VI - Then the operation of complex subtraction takes place c3=c1-c2 Step VII - Then the operation of complex mul and div Step VIII - Terminate the program

SOURCE CODE: #include<iostream,.h> #include<conio.h> Class complex { private: float real,image; public: complex() { real=image=0; } void getdata() { cout<<real part; cin>>real;

cout<<imaginary part; cin>>image; } void outdata(char *msg) { cout<<msg; cout<<(<<real<<,<<image<<); } complex operator +(complex c2); complex operator -(complex c2); complex operator *(complex c2); complex operator /(complex c2); }; complex complex::operator +(complex c2) { complex temp; temp.real=real+c2.real; temp.image=image+c2.image; return temp; } complex complex::operator -(complex c2) { complex temp; temp.real=real-c2.real; temp.image=image-c2.image; return temp; } complex complex::operator *(complex c2) { complex temp; temp.real=real*c2.real-image *c2.image; temp.image= real*c2.image-+mage *c2.real; return temp; } complex complex::operator /(complex c2) { complex temp; float qt; qt=(c2.real*c2.real)+(c2.image*c2.image); temp.real=(real*c2.real+image *c2.image)/qt;

temp.image= (image*c2.real+real *c2.image)qt; return temp; } void main() { complex c1,c2,c3; clrscr(); cout<<enter complex number<<endl; c1.getdata(); cout<<enter c2<<endl; c2.getdata(); cout<<complex number are<<endl; c1.outdata(c2=); c2.outdata(c2=; c3=c1+c2; c3.outdata(\n c3=c1+c2); c3=c1-c2; c3.outdata(\nc3=c1*c2); c3=c1/c2; c3.outdata(\n c3=c1/c2); getch(); }

OUTPUT: enter complex number real part 3 imaginary part 6 enter c2 real part 5 imaginary part 7 complex number are c1=(3,6)c2=(5,7) c3=c1+c2(8,13) c3=c1-c2(-2,-1) c3=c1*c2(-27,51) c3=c1/c2(0.77027,0.689189)

RESULT: Thus the program is executed and output is verified using C++.

Ex.no.4 MATRIX MULTIPLICATION USING DYNAMIC MEMORY ALLOCATION


AIM: To write a program to implement the Matrix multiplication using Dynamic Memory Allocation ALGORITHM: Step I - Define a class Matrix Step II - Member data present in the class are row,column,*p Step III - Member functions present in class are i) initialize the matrix object ii) read the matrix object iii) for displaying iv) for adding v) For subtracting vi) for multiplication Step IV declare objects for class matrix Step V - exercise the execution Step VI - Terminate the program SOURCE CODE: #include<iostream.h> #include<conio.h> class matrix { private: int row, col; int *p[10]; public: matrix() { row=col=0; *p=NULL; } matrix(int r, int c) {

row =r; col=c; *p=new int[row]; for(int i=0;i<row;i++) { p[i]=new int[col]; }} ~matrix() { for(int i=0;i<row;i++) delete p[i]; delete p; } void add(matrix &a,matrix &b) { int i,j; row=a.row; col=a.col; for(i=0;i<row;i++) for(j=0;j<col;j++) p[i][j]=a.p[i][j] +b.p[i][j]; } void sub(matrix &a,matrix &b) { int i,j; row=a.row; col=a.col; for(i=0;i<row;i++) for(j=0;j<col;j++) p[i][j]=a.p[i][j] - b.p[i][j]; } int mul(matrix &a,matrix &b) { int i,j,k; row=a.row; col=a.col; if(a.row!=b.col) { cout<<"\n error";

return 1; } for(i=0;i<row;i++) for(j=0;j<col;j++) { p[i][j]=0; for(k=0;k<col;k++) p[i][j]+=a.p[i][k] * b.p[k][j]; } return 0; } void read() { int i,j; for(i=0;i<row;i++) { for(j=0;j<col;j++) { cout<<"matrix["<<i+1<<","<<j+1<<"]="; cin>>p[i][j]; } }} void show() { int i,j; for(i=0;i<row;i++) { cout<<"\n"; for(j=0;j<col;j++) { cout<<p[i][j]<<"\t"; }} }}; void main() { int m,n,er; clrscr(); cout<<"how many rows"; cin>>m; cout<<"how many cols"; cin>>n;

cout<<"enter matrix A"; matrix A(m,n); A.read(); cout<<"enter matrix B"; matrix B (m,n); B.read(); cout<<"Matrix A"; A.show(); cout<<"Matrix B"; B.show(); matrix C(m,n); C.add(A,B); cout<<"\n C = A+B"; C.show(); C.sub(A,B); cout<<"\n C = A-B"; C.show(); C.mul(A,B); cout<<"\n C = A*B"; C.show(); getch(); }

OUTPUT: how many rows2 how many cols3 enter matrix Amatrix[1,1]=1 matrix[1,2]=2 matrix[1,3]=3 matrix[2,1]=4 matrix[2,2]=5 matrix[2,3]=6 enter matrix Bmatrix[1,1]=5 matrix[1,2]=6 matrix[1,3]=7 matrix[2,1]=8 matrix[2,2]=9 matrix[2,3]=1 Matrix A 1 2 3 4 5 6 Matrix B 5 6 7 8 9 1 C = A+B 6 8 10 12 14 7 C = A-B -4 -4 -4 -4 -4 5 error C = A*B -4 -4 -4 -4 -4 5

RESULT: Thus the program is executed and output is verified using C++.

Ex.no. 5 OVERLOADING ASSIGNMENT OPERATOR


AIM: To write a program to implement Assignment Operator Overloading ALGORITHM: Step I - Declare the array of elements Step II - Create the function as operator Step III Use for loop to enter the elements Step IV Call function using resolution operator Step V - Declare the array element a & b in main Step VI - Terminate the program SOURCE CODE:

#include<iostream.h> #include<conio.h> class sample { private: int ele[3][3]; public: sample(int tem[3][3]); void operator=(sample abc); void display(); }; sample::sample(int tem[3][3]) { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { ele[i][j]=tem[i][j]; } cout<<"\n"; } } void sample::operator=(sample abc) {

int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { ele[i][j]=abc.ele[i][j]; } } } void sample::display() { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { cout<<ele[i][j]<<"\t"; } cout<<"\n"; } } void main() { int a[3][3] = {1,2,3,4,5,6,7,8,9}; int b[3][3] = {4,5,6,7,8,9,1,2,3}; clrscr(); sample obj(a); sample obj1(b); cout<<"\n contents of first"; obj.display(); cout<<"\n overlading the assignment operator \n"; obj=obj1; cout<<"\n content of first"; obj.display(); cout<<"\n contents of second\n"; obj1.display(); getch(); }

OUTPUT:

contents of first 1 2 3 4 5 6 7 8 9 overlading the assignment operator content of first 4 5 6 7 8 9 1 2 3 contents of second 4 5 6 7 8 9 1 2 3

RESULT: Thus the program is executed and output is verified using C++.

Ex.no. 6 LINK LIST USING TEMPLATES


AIM: To write a program to implement link list using templates ALGORITHM: Step I - Start the program Step II - Create a template to implement linklist Step III class has function called create() ,retrieve() ,delete() Step IV Create object for the class Step V - Terminate the program SOURCE CODE:

#include<iostream.h> #include<conio.h> #include<stdlib.h> template <class t> class node { public: t data; int position; node *next; node() { position=0; } int create(); int insert(); int del(); int locate(); int retrieve(); int display(); node *head; }; template<class t> int node<t>::create()

{ head=new node; head->next=NULL; return 0; } template<class t> int node<t>::insert() { int n,i; t da; node* newptr; newptr=new node; node *temp; temp=head; cout<<"\n enter the position b/w 1 &"<<(position+1)<<"is"; cin>>n; cout<<"enter the data:"; cin>>da; if(head->next==NULL) { newptr->data=da; newptr->next=NULL; head->next=newptr; position++; } else { for(i=1;i<n&&temp->next!=NULL;i++) temp=temp->next; if(temp->next!=NULL) { newptr->data=da; newptr->next=temp->next; temp->next=newptr; position++; } else { newptr->data=da; newptr->next=NULL;

temp->next=newptr; position++; } } return 0; } template<class t> int node<t>::del() { t x; node *temp, *delptr; t deldata; cout<<" enter the data to be deleted\n"; cin>>x; temp=head; if(head->next==NULL) { cout<<"the list is empty"; return 0; } while(temp->next->next!=NULL) { if(temp->next->data==x) { delptr=temp->next; deldata=temp->next->data; temp->next=temp->next->next; cout<<"the ele"<<deldata<<"is deleted"; delete delptr; position--; return 0; } temp=temp->next; } if(temp->next->data==x) { delptr=temp->next; deldata=temp->next->data; temp->next=NULL;

cout<<"the ele"<<deldata<<"is deleted"; delete delptr; position--; return 0; } else cout<<"ele is not found"; return 0; } template<class t> int node<t>::display() { node *temp; temp=head; if(head->next==NULL) { cout<<"list is empty"; return 0 ; } cout<<"head->"; while(temp->next!=NULL) { cout<<temp->next->data; cout<<"->"; temp=temp->next; } cout<<"NULL"; return 0; } template<class t> int node<t>::locate() { t x; node *temp; int i=0; temp=head; cout<<"enter the element"; cin>>x; while(temp->next!=NULL) {

i++; if(temp->next->data==x) { cout<<"position of ele"<<x<<"is"<<i; return 0; } temp=temp->next; } cout<<"ele not found"; return 0; } template<class t> int node<t>::retrieve() { node *temp; int p,i=1; temp=head; cout<<"enter the position"; cin>>p; while(temp->next!=NULL) { if(i==p) { cout<<"ele at the position"<<p<<"is"<<temp->next->data; return 0; } temp=temp->next; i++; } cout<<"position is not present"; return 0; } void main() { int choice; clrscr(); cout<<"it is an int template\n"; node<int>s; node<float>s1; s.create();

cout<<"1.insert\n 2.delete\n 3.locate\n 4.retrieve\n 5.display\n 6.exit"<<endl; do { cout<<"enter the choice:"; cin>>choice; switch(choice) { case 1: s.insert(); break; case 2: s.del(); break; case 3: s.locate(); break; case 4: s.retrieve(); break; case 5: s.display(); break; default:cout<<"exited"; } } while(choice<=5); cout<<"it is an float template"; s1.create(); cout<<"\n 1.insert\n 2.delete\n 3.locate\n 4.retrieve\n 5.display\n 6.exit"<<endl; do { cout<<"enter the choice"; cin>>choice; switch(choice) { case 1: s1.insert(); break; case 2: s1.del();

break; case 3: s1.locate(); break; case 4: s1.retrieve(); break; case 5: s1.display(); break; case 6: cout<<"exited"; exit(0); } } while(choice<=6); getch(); }

OUTPUT: it is an int template 1.insert 2.delete 3.locate 4.retrieve 5.display 6.exit enter the choice:1 enter the position b/w 1 &1is1 enter the data:2 enter the choice:1 enter the position b/w 1 &2is2 enter the data:4 enter the choice:1 enter the position b/w 1 &3is3 enter the data:6 enter the choice:2 enter the data to be deleted 4 the ele4is deletedenter the choice:3 enter the element2 position of ele2is1enter the choice:4 enter the position3 position is not presententer the choice:4 enter the position2 ele at the position2is6enter the choice:5 head->2->6->NULLenter the choice:6

RESULT: Thus the program is executed and output is verified using C++.

Ex.no. 7 EXCEPTION HANDLING


AIM: To write a program to implement exception handling ALGORITHM: Step I Step II Step III Step IV Step V - Start the program - Create a class called number and another class called divide - class has function called read(),div() - keyword such as try, catch and throw - Terminate the program

SOURCE CODE: #include<stdio.h> Class number { Private: int num; public: void read() { Cin>>num; } Class divide { }; Int div(number num2) { If(num=num==throw divde(); else return num/num2.num } }; Int main() { number num1,num2;int result;

cout<<enter number1;num1.read(); cout<<enter number2;num2.read(); try { Cout<<trying div op; Result=num1.div(num2); Cout<.<succeeded<<endl; } Catch(number::divide) { Cout<<failed<<endl; Cout<<exception divide by zero; Return1; } Cout<<num1/num2=<<result; Return0; }

OUTPUT:

RESULT: Thus the program is executed and output is verified using C++.

Ex.no 8 RUNTIME POLYMORPHISM


AIM: To write a program to implement Runtime Polymorphism. ALGORITHM: Step I Step II Step III Step IV Step V Step VI - Start the program - Create a class called shape with data member area. - Class has function called shape (), display () - Create a derived class circle, rectangle, and triangle. - Each derived class passing an argument as dimension. -Display circle, triangle and rectangle dimension.

SOURCE CODE: #include<iostream.h> #include<conio.h> #include<string.h> class shape { protected: int d1; float area; public: shape(int dimension) { d1=dimension; } virtual void display() { }}; class circle:public shape { float pi; public: circle(int dimension,float pivalue):shape(dimension) {

pi=pivalue; } void display(); }; class rectangle:public shape { int d2; public: rectangle(int dimension, int dimension2):shape(dimension) { d2=dimension2; } void display(); }; class triangle:public shape { int d3; public: triangle(int dimension,int dimension3):shape(dimension) { d3=dimension3; } void display(); }; void circle::display() { cout<<"\n radius:"<<d1; area=d1*pi; cout<<"\n area:"<<area; } void rectangle::display() { cout<<"\n length:"<<d1; cout<<"\n bradth:"<<d2; area=d1*d2; cout<<"\n area:"<<area; } void triangle::display() { cout<<"\n base:"<<d1;

cout<<"\n height:"<<d3; area=0.5*(d1*d3); cout<<"\n area:"<<area; } void main() { int dim1,dim2; clrscr(); cout<<"\n enter the dimension for circle"; cout<<"\n radius"; cin>>dim1; circle circle1(dim1,3.14); cout<<"\n\nenter the dimension for rectangle:"; cout<<"\n\nlength:"; cin>>dim1; cout<<"\nbreath:"; cin>>dim2; rectangle recl1(dim1,dim2); cout<<"\n enter the dim for the triangle:"; cout<<"\n base"; cin>>dim1; cout<<"\n height:"; cin>>dim2; triangle tri1(dim1,dim2); shape*list[3]; list[0]=&circle1; list[1]=&recl1; list[2]=&tri1; cout<<"\n\n circle"; list[0]->display(); cout<<"\n rectangle"; list[1]->display(); cout<<"triangle"; list[2]->display(); getch(); }

OUTPUT: enter the dimension for circle radius2

enter the dimension for rectangle: length:3 breath:6 enter the dim for the triangle: base5 height:4

circle radius:2 area:6.28 rectangle length:3 bradth:6 area:18triangle base:5 height:4 area:10

RESULT: Thus the program is executed and output is verified using C++.

Ex. no 9 MANAGEMENT OF BANK ACCOUNTS USING INHERITANCE


AIM: To write a C++ program to manage the Bank Accounts of a customer. ALGORITHM: Step I - Start the program Step II - Create a base class to get the required details from the customers. Step III - Create three data members for the base class- name,acc_no,acc_type. Step IV - Create a derived class with three member variables- balance,deposit and withdraw. Step V - Create the following member functions under the public section of the derived class. a) A default constructor to initialize the data members . b) A function deposit_() to deposit the money and display balance. c) A function withdraw_() to withdraw money and display the remaining balance d) A function balance_() to show balance of the customer during each transaction. Step VI - Define the main() function and create an object for the derived class. Step VII - Create a dowhile() loop to accept the users choice and display balance accordingly. Step VIII - The functions are called using the objects to exercise the routines. Step IX - Terminate the program.

SOURCE CODE:

#include<iostream.h> #include<conio.h> class base { protected: char name[20], acc_type[10]; int acc_no; public: void getdata() { cout<<"\nEnter the name: "; cin>>name; cout<<"\nEnter the account type: "; cin>>acc_type; cout<<"\nEnter the account number: "; cin>>acc_no; } }; class derived: public base { private: float balance, deposit, withdraw; public: derived() { deposit=0.0; balance=0.0; withdraw=0.0; } void balance_() { cout<<"\nName: "<<name; cout<<"\nAccount number: "<<acc_no; cout<<"\nAccount type: "<<acc_type;

cout<<"\nYour balance: "<<balance; } void deposit_() { cout<<"\nEnter the amount to be deposited: "; cin>>deposit; balance=balance+deposit; balance_(); } void withdraw_() { cout<<"\nEnter the amount to be withdrawn: "; cin>>withdraw; if(withdraw<=balance) { balance=balance-withdraw; balance_(); } else cout<<"\ntransaction is not possible!"; } }; void main() { int ch=0; clrscr(); derived d; d.getdata(); do { cout<<"\nMAIN MENU\n1. Deposit\n2. Withdraw\n3. Balance\n4. Exit. "; cout<<"\nEnter your choice: "; cin>>ch; switch(ch) { case 1: d.deposit_(); break; case 2: d.withdraw_(); break; case 3: d.balance_();

break; case 4: cout<<"\nExit!"; getch(); break; default: cout<<"\nEnter the correct choice: "; } }while(ch<4); getch(); }

OUTPUT:

Enter the name: Raj Enter the account type: joint Enter the account number: 163 MAIN MENU 1. Deposit 2. Withdraw 3. Balance 4. Exit. Enter your choice: 1 Enter the amount to be deposited: 1500 Name: Raj Account number: 163 Account type: joint Your balance: 1500 MAIN MENU 1. Deposit 2. Withdraw 3. Balance 4. Exit. Enter your choice: 2 Enter the amount to be withdrawn: 250 Name: Raj Account number: 163 Account type: joint Your balance: 1250 MAIN MENU 1. Deposit 2. Withdraw 3. Balance 4. Exit.

Enter your choice: 3 Name: Raj Account number: 163 Account type: joint Your balance: 1250 MAIN MENU 1. Deposit 2. Withdraw 3. Balance 4. Exit. Enter your choice: 4 Exit!

RESULT: Thus the program is executed and output is verified using C++.

EX.NO 10 JAVA CLASS PROGRAM WITH JAVADOC


AIM: To create simple Java program with JAVADOC. ALGORITHM: Step I Step II Step III Step IV Step V - Start the program - Create a class in Java. - Display the message. - Comments must be given appropriately. -Close the program.

SOURCE CODE: /** * The HelloWorldApp class implements an application that * simply prints "Hello World!" to standard output. */ class helloworldapp { public static void main(String[] args) { System.out.println("hello world!"); } }

OUTPUT:

hello world!

RESULT: Thus the program is executed and output is verified using Java.

Ex.no.11 IMPLEMENTATION OF PACKAGES


AIM: To implement the concept of packages using Java. ALGORITHM: Step I Step II - Start the program - Create a package P1 containing the class student .Class student has three data members- name,sub1.sub2 and two methods: a) The first method is getdata() function which is used to assign values to the data members. b) The second method is display() function which is used to display the name and the marks of the student in sub1 and sub2. - Create another package P2 containing the class sports.Class sports has two data members height and weight and single method.This method getdata() is used to get the values for height and weight. - Import the packages P1 and P2. - Create class test to define the main() method. -Create object for the classes student and sports and print the student details. - Stop the program.

Step III

Step IV Step V Step VI Step VII

SOURCE CODE:

CREATION OF FIRST PACKAGE P1: package P1; public class student { String name; int sub1,sub2; public void getdata(String s, int s1,int s2) { name=s; sub1=s1; sub2=s2; } public void display() { System.out.println("Name: "+name+"\nSubject1:"+sub1+"\nSubject2:"+sub2); } }

CREATION OF SECOND PACKAGE P2: package P2; public class sports { double weight,height; public void getdata(double w,double h) { weight=w; height=h; } public void display() { System.out.println("Height:"+height+"feet"+"\nWeight:"+weight+"kg"); } } IMPORTING OF PACKAGES P1 & P2: import P1.*; import P2.*; class Test { public static void main(String args[]) { student t=new student(); sports p=new sports(); t.getdata("xyz",50,80); p.getdata(56,5.7); t.display(); p.display(); } }

OUTPUT:

Name: xyz Subject1:50 Subject2:80 Height:5.7feet Weight:56.0kg

RESULT: Thus the program is executed and output is verified using Java.

Ex.no 12 IMPLEMENTATION OF INTERFACE


AIM: To compute the area of a rectangle and a circle using the concept of interface. ALGORITHM: Step I Step II Step III - Start the program - Create a interface and define it with one constant data member- pi=3.14 and member method- compute(float x,float y). - Create a derived class rectangle which implements the interface area.In the derived class, define the method compute(float x,float y) and return the value(x*y). - Create another derived class circle which implements the interface area and here define the method compute() again and return the value (pi*x*x). - Create a class test to define the main() method. - In the main() definition create objects for each of the derived class and the functions are accessed through the objects. - Print the area of the rectangle and the circle. - Stop the program.

Step IV Step V Step VI Step VII Step VIII

SOURCE CODE:

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

} } class InterfaceTest { public static void main(String args[]) { Rectangle rect= new Rectangle(); Circle cir=new Circle(); Area area; area=rect; System.out.println("Area of the rectangle is :"+area.compute(100,20)); area=cir; System.out.println("Area of the rectangle is :"+area.compute(100,0)); } }

OUTPUT: Area of the rectangle is :2000.0 Area of the rectangle is :31400.0

RESULT: Thus the program is executed and output is verified using Java.

Ex.no 13 IMPLEMENTATION OF INHERITANCE


AIM: To implement the concept of inheritance using Java. ALGORITHM: Step I Step II Step III Step IV Step V Step VI Step VII Step IX - Start the program - Create a base class Box in Java with data members- width, height and depth. - Create member functions- getvolume(),default constructor- Box() and a parameterized contructor-Box(double w,double h,double d). - Create a derived class MatchBox with data members- weight. - Create a default constructor and a parameterized constructor. - A main function is created. - Create a object for the derived class MatchBox and print the details. - Stop the program.

SOURCE CODE:

class Box { double width; double height; double depth; Box() { } Box(double w, double h, double d) { width=w; height=h; depth=d; } void getVolume() { System.out.println(" Volume is : "+ width * height *depth); } } public class MatchBox extends Box { double weight;

MatchBox() { } MatchBox(double w,double h,double d,double m) { super(w,h,d); weight=m; } public static void main(String args[]) { MatchBox mb1= new MatchBox(10,120,130,140); mb1.getVolume(); System.out.println(" width of matchbox 1 is" + mb1.width); System.out.println(" height of matchbox 1 is " +mb1.height); System.out.println(" depth of matchbox 1 is " +mb1.depth); System.out.println(" weight of matchbox 1 is " +mb1.weight); } }

OUTPUT: Volume is : 156000.0 width of matchbox 1 is10.0 height of matchbox 1 is 120.0 depth of matchbox 1 is 130.0 weight of matchbox 1 is 140.0

RESULT: Thus the program is executed and output is verified using Java.

Ex.no. 14 IMPLEMENTATION OF EXCEPTION HANDLING


AIM: To implement the concept of exception handling using Java. ALGORITHM: Step I Step II Step III Step IV Step V Step VI Step VII Step VIII - Start the program - Create a class Multi_Catch. - Create a main() function. - Create variables- array[],num1,num2 and res. - Calculate the value of res and print. - Create a for loop and print the array values. - In case of exception,the catch() function is executed. - Terminate the program.

SOURCE CODE:

public class Multi_Catch { public static void main(String args[]) { int array[]={20, 10, 30}; int num1=15,num2=0; int res=0; try { res=num1/num2; System.out.println("The result is"+res); for(int ct=2;ct>=0;ct--) { System.out.println("The value of array are" +array[ct]); } } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Error.....Array is out of bounds"); } catch(ArithmeticException e)

{ System.out.println ("Can't be divided by Zero"); } } }

OUTPUT: Can't be divided by Zero

RESULT: Thus the program is executed and output is verified using Java.

Ex.no, 15 IMPLEMENTATION OF I/O


AIM: To implement the I/O operations in Java. ALGORITHM: Step I Step II Step III Step IV Step V Step VI Step VII Step VIII Step IX Step X - Start the program - Create a class CopyTextFile. - Create main() function. - Create File objects. - Input two file names- to read from,to write to. - Enclose in try..catch because of possible I/O exceptions. -Create rwo objects reader and writer for BufferedReader and BufferedWriter classes respectively. - Create while() loop. - Close reader and writer. - Close the program.

SOURCE CODE:

import java.io.*; import java.util.*; public class CopyTextFile { public static void main(String args[]) { System.out.println("Enter a filepath to a copy from, and one to copy to."); Scanner in=new Scanner(System.in); File inFile = new File(in.next()); File outFile = new File(in.next()); try { copyFile(inFile, outFile); } catch(IOException e) { System.err.println(e); System.exit(1);

} } public static void copyFile(File fromFile, File toFile) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(fromFile)); BufferedWriter writer = new BufferedWriter(new FileWriter(toFile)); String line = null; while((line=reader.readLine())!=null) { writer.write(line); writer.newLine(); } reader.close(); writer.close(); } } OUTPUT: Enter a filepath to a copy from, and one to copy to. helloworldapp.java nn.java

RESULT: Thus the program is executed and output is verified using Java.

Ex.no.16 IMPLEMENTATION OF MULTITHREADED PROGRAM


AIM: To implement the concept of multithreaded program in Java. ALGORITHM: Step I Step II Step III Step IV Step V Step VI Step VII Step VIII Step IX - Start the program - Create a class RunnableThread. - Create member function- run() and constructors-default and parameterized for the above class. - Create a new thread and start the thread. - Create another class RunnableExample. - Create main() function. - Create objects and start the threads. - Display info about the main thread. - Stop the program.

SOURCE CODE:

class RunnableThread implements Runnable { Thread runner; public RunnableThread() { } public RunnableThread(String threadName) { runner = new Thread(this, threadName); System.out.println(runner.getName()); runner.start(); } public void run() { System.out.println(Thread.currentThread()); } } public class RunnableExample {

public static void main(String[]args) { Thread thread1=new Thread(new RunnableThread(),"thread1"); Thread thread2=new Thread(new RunnableThread(),"thread2"); RunnableThread thread3= new RunnableThread("thread3"); thread1.start(); thread2.start(); try { Thread.currentThread().sleep(1000); } catch(InterruptedException e) { } System.out.println(Thread.currentThread()); } } OUTPUT: Thread3 Thread[thread3,5,main] Thread[thread1,5,main] Thread[thread2,5,main] Thread[main,5,main]

RESULT: Thus the program is executed and output is verified using Java.

You might also like