Upload
krishna-sankar-pa
View
235
Download
0
Embed Size (px)
Citation preview
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
1/80
~ 1 ~
CS2312 OBJECT ORIENTED PROGRAMMING LAB L T P C
0 0 3 2
Aim:
To develop object-oriented programming skills using C++ and Java.
1. Function overloading, default arguments in C++
2. Simple class design in C++, namespaces, objects creations
3. Class design in C++ using dynamic memory allocation, destructor, copy constructor
4. Operator overloading, friend functions
5. Overloading assignment operator, type conversions
6. Inheritance, run-time polymorphism
7. Template design in C++
8. I/O, Throwing and Catching exceptions
9. Program development using STL
10.Simple class designs in Java with Javadoc
11.Designing Packages with Javadoc comments
12. Interfaces and Inheritance in Java
13.Exceptions handling in Java
14.Java I/O
15.Design of multi-threaded programs in Java
TOTAL: 45 PERIODS
PKS
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
2/80
~ 2 ~
Programming using C++
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
3/80
~ 3 ~
Output:
Volume of Cube :1000
Volume of Cuboid :1004.94
Volume of Cylinder:2795.92
Viva questions:
1. What are the features of OOP? When a program termed to be OO Program?
2. Define Function Overloading
3. Give prototype for Function Overloading
4. Brief default parameters available in exercise
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
4/80
~ 4 ~
Ex. No. : 1Function overloading, default arguments in C++
Aim:
To implement calculation for volume of Cube, Cuboids and Cylinder through Function
Overloading and also passing Default arguments in C++.
Algorithm:
Step 1: Defining the variable and function for cube, cuboids and cylinder.
Step 2: Declaring the function with one argument for cube and return type as integer.
Step 3: Declaring the function with three argument length, breadth, and height for cuboids with
return type double.
Step 4: Declaring the function with two argument radius and height for cylinder with return type
double.
Step 5: Calling the declared function with parameter value for cube, cuboid and cylinder.
Step 6: Print the return value.
Step 7: Stop the process.
Program:
#include
#include
int volume(int); // Volume of cube
double volume (int, double, int h = 9); // Volume of Cuboids
double volume (double, int h = 8); // Volume of Cylinder
float pi=3.14;
int volume (int a) //Implementation of above definition
{return (a*a*a);
}
double volume (int l, double b, int h)
{
return (l*b*h);
}
double volume (double r, int h)
{
return (pi*r*r*h);
}
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
5/80
~ 5 ~
5. Write a program for area of circle, rectangle and triangle using function overloading
6. Write a program to demonstrate function overloading is carried out for swapping of two
variables of the various data types, namely integer, floating point number and character.
Result:
Thus, Volume of Cube, Cuboids and Cylinder was calculated using Function Overloading
and Default arguments.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
6/80
~ 6 ~
int main()
{
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
7/80
~ 7 ~
Output:
Simple Class design
Enter the values a &b: 78
96
C = 174
Average=87
Viva questions:
1. Define namespace
2. Give prototype for Class and creation of object
3. Brief use of data member and function in exercise
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
8/80
~ 8 ~
Ex. No. : 2Simple class design in C++, namespaces, objects creations
Aim:
To implement the class with namespaces, object creations in C++.
Algorithm:
Step 1: Defining the namespace, class, variable and function.
Step 2: Declaring the function getdata and enter the two values for variable.
Step 3: Declaring the function sum for performing adding operation.
Step 4: Declaring the function display then print the added value.
Step 5: Creating the object for a class and calling the functions by object variable.
Step 6: Stop the process.
Program:
#include
using namespace std;
namespacevalD
{
int t=2;
}
class add
{
inta,b,c; // Declare members to class Add
public:
voidgetdata(void); // Declare member functions to class Add
voidsumavg(void);
void display(void);
};
void add::getdata(void) // Collect the data
{
couta>>b;
}
void add::sumavg(void) // Sum the data values
{
c=a+b;
}
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
9/80
~ 9 ~
4. Write a program to declare a integer, float and character in different namespace and use it
5. Write aSimple Class Example Program to checka number is Prime or not
Result:
Thus the implementation of class with namespaces and creating object are executed
successfully.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
10/80
~ 10 ~
void add::display(void) // Display the data
{
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
11/80
~ 11 ~
Output:
Enter rows and cols of the matrix...
3
3
Enter the matrix elements one by one...5
5
5
5
5
5
5
5
5
Entered matrix is...
5 5 5
5 5 5
5 5 5
Copy constructor invoked...
Result of copy constructor is...
5 5 5
5 5 5
5 5 5
Destructor invoked...
Destructor invoked...
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
12/80
~ 12 ~
Ex. No. : 3 Class design in C++ using dynamic memory allocation,
destructor, copy constructor
Aim:
To implement class with the dynamic memory allocation of construct a matrix, destructorand copy constructor using C++.
Algorithm:
Step 1: Defining the namespace, class and variable.
Step 2: Declaring the class matrix along with the constructor and destructor.
Step 3: Constructor with no argument which assign the value for row and column as null.
Step 4: Constructor with two argument are the function are declared.
Step 5: The values are copied to another matrix.
Step 6: De-allocate the memory after copying the matrix.
Step 7: Stop the process.
Program:
#include
using namespace std;
class matrix
{
int **m;
int row, col;
public:
matrix()
{
row=col=0;m=NULL;
}
matrix(int r ,int c); // Two argument Constructor
~matrix(); // Destructor
voidgetmatrix(); // Collect value
voidshowmatrix(); // Display & delete matrix memory
matrix(matrix &m2); //copy constructor
};
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
13/80
~ 13 ~
Viva questions:
1. Define dynamic memory allocation
2. What is Constructor? When it will be invoked? Give Prototype for multiple arguments
Constructor?
3. What is Destructor?When it will be invoked? Give Prototype for Destructor?
4. What is need for Copy Constructor? Give prototype for Copy Constructor
5. Compare features of Constructors and Destructors.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
14/80
~ 14 ~
matrix::~matrix()
{
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
15/80
~ 15 ~
6. Write a Program using above code create two matrices A and B and perform addition display
it
Result:
Thus implementation of dynamic memory allocation of construct a matrix, destructor and
copy constructor are executed successfully.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
16/80
~ 16 ~
int main()
{
intr,c;
coutr>>c;
matrix m1(r,c);
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
17/80
~ 17 ~
Output:
Before operator overloading
I : Longitude :10 Latitude :20
II : Longitude :5 Latitude :30
After overloading binary operator +: Longitude :15 Latitude :50
After overloading binary operator -: Longitude :10 Latitude :20
After overloading unary operator ++: Longitude :11 Latitude :21
After overloading unary operator ++: Longitude :12 Latitude :22
Viva questions:
1. What is need for Operator overloading? Give prototype for Operator overloading
2. What is need for Friend function? Give prototype for Friend function
3. Give prototype for Friend class
4. What are operators cannot be overloaded ?
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
18/80
~ 18 ~
Ex. No. : 4Operator overloading, friend functions
Aim:
To implement Longitude and Latitude value based operation using operator overloading and
friend functions using C++.
Algorithm:
Step 1: Defining the namespace, class, variable and function.
Step 2: Constructor with two arguments for assigning the longitude and latitude values.
Step 3: Declaring the friend function with operator overloading.
Step 4: Declaring the show function, displays the operator overloaded values.
Step 5: Creating the object for a class and calling the functions by object variable for unary,
binary overloading operation.
Step 6: Stop the process.
Program:
#include
using namespace std;
class loc
{
intlongitude,latitude;
public:
loc()
{
}
loc(intlg, intlt)
{
longitude=lg;
latitude=lt;
}
void show()
{
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
19/80
~ 19 ~
5. Give prototype for Unary and Binary operator overloading
6. Write a Program to create collect two complex number and perform addition, subtraction
Result:
Thus the implementation of operator overloading and friend function executed successfully.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
20/80
~ 20 ~
loc operator +(loc op1,loc op2)
{
loc temp;
temp.longitude=op1.longitude+op2.longitude;
temp.latitude=op1.latitude+op2.latitude;
return temp;
}
loc operator -(loc op1,loc op2)
{
loc temp;
temp.longitude=op1.longitude-op2.longitude;
temp.latitude=op1.latitude-op2.latitude;
return temp;
}
loc operator++(loc &op1)
{op1.longitude ++;
op1.latitude ++;
return op1;
}
int main()
{
loc ob1(10,20),ob2(5,30);
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
21/80
~ 21 ~
Output:
c1= 5+10i
c2= 50+100i
c2 with += increment of c1:
c2= 55+110i
Assign c2 to c3:
c3= 55+110i
c4= 10+50i
c3 with -= decrement of c4
c3= 45+60i
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
22/80
~ 22 ~
Ex. No. : 5Overloading assignment operator, type conversions
Aim:
To implement assignment operator overloading and type conversions are using C++.
Algorithm:
Step 1: Defining the namespace, class, variable and function.
Step 2: Constructor with two arguments for assigning the real and imaginary values in complex
number.
Step 3: Declaring the function with assignment operator overloading.
Step 4: Passing the values in the calling function argument for complex number.
Step 5: The incremented values are copied to another complex number.
Step 6: The decrement operation are performed with the copied complex number using the
assignment operator.
Step 7: Stop the process.
Program:
#include
using namespace std;
classMyComplex
{
private:
double real, imag;
public:
MyComplex() // Constructor declaration
{
real = 0;
imag = 0;
}
MyComplex(double r, double i) // Constructor invoked with parameters
{
real = r;
imag = i;
}
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
23/80
~ 23 ~
Viva questions:
1. Give prototype for Assignment Operator overloading
2. Compare the Operator and Assignment Operator overloading
3. Is default value is exist in declaration or not ?
4. Compare the Binary and Assignment operator overloading
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
24/80
~ 24 ~
doublegetReal()
{
return real;
}
doublegetImag()
{returnimag;
}
MyComplex& operator=(MyComplex&);// Assignment of complex numbers
MyComplex& operator+=(MyComplex& );// Increment of one with another complex
MyComplex& operator-=(MyComplex& );// Decrement of one with another complex
};
MyComplex&MyComplex::operator = (MyComplex& c)
{this->real = c.real;
this->imag = c.imag;
return *this;
}
MyComplex&MyComplex::operator += (MyComplex& c)
{
this->real += c.real;
this->imag += c.imag;
return *this;
}
MyComplex&MyComplex::operator -= (MyComplex& c)
{
this->real -= c.real;
this->imag -= c.imag;
return *this;
}
int main()
{
MyComplexc1(5,10);
MyComplexc2(50,100);
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
25/80
~ 25 ~
5. Write a Program to create collect two matrix and perform addition, subtraction
Result:
Thus the implementation of assignment operator overloading and type conversions are
executed successfully.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
26/80
~ 26 ~
c3 = c2;
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
27/80
~ 27 ~
Output:
Enter your A/c no.:15421
Account Number : 15421
Enter the A/c type, 1.Savings 2.Current 3.Exit
1
Enter the A/c type, 1.Deposit 2.Withdraw
1
Enter the amount to be deposited = Rs.21545
Your A/c Balance is=Rs.21545
Account Number : 15421
Enter the A/c type, 1.Savings 2.Current 3.Exit
1
Enter the A/c type, 1.Deposit 2.Withdraw
2
Enter the amount to be withdrawn = Rs.254
Your A/c Balance is=Rs.21291
Account Number : 15421
Enter the A/c type, 1.Savings 2.Current 3.Exit2
Last amount withdrawn=Rs.254
Last amount deposited=Rs.21545
Your A/c Balance is=Rs.21291
Account Number : 15421
Enter the A/c type, 1.Savings 2.Current 3.Exit
3
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
28/80
~ 28 ~
Ex. No. : 6Inheritance, run-time polymorphism
Aim:
To implement the inheritance and run-time polymorphism using C++.
Algorithm:
Step 1: Defining the namespace, class, variable and function.
Step 2: Class bank displays the customer name and account number.
Step 3: Inherit properties of the class bank in newly created class savings.
Step 4: Class savings to perform the deposit and withdraw process.
Step 5: Class current inherits with class savings to display the account information.
Step 6: By creating the object for a class currentall the values are accessed and calling the
functions by object variable for performing banking operation.
Step 7: Stop the process.
Program:
#include
using namespace std;
class bank // Create Bank Base Class
{
public: //Bank account Number
longintacno;
virtual void display()
{
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
29/80
~ 29 ~
Viva questions:
1. Define inheritance
2. What are types of inheritance
3. Compare inheritance and run-time polymorphism
4. Write a Program to collect student details of mark using Multilevel Inheritance
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
30/80
~ 30 ~
switch(ch)
{
case 1:
coutdep;
bal+=dep;
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
31/80
~ 31 ~
5. Write a Program to maintain book lending to staff and students using Multiple Inheritance
Result:
Thus the implementation of inheritance and run time polymorphism are executed
successfully.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
32/80
~ 32 ~
switch(ch)
{
case 1:
ac.saves();
break;
case 2:
ac.display();break;
case 3:
break;
default:
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
33/80
~ 33 ~
Output:
Bubble SORT
1.Integer Sort 2.Float Sort 3.Character Sort 4.Exit
Enter Ur choice:1
Bubble Sort on Integer Values...
Enter the size of array:
4
Enter the array elements:85
68
45
21
The sorted array is...
21 45 68 85Bubble SORT
1.Integer Sort 2.Float Sort 3.Character Sort 4.Exit
Enter Ur choice:2
Bubble Sort on Float Values...
Enter the size of array:
4
Enter the array elements:5.665532
3.24541
8.456541
4.56646665
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
34/80
~ 34 ~
Ex. No. : 7Template design in C++
Aim:
To implement the template design using C++
Algorithm:
Step 1: Defining the namespace, class, variable and function.
Step 2: Declaring the template class T.
Step 3: Declaring the get function for getting the value to bubble sort.
Step 4: Declaring the display function to display the value before bubble sorting.
Step 5: Declaring the sort function for bubble sorting process.
Step 6: The integer, float and character are bubble sorted.
Step 7: Separate object are created for bubble sorting integer, float and character.
Step 8: Stop the process.
Program:
#include
#include
using namespace std;
template // Create template class
class bubble
{
t a[25]; // Declare template variable
public:
void get(int); // Definition for collect values
void sort(int); // Definition for sort values
void display(int); // Definition for display sorted values
};
template
void bubble ::get(int n)
{
inti;
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
35/80
~ 35 ~
The sorted array is...
3.24541 4.56647 5.66553 8.45654
Bubble SORT
1.Integer Sort 2.Float Sort 3.Character Sort 4.Exit
Enter Ur choice:3
Bubble Sort on Character Values...
Enter the size of array:
4
Enter the array elements:p
a
s
d
The sorted array is...
a d p s
Bubble SORT
1.Integer Sort 2.Float Sort 3.Character Sort 4.Exit
Enter Ur choice:4
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
36/80
~ 36 ~
template
void bubble ::display(int n)
{
inti;
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
37/80
~ 37 ~
Viva questions:
1. Define Template
2. Compare Class and Template
3. Classify features of template member with class member
4. Can we use friend member function for template? If so How?
5. Can we use constructor in template? If so How?
Result:
Thus the implementation of Template executed successfully.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
38/80
~ 38 ~
case 2:
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
39/80
~ 39 ~
Output:
STACK
1.Push 2.Pop 3.Display 4.ExitENTER THE CHOICE:1
Enter Number to Push:56
Top element in stack is:56
1.Push 2.Pop 3.Display 4.Exit
ENTER THE CHOICE:1
Enter Number to Push:78
Top element in stack is:78
1.Push 2.Pop 3.Display 4.Exit
ENTER THE CHOICE:1
Enter Number to Push:99
Top element in stack is:99
1.Push 2.Pop 3.Display 4.Exit
ENTER THE CHOICE:1
Enter Number to Push:23
Top element in stack is:23
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
40/80
~ 40 ~
Ex. No. : 8I/O, Throwing and Catching exceptions
Aim:
To implement the I/O, Throwing and Catching Exceptions using C++.
Algorithm:
Step 1: Defining the namespace, class, variable and function.
Step 2:Declaring the function for push, pop, and display operation.
Step 3:In push function the stack is full it return x=1, otherwise it gets the number and push into
the stack.
Step 4:In pop function the stack is empty it return x=-1, otherwise it removes the number from top
of stack
Step 5: By selecting the operation that will performed on the stack
Step 6: The values in stack are displayed by selecting the display option.
Step 7: Stop the process using exit.
Program:
#include
#include
using namespace std;
template
class bubble
{
#include
using namespace std;
classstk // Declare a stack class
{
intx,top=-1; //Stack pointer initialize with -1
int stack[5]; // Integer type stack is created with size of 5
public:
int push(); // Stack push operation with return type
int pop(); // Stack pop operation with return type
int display(); // Display Stack contents with return type
};
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
41/80
~ 41 ~
1.Push 2.Pop 3.Display 4.Exit
ENTER THE CHOICE:1
Enter Number to Push:55
Top element in stack is:55
1.Push 2.Pop 3.Display 4.Exit
ENTER THE CHOICE:3
CONTENTS OF STACK:
55
23
99
78
56
1.Push 2.Pop 3.Display 4.Exit
ENTER THE CHOICE:1
An exception occurred!
Stack is Full
1.Push 2.Pop 3.Display 4.Exit
ENTER THE CHOICE:2
Popped element from Stack is55
1.Push 2.Pop 3.Display 4.Exit
ENTER THE CHOICE:2
Popped element from Stack is23
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
42/80
~ 42 ~
intstk::push()
{
int x = 0;
if(top==5-1)
x=1;
else
{
top=top+1;
coutx;
stack[top]=x;
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
43/80
~ 43 ~
1.Push 2.Pop 3.Display 4.Exit
ENTER THE CHOICE:2
Popped element from Stack is99
1.Push 2.Pop 3.Display 4.Exit
ENTER THE CHOICE:2
Popped element from Stack is78
1.Push 2.Pop 3.Display 4.Exit
ENTER THE CHOICE:2
Popped element from Stack is56
1.Push 2.Pop 3.Display 4.Exit
ENTER THE CHOICE:2
An exception occurred!
Stack is Empty
1.Push 2.Pop 3.Display 4.Exit
ENTER THE CHOICE:3
An exception occurred!
Stack is Empty
1.Push 2.Pop 3.Display 4.Exit
ENTER THE CHOICE:4
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
44/80
~ 44 ~
int main()
{
stk s;
int a;
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
45/80
~ 45 ~
Viva questions:
1. What is need for exception handling
2. Write prototype for exception handling
3. How many catches and throws use for a try ?
4. Write a program for Array bound exception
5. Write a program for handling divide by zero
Result:
Thus the implementation of I/O, Throwing and Catching exceptions are executed
successfully.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
46/80
~ 46 ~
catch (float b) // Message for stack full is catched
{
cout
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
47/80
~ 47 ~
Input (test.txt):
File Types of access
Parameter mode detailed
Output:
File Types of access
Parameter mode detailed
Viva questions:
1. Write a program using files to handle exceptions
2. What are different modes for accessing files
3. List some STL features available
4. Can we create a STL on our own?
Result:
Thus the implementation of file operation executed successfully.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
48/80
~ 48 ~
Ex. No. : 9Program development using STL File Operation
Aim:
To implement the file operation using C++.
Algorithm:Step 1: Declaring the header file file.h
Step 2:Declaring the file output stream
Step 3: Open the file in append mode
Step 4: Add content to file through stream
Step 5: Close the file in write mode
Step 6:Declaring the file input stream
Step 7: Open the file read mode
Step 8: Get content from file through stream and print it
Step 9: Close the file in write mode
Step 10: Stop the process using exit.
Program:
#include
#include
using namespace std;
int main (){
string line;
ofstream myfile1 ("test.txt", ofstream::app); // File opened in append mode to write
if(myfile1.is_open()) {
myfile1
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
49/80
~ 49 ~
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
50/80
~ 50 ~
Programming using Java
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
51/80
~ 51 ~
Output:
Generating 10 random integers in range 0..99.
Random No is 60
Random No is 79
Random No is 31
Random No is 62
Random No is 6
Random No is 79
Random No is 1
Random No is 26
Random No is 76
Random No is 28
Viva questions:
1. List some packages and its operation
2. Compare STL and Java package
3. What are types of data types can be generated randomly?
4. Is Java partially object oriented or fully object oriented? How ?
Result:
Thus the implementation of file operation executed successfully.
.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
52/80
~ 52 ~
Ex. No. : 10Simple class designs in Java with Javadoc
Aim:
To implement class with usage of random number generation using java.
Algorithm:
Step 1: Declare the class and variable.
Step 2: To generate the random number from 0.99 using RandomGenerator.
Step 3: Limiting the number range by passing the value to the functionnextInt.
Step 4: Display the random number.
Step 5: Stop the process.
Program:
importjava.util.Random;
public final class l10
{
public static final void main(String[] Args)
{
System.out.println("Generating 10 random integers in range 0..99.");
Random randomGenerator = new Random();
for (int n = 1; n
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
53/80
~ 53 ~
Output:
Number Serious Generation
7
Factorial is 5040
Fibonacci Serious
0, 1, 1,2,3,5,8,13
Viva questions:
1. Design a package for banking operations
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
54/80
~ 54 ~
Ex. No. :11Designing Packages with Javadoc comments
Aim:
To implement design of packages and utilize using java.
Algorithm:
Step 1: Declaring the package L11pack, class L11numman and function
Step 2:Enter the value for Number Serious Generation
Step 3: Import the package L11pack
Step 3: Passing the value as parameter for Factorial number and Fibonacci series
Step 4: The generated series are displayed
Step 5: Stop the process
Program:
// Liinumman.java
package L11pack; // Package Declaration
public class L11numman{
public void factorial(int n)
{
int f=1;
if(n==0)
{
System.out.println("Factorial is 1");
}
else
{for(inti=1; i
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
55/80
~ 55 ~
2. Design a package for library lending operations
3. Write prototype for package declaration and accessing it
Result:
Thus the implementation of package design using java
.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
56/80
~ 56 ~
public void fibonci(int n)
{
int f1=0, f2=1;
System.out.println("Fibonacci Serious ");
if(n>=0)
{
System.out.print("0");}
if(n>=1)
{
System.out.print(", 1");
}
if(n>=2)
{
for(inti=2; i
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
57/80
~ 57 ~
Output:
Enter Rectangle length and width :
10
5
Area of Rectangle =50.0
Circle Radius :
6
Area of Circle =113.04
Viva questions:
1. What is need for interface?
2. What is inheritance?
3. What are types of inheritance? Give it in pictorial form.
4. Write prototype for interface and implementing interface.
5. How to create multiple inheritance in Java ?
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
58/80
~ 58 ~
Ex. No. : 12Interfaces and Inheritance in Java
Aim:
To implement the interface and inheritance using java.
Algorithm:
Step 1: Define the interface area.
Step 2: Implement the interface are to the class rectangle and circle.
Step 3: Inherit the rectangle and circle by using the interface.
Step 4: Entering the length, breadth and radius total area for rectangle and circle are calculate.
Step 4: By creating the objects the area of the rectangle and circle are Displayed.
Step 5: Stop the process.
Program:
// Area.java
interface Area
{
final static float pi=3.14F;
float compute(float x,float y);
}
//Rectangle.java
class Rectangle implements Area
{
public float compute(float x,float y)
{
return(x*y);
}
}
//Circle.java
class Circle implements Area
{
public float compute(float x, float y)
{
return(pi*x*x);
}
}
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
59/80
~ 59 ~
6. Write a interface for sorting and implement bubble and selection sort
Result:
Thus the implementation of interfacing and inheritance are executed successfully in java.
.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
60/80
~ 60 ~
// l12.java
class l12
{
public static void main(String args[])
{Rectangle rect=new Rectangle();
Circle cir=new Circle();
Area ar;
ar=rect;
Scanner ir= new Scanner(System.in);
System.out.println("Enter Rectangle length and width :");
int x = ir.nextInt();
int y = ir.nextInt();
System.out.println("Area of Rectangle ="+ar.compute(x,y));
ar=cir;System.out.println("Circle Radius :");
int r = ir.nextInt();
System.out.println("Area of Circle ="+ar.compute(r,0));
}
}
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
61/80
~ 61 ~
Output:
Oops, we went to far, better go back to 0!3
Next Process
Cannot Divide by Zero!
Viva questions:
1. Write prototype for Exception handling
2. What are default exception available
3. Compare difference between C++ and Java exception handling
4. Write a program to find IO exception
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
62/80
~ 62 ~
Ex. No. :13Exceptions handling in Java
Aim:
To implement the Exception handling using java.
Algorithm:
Step 1: Declare the class.
Step 2: Define the variables.
Step 3: By the single try, catch and finally block for handling array out of exception.
Step 4: By using single try and multiple catch block handles many exception.
Step 5: Stop the process
Program:
public class l13
{
public static void main(String[] args)
{
int[] array = new int[3];
try
{
for(inti=0;i
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
63/80
~ 63 ~
5. Write a program to find Class not found exception
Result:
Thus the implementation of java exception handling executed successfully.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
64/80
~ 64 ~
try
{
array[0] = 2/0;
}
catch(ArithmeticException e)
{
System.out.println("Cannot Divide by Zero!");}
catch(Exception e)
{
System.out.println("An Unknown Error has Occured");
}
}
}
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
65/80
~ 65 ~
Input:
Hai
Basic demo for
File Manipulation with streams
exit
Output:
Hai
Basic demo forFile Manipulation with streams
Viva questions:
1. Write a program using files to handle exceptions
2. What are different modes for accessing files
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
66/80
~ 66 ~
Ex. No. : 14Java I/O
Aim:
To implement the Java input and output for file based using java.
Algorithm:
Step 1: Declaring class and variables.
Step 2: Read the input and write in input text file in bytes until exit String is not input.
Step 3: Read the input file in bytes and display it.
Step 4: In each read and write operation the file must be open and close properly.
Step 5: Stop the process.
Program:
import java.io.*;
importjava.util.Scanner;
public class l13
{
public static void main (String args[]) throws Exception
{
String src = "";
String s;
do
{
Scanner in = new Scanner(System.in);
s = in.nextLine();
src += s;
}while(!(s.equals("exit")));
bytebuf[] = src.getBytes();
OutputStream f1 = new FileOutputStream("input.txt");
f1.write(buf);
f1.close();
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
67/80
~ 67 ~
3. Write a program using reader and writer class to perform file operation
\
4. What are different streamsfor file operations
Result:
Thus the implementation of file operation executed successfully.
.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
68/80
~ 68 ~
InputStreaminputStream = new FileInputStream("input.txt");
Reader reader = new InputStreamReader(inputStream);
int data = reader.read();
while(data != -1)
{chartheChar = (char) data;
data = reader.read();
System.out.print(theChar);
}
reader.close();
}
}
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
69/80
~ 69 ~
Output (Run 1):
Run method: Thread 1
Run method: 2nd Thread
Run method: Thread 1
Run method: 2nd Thread
Run method: Thread 1
Run method: 2nd Thread
Run method: Thread 1
Run method: Thread 1
Run method: 2nd Thread
Run method: 2nd Thread
Run method: Thread 1
Run method: 2nd Thread
Output (Run 2):
Run method: 2nd Thread
Run method: Thread 1
Run method: 2nd Thread
Run method: 2nd Thread
Run method: Thread 1
Run method: Thread 1
Run method: Thread 1
Run method: Thread 1
Run method: 2nd ThreadRun method: Thread 1
Run method: 2nd Thread
Run method: 2nd Thread
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
70/80
~ 70 ~
Ex. No. :15Design of multi-threaded programs in Java
Aim:
To implement the multi-threading using Java.
Algorithm:
Step 1: Declare the class with extended thread.
Step 2: Define the variables.
Step 3: Multi-Threads are handled by the declared package.
Step 4: By using try and catch interrupted exception are handled.
Step 5: Stop the process.
Program:
//ThreadClass.java
Package l15pack;
public class ThreadClass extends Thread
{
String msg;public void run()
{
for(inti=0;i
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
71/80
~ 71 ~
Viva questions:
1. What are differences between wait and sleep method in java
2. How will you awake a blocked thread in java?
3. What is the difference between Process and Thread?
4. What are different states in lifecycle of Thread?
5. What do you understand about Thread Priority?
Result:
Thus the implementation of multi-threading executed successfully.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
72/80
~ 72 ~
//l15.java
class l15
{
public static void main(String[] args)
{
ThreadClass dt1=new ThreadClass("Thread 1");
ThreadClass dt2=new ThreadClass("2nd Thread");
dt1.start(); // this will start thread of object 1
dt2.start(); // this will start thread of object 2
}
}
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
73/80
~ 73 ~
Additional Exercise for Practice
C++:
1. Write a Program For Simple Class Example Program to Prime Number or not.
2. Write a C++ program for Students mark analysis using Static Data member, Default
Argument and Friend Function.
3. Write a C++ program for Students mark analysis using Static Data member, Default
Argument and Friend Function.
4. Design C++ classes with static members, methods with default arguments, friend functions.
(For example, design matrix and vector classes with static allocation, and a friend function to
do matrix-vector multiplication)
5. Write a Program for Addition of two distances of different object using Friend Function in
C++
6. Write a Program for Maximum of two distances of different object using Friend Function in
C++
7. Write a Program for Addition and Subtraction of Two Polynomial Object using Operator
Overloading in C++
8. Write a C++ program to Implement Matrix Class using Constructor, Destructor, Copy
Constructor, Overloading assignment operator.
9. Write a C++ program to implement complex number class with operator overloading and type
conversions such as integer to complex, double to complex, complex to double.
10.Implement complex number class with necessary operator overloading and type conversions
such as integer to complex, double to complex, complex to double etc.
Implement Matrix class with dynamic memory allocation and necessary methods. Give proper
constructor, destructor, copy constructor, and overloading of assignment operator.
Overload the new and delete operators to provide custom dynamic allocation of memory.
11.Write a C++ program to Overload the new and delete operators for addition of vector
elements to provide custom dynamic allocation of memory.
12.Write a Program for String concatenation using Operator Overloading concept in C++
13.Write a Program for Implementation of Arithmetic Operations on Complex numbers using
Constructor Overloading in C++.
14.Write a Program for String concatenation using Dynamic Memory Allocation in C++
15.Write a C++ program to implement the template of linked list class
16.Write a C++ program to implement the Templates of standard sorting algorithms such as
bubble sort, insertion sort, merge sort, and quick sort.
17.Write a Program for Implementation of Virtual Base Class in C++.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
74/80
~ 74 ~
18.Define Point class and an Arc class. Define a Graph class which represents graph as a
collection of Point objects and Arc objects. Write a method to find a minimum cost spanning
tree in a graph.
19.Develop with suitable hierarchy, classes for Point, Shape, Rectangle, Square, Circle, Ellipse,
Triangle, Polygon, etc. Design a simple test application to demonstrate dynamic
polymorphism and RTTI.
20.Develop a template of linked-list class and its methods.
21.Design stack and queue classes with necessary exception handling.
22.Write a C++ program that randomly generates complex numbers (use previously designed
Complex class) and writes them two per line in a file along with an operator (+, -, *, or /). The
numbers are written to file in the format (a + ib). Write another program to read one line at a
time from this file, perform the corresponding operation on the two complex numbers read,
and write the result to another file (one per line).
23.Write a C++ program to implement the Queue class with necessary exception handling.
24.Write a Program for Student Grade Calculations in C++.
25.Write a Program for Employee Details in C++.
26.Write a Program for Convert Fahrenheit to Celsius in C++.
27.Write a Program for Implementation of Addition Operation of Octal Object in C++
28.Write a Program for Managing Bank Account using Inheritance concept in C++.
29.Write a Program for Area of different dimensions using Virtual Function in C++.
30.Write a Program for Implementation of Pure Virtual Function in C++.
Java:
31.Write a Program for Area of Different Dimension Using Interface in Java
32.Write a Program for Student Mark Details using Interface in Java.
33.Write a Program for Developing Packages in Java
34.Write a Program for Arithmetic operations in Java
35.Write a Program to Implement Exceptions in Java.
36.Write a Program for Largest of two numbers in Java
37.Write a Program for Addition of two numbers using Data Input Stream in Java.
38.Write a Program for Program to Implement Exceptions in Java.
39.Write a Program for Odd or Even using Data Input Stream in Java
40.Write a Program for Factorial using Data Input Stream in Java.
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
75/80
~ 75 ~
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
76/80
~ 76 ~
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
77/80
~ 77 ~
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
78/80
~ 78 ~
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
79/80
~ 79 ~
7/22/2019 Eee 5th V sem CS 2312 Oop lab manual
80/80