OOPS Record With Algorithms
OOPS Record With Algorithms
OOPS Record With Algorithms
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
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:
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);
OUTPUT:
count 1 count 2
RESULT: Thus the program is executed and output is verified using C++.
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(); }
RESULT: Thus the program is executed and output is verified using C++.
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
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
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++.
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++.
#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++.
#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++.
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++.
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(); }
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++.
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++.
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.
Step III
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:
RESULT: Thus the program is executed and output is verified using Java.
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)); } }
RESULT: Thus the program is executed and output is verified using Java.
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.
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)
RESULT: Thus the program is executed and output is verified using Java.
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.
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.