Function Overloading

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 48

FUNCTION OVERLOADING

#include<iostream.h> #include<conio.h> class student { int sqr,rect,tria; public: void area(int a); void area(int a,int b); void area(int a,int b,float c); }; void student::area(int a) { sqr=a*a; cout<< " The Area Of Square Is : "<<sqr<<"\n"; } void student::area(int a,int b) { rect=a*b; cout<< " The Area Of Rectangle Is : "<<rect<<"\n"; } void student::area(int a,int b,float c) { tria=a*b*c; cout<<" The Area Of Triangle Is : "<<tria<<"\n"; } void main() { clrscr(); student s; int a,b; float c=0.5; cout<< " Enter the two values \n" ; cin>>a>>b; s.area(a); s.area(a,b); s.area(a,b,c); getch(); }

OUTPUT
Enter The Two Values : 10,20 The Area Of Square Is : 100 The Area Of Rectangle Is : 200 The Area Of Triangle Is : 100

DEFAULT ARGUMENT
#include<iostream.h> #include<conio.h> class defarg { int p,n,r; float si,amount; public: void getdata(int p,int n,int r=10); }; void defarg::getdata(int p,int n,int r) { si=(p*n*r)/100; amount=amount+si; cout<<Simple Interest is<<si<< "\n"<<Amount is<<amount; } void main() { clrscr(); defarg s; s.getdata(10,5); getch(); }

OUTPUT
Simple Interest is 5 Amount is 5

PROGRAM USING CLASS


#include<iostream.h> #include<conio.h> class sumavg { int a,b; public : void getdata(void); void calculate(void); void display(void); } void sumavg::getdata(void) { cout<<"ENTER THE NUMBER"; cin>>a>>b; } void sumavg::calculate(void) { sum=a+b; avg=sum/2; } void sumavg::display(void) { cout<<"SUM"<<sum; cout<<"AVERAGE"<<avg; void main() { sumavg s; s.getdata(); s.calculate(); s.display(); }

OUTPUT
ENTER THE NUMBER: 10 20 SUM 30 AVERAGE 15

DYNAMIC MEMORY ALLOCATION


#include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> class dma { int len; char *p; public: dma() { len=0; p=new char[len+1]; } dma(char *s) { len=strlen(s); p=new char[len=1]; strcpy(p,s); } dma(dma &a,dma &b) { len=a.len+b.len; p=new char[len+1]; strcpy(p,a.p); strcat(p,b.p); } void display() { cout<<"\n"<<p; } }; void main() { char *s; char *a; clrscr(); cout<<"ENTER THE STRING"; cin>>s; cout<<"ENTER THE STRING"; cin>>a; dma n1(s); dma n2(a); dma n3(n1,n2); n3.display(); getch(); }

OUTPUT
ENTER THE STRING OBJECTORIENTED ENTER THE STRING PROGRAMMING OBJECTORIENTEDPROGRAMMING

COPY CONSTRUCTOR
#include<iostream.h> #include<conio.h> class student { int i,j; public: student() { i=0; j=0; } student(int a,int b) { i=a; j=b; } student(student&a) { i=a.i; j=a.j; } void display() { cout<<" THE VALUE OF I IS : "<<i<<endl; cout<<" THE VALUE OF J IS : "<<j<<endl; } }; void main() { clrscr(); student s(10,20); cout<<OBJECT S; s.display(); student s1(s); cout<<OBJECT S1; s1.display(); getch(); }

//call to copy constructor

OUTPUT
OBJECT S THE VALUE THE VALUE OBJECT S1 THE VALUE THE VALUE OF I IS 10; OF J IS 20; OF I IS 10; OF J IS 20;

