Cs2312 OOPS LAB MANUAL

9,727 views

Published on

4 Comments
3 Likes
Statistics
Notes
No Downloads
Views
Total views
9,727
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
573
Comments
4
Likes
3
Embeds 0
No embeds

No notes for slide

Cs2312 OOPS LAB MANUAL

  1. 1. LAB MANUAL MAILAMENGINEERING COLLEGE CS2312 OBJECT ORIENTED PROGRAMMING LABLIST 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 TemplatesLIST 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. 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 programPROGRAM:#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. 3. LAB MANUALcout<<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 argumentscout<<endl<<"enter two integer values"<<endl; 3
  4. 4. LAB MANUALcin>>a>>b;swap(a,b);//swapping with user input argumentsc=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 argumentscout<<endl<<"enter two float values"<<endl;cin>>c>>d;swap(c,d);//swapping with user input argumentse=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 argumentscout<<endl<<"enter two characters"<<endl;cin>>e>>f;swap(e,f);//swapping with user input argumentsgetch();}Output:***FUNCTION OVERLOADING WITH DEFAULT ARGUMENTS***before swapvalue of a: 10before swapvalue of b: 20CALLING FUNCTION 1after swapvalue of x: 20 4
  5. 5. LAB MANUALafter swapvalue of y: 10enter two integer values57CALLING FUNCTION 1after swapvalue of x: 7after swapvalue of y: 5before swapvalue of c: 92.7before swapvalue of d: 106.4CALLING FUNCTION 2after swapvalue of x: 106.4after swapvalue of y: 92.7enter two integer values5.557.89CALLING FUNCTION 2after swapvalue of x: 7.89after swapvalue of y: 5.55before swapvalue of e: Abefore swapvalue of f: ZCALLING FUNCTION 3after swapvalue of x: Zafter swapvalue of y: Aenter two integer valuesPQCALLING FUNCTION 3 5
  6. 6. LAB MANUALafter swapvalue of x: Qafter swapvalue of y: PRESULT: Thus the c++ program for inline function overloading with default arguments and user input has beenexecuted successfully----------------------------------------------------XXX------------------------------------------------------------------------ EXPT NO:2 Complex Number Using Operator Overloading.AIM: To write a c++ program for complex numbers using operator overloadingALGORITHM: 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 programPROGRAM:#include<iostream.h>#include<conio.h>class complex{float real;float imag;public: 6
  7. 7. LAB MANUALcomplex(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. 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. 9. LAB MANUAL11+20i21+30iRESULT: 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 functionAIM: To write a c++ program to read a value of distance from one object and add with a value in anotherobject 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 programPROGRAM:#include<iostream.h>#include<conio.h>#include<math.h>class AC;class AB{float d; 9
  10. 10. LAB MANUALpublic: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):12Enter the second distance(in inches):30Total Distance:14.5feetRESULT: 10
  11. 11. LAB MANUAL Thus the c++ program for reading a value of distance from one object and add with a value in anotherobject 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 allocationconcept.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 programPROGRAM:#include<iostream.h>#include<string.h>#include<conio.h> 11
  12. 12. LAB MANUALclass 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. 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:ObjectOrientedProgramming LabObject OrientedObject Oriented Programming LabRESULT: Thus the c++ program for String concatenation using dynamic memory allocation concepthas been executed successfully----------------------------------------------------XXX------------------------------------------------------------------------ 13
  14. 14. LAB MANUAL EXPT NO:5 Type Conversion Using Complex NumbersAIM: To write a c++ program for type conversion using complex numbers.ALGORITHM:Step 1: start the program.Step 2: create the class complexStep 3: declare the variables x and y.Step 4: construct the default constructor.Step 5: define the variablesStep 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. 15. LAB MANUALcomplex (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
  16. 16. LAB MANUALc1=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+i3C2=1+i2C3=3+i5C1=6+i2C2=4+i3C3=10+i5 16
  17. 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. 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. 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. 20. LAB MANUALac.info();break;case 3:break;default:cout<<"Invalid Choice";break;}if (ch==3)break;}getch();}OUTPUT:Enter customer name: XXXEnter your A/c no.:666Enter 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.Exit3RESULT: Thus the C++ program for managing bank account has been executed successfully.----------------------------------------------------XXX------------------------------------------------------------------------ EXPT NO:7 Stack Class With Exception Handling 20
  21. 21. LAB MANUALAIM: To write a c++ program for stack class with exception handlingALGORITHM: 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 programPROGRAM:#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. 22. LAB MANUALcout<<"OverFlow occurs......Size is"<<top<<"> than stack size";}int pop(){if(top<0){cout<<"UnderFlow occurs......Size is";return top+1;}elsereturn 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. 23. LAB MANUALEnter the stack size:3Push operation ….Enter the value : 10Enter the value: 20Enter the value: 30Pop operation …302010underflow occurs…size is 0RESULT: Thus the c++ program for stack class with exception handling has been executed successfully.----------------------------------------------------XXX------------------------------------------------------------------------ EXPT NO:8 QUEUE Class With Exception Handling and TemplatesAIM: To write a c++ program for queue class with exception handling using templatesALGORITHM:Step 1: start the programStep 2: create the class queue and decleare the front and rear valuesStep 4: initialize the front and rear == -1Step 4: then the condition is true incremented the front valueStep 5: then the condition is flase then display the output the queue is emptyStep 6: after that incremented the rear valueStep 7: the elements are inserted and display the queueStep 8: print the outputStep 9: stop the programPROGRAM:#include <iostream># define MAX 4class queue 23
  24. 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]=11arr[1]=12arr[2]=13front=0arr[3]=14arr[rear]=item;if(front==-1)front=0;count<<"n Item added into queue : <<arr[real];}int del q(){ 24
  25. 25. LAB MANUALint data;if(front==-1)throw q emty ();data=arr[front] ;if(front==rear=-1;elsefront++;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. 26. LAB MANUALcout <<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:11item added into queue:12item added into queue:13item added into queue:14queue is fullitem deleted:11item deleted:12item deleted:13item deleted:14queue is emptyRESULT: 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 NumberAIM:To write a java program to print multiplication table of a given numberAlgorithm: Step 1: Start the Program Step 2: create a class with the name mtable. 26
  27. 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 programPROGRAM: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.javaD:javaprg>java mtableOUTPUT: 27
  28. 28. LAB MANUALenter the number9***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. 29. LAB MANUALAIM:To write a java program to print multiplication table of a given numberAlgorithm: 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. 30. LAB MANUALD:javaprg>javac mtable.javaD:javaprg>java mtableOUTPUT:GIVEN STRING: malayalamREVERSED STRING:: malayalamMalayalam :: IS A PALINDROMERESULT:Thus the program to perform palindrome using java is done.----------------------------------------------------XXX------------------------------------------------------------------------ EXPT NO:11 To calculate area of rectangle and circle using interfacesAIM: 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. 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. 32. LAB MANUALclass 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.javaD:javaprg>java InterfaceTestOUTPUT:Area of Rectangle=200.0Area of Circle=314.0RESULT:Thus the programs for calculating area for circle and rectangle are done using java.----------------------------------------------------XXX------------------------------------------------------------------------ EXPT NO:12 To perform multilevel inheritanceAIM: To perform multiple inheritance in java. 32
  33. 33. LAB MANUALALGORITHM: 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. 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. 35. LAB MANUALSystem.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.javaD:javaprg>java InterfaceTestOUTPUT:***** multilevel inheritance*******values from class a1: 70values from class a1: 50values from class a1: 60values from class b1: 70values from class b1: 90total value from class b1: 340average of 5 numbers: 68GRADE IS (61-70): CRESULT:Thus the programs for are done using java. EXPT NO:13 Exception handling using java 35
  36. 36. LAB MANUALAIM: 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. 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. 38. LAB MANUALRESULT:Thus the program for exception handling is done using java. EXPT NO:14 Multithreading using javaAIM: 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. 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. 40. LAB MANUAL}OUTPUT:E:D.PRABHUthrd>javac sqr1.javaE:D.PRABHUthrd>javac cub1.javaE:D.PRABHUthrd>javac thrd1.javaE:D.PRABHUthrd>java thrd11 square is:: 12 square is:: 43 square is:: 94 square is:: 165 square is:: 256 square is:: 367 square is:: 498 square is:: 641 cube is:: 12 cube is:: 83 cube is:: 274 cube is:: 645 cube is:: 125 40
  41. 41. LAB MANUAL6 cube is:: 2167 cube is:: 3438 cube is:: 5129 cube is:: 72910 cube is:: 10009 square is:: 8110 square is:: 100RESULT:Thus the program for multithreading using java is done. 41

×