DESTRUCTOR
#include<iostream.h> #include<conio.h> class stud { public: int rollno; stud(){} void get() { cin>>rollno; } void print() { cout<<"Roll number is"<<rollno<<"\n"; } ~stud() { cout<<"Object Destroyed"<<"\n"; } }; void main() { { cout<<"Pgm start"<<"\n"<<Scope1 start; stud s1,s2,s3; s1.get(); s1.print(); s2.get(); s2.print(); s3.get(); s3.print(); cout<<"Scope1 end"<<"\n"; } cout<<"New Object"<<"\n"<<Scope2 start; stud s4; s4.get(); s4.print(); { cout<<"New Object2 "<<"\n"<<Scope3 start; stud s5; s5.get(); s5.print(); cout<<"Scope3 end"<<"\n"; } cout<<"Pgm end"<<"\n"; cout<<Scope2 end; getch(); }

OUTPUT
Pgm start Scope1 start 10 Roll number is 10 15 Roll number is 15 20 Roll number is 20 Scope1 end Object destroyed Object destroyed Object destroyed New object Scope2 start 30 Roll number is 30 New object2 Scope3 start 35 Roll number is 35 Scope3 end Object destroyed Pgm end Scope2 end Object Destroyed

OPERATOR OVERLOADING
#include<iostream.h> #include<conio.h> class OP { int a; public: OP() { } OP(int k) { a=k; } OP operator + (OP n) { OP o; o.a=a+n.a; return o; } OP operator - (OP n) { OP o; o.a=a-n.a; return o; } OP operator * (OP n) { OP o; o.a=a*n.a; return o; } OP operator / (OP n) { OP o; o.a=a/n.a; return o; } void display() { cout<<a<<endl; }; void main() { int a,b; cout<<ENTER THE NUMBERS:; cin>>a>>b; OP o1(a); OP o2(b); OP o3,o4,o5,o6; o3=o1+o2;

o4=o1-o2; o5=o1*o2; o6=o1/o2; cout<<THE SUM IS<<endl; o3.display(); cout<<THE DIFFERENCE IS<<endl; o4.display(); cout<<THE PRODUCT IS<<endl; o5.display(); cout<<THE QUOTIENT IS<<endl; o6.display(); getch(); }

OUTPUT
ENTER THE NUMBERS: 10 5 THE SUM IS 15 THE DIFFERENCE IS 5 THE PRODUCT IS 50 THE QUOTIENT IS 2

OPERATOR OVERLOADING USING FRIEND FUNCTION


#include<iostream.h> #include<conio.h> class stud { int r,t,m; public: friend ostream &operator<<(ostream &,stud&) friend istream &operator>>(istream&,stud&) }; ostream & operator<<(ostream & tout,stud&s) { tout<<"Roll Number is"<<s.r<<endl; tout<<"Mark is"<<s.m<<endl; tout<<"Total is"<<s.t<<endl; return tout; } istream & operator>>(istream & tin,stud &s) { cout<<"Enter roll number"; tin>>s.r; cout<<"Enter the mark"; tin>>s.m; cout<<"Enter the total mark"; tin>>s.t; return tin; } void main() { stud stud1; cin>>stud1; cout<<"student data"; cout<<stud1<<"end"; getch(); }

OUTPUT
Enter roll number 1 Enter the mark 50 Enter the total mark 200 Roll Number is 1 Mark is 50 Total is 200

ASSIGNMENT OPERATOR OVERLOADING


#include<iostream.h> #include<conio.h> class stud { public: int rollno; void operator=(int q) { rollno=q; } void display() { cout<<"Roll Number is "<<rollno; }}; void main() { clrscr(); stud s2; s2=5; s2.display(); getch(); }

OUTPUT
Roll Number is 5

DATA TYPE CONVERSION


#include<iostream.h> #include<conio.h> class first { public: double a,b; first(){} first(double p,double q) { a=p; b=q; } double geta() { return a; } double getb() { return b; } void show() { cout<<a<<"\n"<<b; } }; class second { double x,y; public: second(){} second(double p,double q) { x=p; y=q; } second(first f) { double s=f.geta(); double t=f.getb(); x=s+t; y=s-t; } operator first() { double s=x+y; double t=x-y; return first(s,t); }

void show() { cout<<x<<"\n"<<y<<"\n"; }}; void main() { clrscr(); first f1(20,20); second s1(10,10); first f2; second s2; s2=f1; s2.show(); f2=s1; f2.show(); getch(); }

OUTPUT
40 0 20 0

INHERITANCE
#include<iostream.h> #include<conio.h> class stud { public: int roll; char name; void getdata() { cout<< ENTER THE NAME:; cin>>name; cout<<ENTER THE ROLL NUMBER:; cin>>roll; } void show() { cout<< NAME IS<<name<<endl; cout<< ROLL NUMBER IS<<roll<<endl; } }; class intm:virtual public stud { public: int m1; void getdata1() { cout<< ENTER THE MARK1:; cin>>m1; } void show1() { cout<<"MARK1 IS"<<m1<<"\n"; } }; class extm:virtual public stud { public: int m2; void getdata2() { cout<< ENTER THE MARK2:; cin>>m2; } void show2() { cout<<"MARK2 IS"<<m2<<"\n"; } }; class tot:public intm,public extm {

public: int tot; void total() { tot=m1+m2; cout<< THE TOTAL MARKS IS; cout<<tot<<"\n"; } }; void main() { clrscr(); tot t1; t1.getdata(); t1.getdata1(); t1.getdata2(); t1.show(); t1.show1(); t1.show2(); t1.total(); getch(); }

OUTPUT
ENTER THE NAME: AAA ENTER THE ROLL NUMBER: 01 ENTER THE MARK1: 90 ENTER THE MARK2: 99 NAME IS AAA ROLL NUMBER IS 01 MARK1 IS 90 MARK2 IS 99 THE TOTAL MARKS IS 189

RUNTIME POLYMORPHISM
#include<iostream.h> #include<conio.h> class base { public: virtual void show() { cout<<"Base"<<"\n"; } }; class derived:public base { public: void show() { cout<<"Derived"<<"\n"; } }; void main() { clrscr(); base b,*ptr_b; derived d,*ptr_d; b.show(); d.show(); ptr_b=&b; ptr_d=&d; ptr_b->show(); ptr_d->show(); ptr_b=&d; ptr_b->show(); getch(); }

OUTPUT
Base Derived Base Derived Derived

CLASS TEMPLATE
#include<iostream.h> #include<conio.h> template<class t,class t1> class stack { t stkpointer; t1 stkarray[10]; public: stack() { stkpointer=0; } void push(t1 value) { if(stkpointer==9) cout<<" stack overflow"; else { stkarray[stkpointer]=value; stkpointer++; } } t1 pop() { if(stkpointer==0) cout<<" Stack empty "; else { stkpointer--; return stkarray[stkpointer]; } } void display() { int i; for(i=0;i<stkpointer;i++) cout<<stkarray[i]<<endl; } void search(int k) { for(int j=0;j<stkpointer;j++) { if(stkarray[j]==k) cout<<" ELEMENT FOUND AT POSITION "<<j<<endl; else cout<<" NOT FOUND"<<endl; } } };

void main() { clrscr(); int s; stack<int,int> s1; s1.push(5); s1.push(6); s1.push(7); cout<<ELEMENTS IN STACK<<endl; s1.display(); cout<<ELEMENT POPPED<<endl; cout<<s1.pop(); s1.display(); cout<<" ENTER THE VALUE TO BE SEARCHED : "; cin>>s; s1.search(s); getch(); }

OUTPUT
Elements in stack 5 6 7 Element popped:7 Elements in stack 5 6 ENTER THE VALUE TO BE SEARCHED: 5 ELEMENT FOUND AT POSITION 1

FUNCTION TEMPLATE
#include<iostream.h> #include<conio.h> template <class A> void bubble(A a[],int n) { for(int i=0;i<n-1;i++) for(int j=n-1;j>i;j--) { if(a[j]<a[j-1]) { A a1; a1=a[j]; a[j]=a[j-1]; a[j-1]=a1; } } } void main() { clrscr(); int r[5]={4,3,5,2,1}; float s[5]={4.4,3.3,5.5,2.2,1.1}; bubble(r,5); bubble(s,5); cout<<THE SORTED VALUES ARE; for(int i=0;i<5;i++) cout<<"\n"<<r[i]; for(int j=0;j<5;j++) cout<<"\n"<<s[j]; getch(); }

OUTPUT
THE SORTED VALUES ARE: 1,2,3,4,5 1.1,2.2,3.3,4.4,5.5.

EXCEPTION HANDLING
#include<iostream.h> #include<conio.h> class exception { public: int n; } int main() { int ch; cout<<Enter Your Choice" cout<<1.Integer\n2.Character\n3.DecimalNumber\n4.Obbject; cin>>ch; try { switch(ch) { case 1: cout<<enter a integer; cin>>a throw a; case 2: cout<<enter a character; cin>>b; throw b; case 3: cout<<enter a decimal number; cin>>c; throw c; case 4: cout<<Enter a value for member variable; cin>>e.n; throw e.n; } } catch(int a) { cout<<a<<"Integer exception caught"; } catch(char a) { cout<<a<<Character exception caught; } catch(double d) { cout<<a<<Decimal exception caught; } catch(exception e) {

cout<<e.n<<Object exception caught; } }

OUTPUT
Enter ur choice 1.Integer 2.Character 3.Decimal Value 4.Object 1 enter a integer 5 5 Integer Exception Caught 2 enter a character a a Character Exception Caught 3 enter a decimal value 5.3 5.3 Decimal Exception Caught 4 Enter a value for member variable 100 100 Object Exception Caught

FORMATTING USING IOS FUNCTIONS


#include<iostream.h> #include<conio.h> int main() { cout.fill(*); cout.setf(ios::left|ios::adjustfield) cout.width(10); cout<<Value; cout.setf(ios::right|ios::adjustfield); cout.width(15); cout<<Square Root; cout.fill(.; cout.precision(4); cout.setf(ios::showpos|ios::showpoint); cout.setf(ios::fixed|ios::floatfield); for(int i=1;i<=5;i++) { cout.setf(ios::internal|ios::adjustfield); cout.width(5); cout<<i; cout.setf(ios::right|ios::adjustfield); cout.widht(20); cout<<sqrt(i); } getch(); return 0; }

OUTPUT
Value*********Square Root +...1...............+1.0000 +...2...............+1.4142 +...3...............+1.7321 +...4...............+2.0000 +...5...............+2.2361

FORMATTING USING MANIPULATORS

#include<iostream.h> #include<conio.h> #include<iomanip.h> int main() { cout<<setw(15); cout<<setiosflags(ios::left|ios::fixed); cout<<setprecision(2); cout<<setiosflags(ios::showpoint); cout<<Roll Number; cout<<setw(15)<<Name; cout<<setw(10)<<setprecision(2)<<Marks<<endl; cout<<setw(15)<<1; cout<<setw(15)<<AAAA; cout<<setw(10)<setprecision(2)<<355.50<<endl; cout<<setw(15)<<2; cout<<setw(15)<<BBBB; cout<<setw(10)<<setprecision(2)<<275.00<<endl; cout<<setw(15)<<3; cout<<setw(15)<<CCCC; cout<<setw(10)<<setprecision(2)<<290.75<<endl; cout<<setw(15)<<4; cout<<setw(15)<<DDDD; cout<<setw(10)<<setprecision(2)<<270.0<<endl; cout<<setw(15)<<5; cout<<setw(15)<<EEEE; cout<<setw(10)<<setprecision(2)<<200.30<<endl; cout<<setfill(*)<<\n; getch(); return 0; }

OUTPUT
Roll Number Name Marks 1 AAAA 355.50 2 BBBB 275.00 3 CCCC 290.75 4 DDDD 270.00 5 EEEE 200.60 **************************************

STANDARD TEMPLATE LIBRARY VECTOR CONTAINER


using namespace std; #include<iostream.h> #include<vector.h> #include<string.h> #include<conio.h> class stud { int rno; string name; public: stud(int r,string n) { rno=r; name=n; } bool operator<(stud s) { return(rno<s.rno); } friend ostream &operator<<(ostream &tout,stud s1); }; ostream &operator<<(ostream &tout,stud s) { tout<<" ROLL NO "<<s.rno<<endl; tout<<" NAME "<<s.name<<endl; return tout; } int main() { vector<stud>vs; vector<int>va; stud s1(1,"AAA\n"); stud s2(2,"BBB\n"); stud s3(3,"CCC\n"); vs.push_back(s1); vs.push_back(s2); vs.push_back(s3); va.push_back(10); va.push_back(15); vector<stud>::iterator si; for(si=vs.begin();si<vs.end();si++) { cout<<*si; } cout<<INTEGER VECTOR; vector<int>::iterator sq; for(sq=va.begin();sq<va.end();sq++)

{ cout<<*sq<<endl; } getch(); return 0; }

OUTPUT
ROLL NAME ROLL NAME ROLL NAME NO 1 AAA NO 2 BBB NO 3 CCC

Integer vector 10 15

STANDARD TEMPLATE LIBRARY DEQUE CONTAINER


using namespace std; #include<iostream.h> #include<deque.h> #include<string.h> #include<conio.h> class stud { int rno; string name; public: stud(int r,string n) { rno=r; name=n; } bool operator<(stud s) { return(rno<s.rno); } friend ostream &operator<<(ostream &tout,stud s1); }; ostream &operator<<(ostream &tout,stud s) { tout<<"ROLL NO"<<s.rno<<endl; tout<<"NAME"<<s.name<<endl; return tout; } int main() { deque<stud>ds; deque<int>da; stud s1(1,"AAA\n"); stud s2(2,"BBB\n"); stud s3(3,"CCC\n"); ds.push_front(s1); ds.push_front(s2); ds.push_front(s3); da.push_front(10); da.push_front(15); deque<stud>::iterator si; for(si=vs.begin();si<vs.end();si++) { cout<<*si; } cout<<INTEGER DEQUE; deque<int>::iterator sq; for(sq=va.begin();sq<va.end();sq++) {

cout<<*sq<<endl; } getch(); return 0; }

OUTPUT
ROLL NO 1 NAME CCC ROLL NO 2 NAME BBB ROLL NO 3 NAME AAA INTEGER DEQUE 15 10

STANDARD TEMPLATE LIBRARY FIND ALGORITHM


using namespace std; #include<iostream> #include<vector> #include<string> #include<conio.h> #include<algorithm> class stud { int rno; string name; public: stud(int r,string n) { rno=r; name=n; } bool operator==(stud s)const { return(rno==s.rno); } friend ostream &operator<<(ostream &tout,stud s1); }; ostream &operator<<(ostream &tout,stud s) { tout<<"\tROLL NO\t"<<s.rno<<endl; tout<<"\tNAME\t"<<s.name; return tout; } int main() { vector<stud>vs; stud s1(1,"AAA\n"); stud s2(2,"BBB\n"); stud s3(3,"CCC\n"); vs.push_back(s1); vs.push_back(s2); vs.push_back(s3); vector<stud>::iterat or si; si=find(vs.begin(),vs.end(),s2); cout<<*si; getch(); return 0; }

OUTPUT
ROLL NO 2 NAME BBB

STANDARD TEMPLATE LIBRARY SORT ALGORITHM


using namespace std; #include<iostream> #include<vector> #include<string> #include<conio.h> #include<algorithm> class stud { int rno; string name; public: stud(int r,string n) { rno=r; name=n; } bool operator<(stud s)const { return(rno<s.rno); } friend ostream &operator<<(ostream &tout,stud s1); { tout<<"\tROLL NO\t"<<s.rno<<endl; tout<<"\tNAME\t"<<s.name; return tout; } }; int main() { vector<stud>vs; stud s1(33,"AAA\n"); stud s2(23,"BBB\n"); stud s3(13,"CCC\n"); vs.push_back(s1); vs.push_back(s2); vs.push_back(s3); vector<stud>::iterator si; sort(vs.begin(),vs.end(),s2); for(si=vs.begin();si<vs.end();si++) { cout<<*si; } getch(); return 0; }

OUTPUT
ROLL NO 13 NAME CCC ROLL NO 23 NAME BBB ROLL NO 33 NAME AAA

SIMPLE CLASS DESIGN USING javadoc


import java.io.*; import java.util.Scanner; /** this class demonstrates documentation comments *@ author eee *version 01 */ public class squareno { /** * this method returns square of num *@ param num-value to be squared *@ return squared number */ public double square (double num) { return(num*num); } /** This method demonstrates square () *@ parameter arguments unused */ public static void main(String args[]) { squareno sq=new squareno(); double val; System.out.println("Enter the number"); Scanner in=new Scanner(System.in); val=in.nextDouble(); val=sq.square(val); System.out.println("squared value"+val); } }

OUTPUT
Z:\>javac squareno.java Z:\>javadoc squareno.java Loading source file squareno.java... Constructing Javadoc information... Standard Doclet version 1.6.0_15 Building tree for all the packages and classes... Generating squareno.html... Generating package-frame.html... Generating package-summary.html... Generating package-tree.html... Generating constant-values.html... Building index for all the packages and classes... Generating overview-tree.html... Generating index-all.html... Generating deprecated-list.html... Building index for all classes... Generating allclasses-frame.html... Generating allclasses-noframe.html... Generating index.html... Generating help-doc.html... Generating stylesheet.css...

INDEX HTML FILE

INHERITANCE
class superclass { public int a; public int b; superclass(int a1,int b1) { a=a1; b=b1; } void show() { System.out.println("Super Class Show Function"); System.out.println("a="+a+"b="+b); } } class subclass extends superclass { public int c; subclass (int a1,int b1,int c1) { super(a1,b1); c=c1; } void sum() { System.out.println("Subclass Sum Function"); System.out.println("c="+c); System.out.println("Sum="+(a+b+c)); } public static void main(String args[]) { subclass s2=new subclass(5,10,5); s2.sum(); s2.show(); } }

OUTPUT
Z:\>javac subclass.java Z:\>java subclass Subclass Sum Function c=5 Sum=20 Super Class Show Function a=5b=10

INTERFACE
import java.io.*; interface showinterface { int a=10; void show(int p); void print(String p); } abstract class example implements showinterface { public int b=20; public void show(int p) { System.out.println("Abstract class"); System.out.println("Inside Show Function"); System.out.println(" Hello world "+p); } } public class example1 extends example { public void print(String n) { System.out.println(Derived class); System.out.println(Inside Print Function); System.out.println(String is +n); } public static void main(String args[]) { example1 e=new example1(); e.show(5); e.print(hello); e.sum(5); } public void sum(int c1) { int c=c1; int d=a+b+c; System.out.println(Derived Class); System.out.println(Inside Sum function); System.out.println(Sum=+d); } }

OUTPUT
Z:\>javac example1.java Z:\>java example1 Abstract class Inside Show Function Hello world 5 Derived class Inside Print Function String is hello Derived Class Inside Sum function Sum=35

JAVA IO
import java.io.*; class ioexample { public static void main(String args[]) throws IOException { InputStreamReader s=new InputStreamReader(System.in); BufferedReader b=new BufferedReader(s); char c; String str; System.out.println("Enter Character q to quit"); do{ c=(char)b.read(); System.out.println(c); }while(c!='q'); System.out.println("Enter the strings, stop to quit"); do { str=b.readLine(); System.out.println(str); }while(!str.equals("stop")); } }

OUTPUT
Z:\>javac ioexample.java Z:\>java ioexample Enter Character q to quit object oriented programming q o b j e c t o r i e n t e d p r o g r a m m i n g q Enter the strings, stop to quit object oriented programming object oriented programming stop stop

MULTITHREADING
import java.util.*; import java.io.*; class threadA extends Thread { public void run() { for(int i=0;i<5;i++) { System.out.println("ThreadA="+i); } } } class threadB extends Thread { public void run() { for(int i=0;i<5;i++) { System.out.println("ThreadB="+i); } } } class multithread { public static void main(String args[]) { threadA a=new threadA(); threadB b=new threadB(); a.start(); b.start(); } }

OUTPUT
Z:\>javac multithread.java Z:\>java multithread ThreadA=0 ThreadB=0 ThreadA=1 ThreadB=1 ThreadA=2 ThreadB=2 ThreadA=3 ThreadB=3 ThreadA=4 ThreadB=4

EXCEPTION HANDLING NESTED TRY AND MULTIPLE CATCH


import java.util.*; class exetry { static void nestedtry(int a) { try { if(a==1) { a=a/(a-a); } if(a==2) { int c[]={1}; c[50]=100; } } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array index out of bound"+e); } } public static void main(String args[]) { int a=args.length; System.out.println("a"+a); try { nestedtry(a); } catch(ArithmeticException e) { System.out.println("Divide by zero "+e); } } }

OUTPUT
Z:\>javac exetry.java Z:\>java exetry a0 Z:\>java exetry hi a1 Divide by zero java.lang.ArithmeticException: / by zero Z:\>java exetry hi hello a2 Array index out of boundjava.lang.ArrayIndexOutOfBoundsException: 50

EXCEPTION HANDLING THROW,THROWS AND FINALLY


import java.io.*; import java.util.*; class exceptions { void throweg1(int a) throws SecurityException { try { if(a==0) { System.out.println("inside throw example 1"); throw new SecurityException ("example"); } } catch(SecurityException e) { System.out.println("throw eg1 exception caught"); System.out.println(e); } finally { System.out.println("throw eg1 finally"); } } void throweg2() throws NullPointerException { try { throw new NullPointerException("example"); } catch(NullPointerException e) { System.out.println("throw eg2 exception caught"); System.out.println(e); } finally { System.out.println("throw eg2 finally"); } } } public class exceptionmain { public static void main(String args[]) { int a=args.length;

exceptions e=new exceptions(); e.throweg1(a); e.throweg2(); System.out.println("throw, throws and finally are implemented successfully"); } }

OUTPUT
Z:\>javac exceptionmain.java Z:\>java exceptionmain inside throw example 1 throw eg1 exception caught java.lang.SecurityException: example throw eg1 finally throw eg2 exception caught java.lang.NullPointerException: example throw eg2 finally throw, throws and finally are implemented successfully

PACKAGE
Package p1 Class addsub package p1; public class addsub { public void addsub1(int x ,int y) { System.out.println("Package 1"); System.out.println("Class addsub"); System.out.println("Addition="+(x+y)); System.out.println("Subraction="+(x-y)); } } Package p2 Class muldiv package p2; public class muldiv { public void muldiv1(int x,int y) { System.out.println("Package 2"); System.out.println("Class Muldiv"); System.out.println("multiplication="+(x*y)); System.out.println("division="+(x/y)); } } Class packmain import java.util.*; import p1.*; import p2.*; class packmain { public static void main(String args[]) { System.out.println("Enter two numbers"); Scanner in=new Scanner(System.in); int a=in.nextInt(); int b=in.nextInt(); addsub a1=new addsub(); muldiv m1=new muldiv(); a1.addsub1(a,b); m1.muldiv1(a,b); } }

OUTPUT
Z:\>cd p1 Z:\p1>javac addsub.java Z:\p1>cd.. Z:\>cd p2 Z:\p2>javac muldiv.java Z:\p2>cd.. Z:\>javac packmain.java Z:\>java packmain Enter two numbers 10 20 Package 1 Class addsub Addition=30 Subraction=-10 Package 2 Class Muldiv multiplication=200 division=0

You might also like