0% found this document useful (0 votes)
66 views

22 C++and Java Mansi Punde

The document contains details about Mansi Punde who is a student of Class DSE EXTC (B) with Roll Number 22. It then lists 20 experiments written in C++ covering topics like relational operators, arithmetic operators, logical operators, bitwise operators, if-else statements, loops, functions, classes, constructors, destructors and inheritance. It also lists 6 Java experiments covering topics like control statements, arrays, constructors, inheritance, string operations and method overloading/overriding.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

22 C++and Java Mansi Punde

The document contains details about Mansi Punde who is a student of Class DSE EXTC (B) with Roll Number 22. It then lists 20 experiments written in C++ covering topics like relational operators, arithmetic operators, logical operators, bitwise operators, if-else statements, loops, functions, classes, constructors, destructors and inheritance. It also lists 6 Java experiments covering topics like control statements, arrays, constructors, inheritance, string operations and method overloading/overriding.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 168

Name: mansi punde

Class: DSE extc (B)


Roll No: 22

C++ all Experiments


INDEX
Experiment Title of The Experiment
Number

1 To write Program using relational operators

2 To write Program using arithmetic operators

3 To write Program using logical operators

4 To write Program using bitwise operators

5 To write Program using if statements

6 To write Program to check whether number is odd


or even using if else statement.

7 To write Program to display weekdays using switch


statement.

8 To write Program to print numbers form 1 to 10


using for loop statement.
9 To write Program to display table of any number
using for loop statement.

10 Write a c++ program to generate first n terms of


Fibonacci sequence using while statement.

11 To write Program to find small and large number


using function.

12 To write Program to swap two number using


function.

13 To write Program to find simple interest using


function.

14 To write Program of overloading using different


types of parameters.

15 To write Program of overloading using different


number of parameters.

16 To write Program to add two integers using class.

17 To write Program to print two integers using


constructor.

18 To write Program using copy constructor.

19 To write Program using destructor.

20 To write Program which illustrates single


inheritance.
21 To write a Program that illustrate multi-level
inheritance.

22 To write Program that illustrate multiple


inheritance.

23 To write Program that illustrate hierarchical


inheritance.

Java experiment
INDEX
Experiment no Title of The Experiment

1 control statement

2 Array

3 coustructure in java

4 inheritance in java
5 Write a java program for string operations

6 Write a java program for method over


loading and method over riding
22_mansi_punde_2021PE0362

EXPERIMENT NO. 1

AIM: To write a program using relational operators

SOFTWARE: Online GDB compiler

THEORY: Relational Operators are used for the comparison of the values of two
operands. For example, checking if one operand is equal to the other operand or
not, an operand is greater than the other operand or not, etc. Some of the
relational operators are (==, >=, <=).

ALGORITHM:
Step 1: Start
Step 2: Input a=10, b=13

Step 3: Check condition a<b, if true then print a is less than b otherwise go to step
4

Step 4: Check condition a>b, if true then print a is greater than b otherwise go to
step 5

Step 5: Check condition a<=b, if true then print a is less than or equal to b
otherwise go to step 6

Step 6: Check condition a>=b, if true then print a is greater than or equal to b
otherwise go to step 7

Step 7: Check condition a==b, if true then print a is equal to b otherwise go to


step 8

Step 8: Check condition a!=b, if true then print a is not equal to b otherwise go to

step 9

Step 9: Stop
22_mansi_punde_extc_2021PE0362

22_mansi_punde_2021PE0362
PROGRAM:
//Relational Operators
#include<iostream.h>
#include<conio.h> void main()
{
i
n
t

a
,
b
;
a =
1
0
;
b =
1
3
;
i
f
(
a
<
b
)

{
cout<<"a is less than b";
}
if(a>b)
{
cout<<"\na is greater than b";
}
if(a<=b)
{
cout<<"\na is less than or equal to b";
}
if(a>=b) 22_mansi_punde_2021PE0362
{
cout<<"\na is greater than or equal to b";
}
if(a==b)
{
cout<<"\na is equal to b";
}
if(a!=b)
{
cout<<"\na is not equal to b";
}
}

OUTPUT:

CONCLUSION:

Hence the program has been executed successfully.


22_mansi_punde_extc_2021PE0362

22_mansi_punde_2021PE0362

EXPERIMENT NO. 2

AIM: To write a program using arithmetic operators

SOFTWARE: Online GDB compiler

THEORY: Arithmetic operators are the operators used to perform


arithmetic/mathematical operations on operands. Examples: (+, -, *, /, %, ++, –).
Arithmetic operators are of two types:

1. Unary Operators: Operators that operate or work with a single


operand are unary operators. For example: (++, –)

2. Binary Operators: Operators that operate or work with two operands


are binary operators. For example: (+, –, *, /)

ALGORITHM:

Step 1: Start
Step 2: Read a, b

Step 3: c = a+b

Step 4: Print value of c Step

5: c = a-b

Step 6: Print value of c

Step 7: c = a*b

Step 8: Print value of c

Step 9: c = a/b

Step 10: Print value of c

Step 11: c = ++a

Step 12: Print value of c


Step 13: c = --a

Step 14: Print value of c

Step 15: Stop


FLOWCHART:
22_mansi_punde_extc_2021PE0362
PROGRAM:

//Arithmetic Operators
#include<iostrea m.h>
#include<conio.h
> void main()
{

i
n
t
a
,
b
,
c
;
cout<<"Enter the value for a and b: ";
cin>>a>>b; c=a+b; cout<<"\n"<<c;
c=a-b; cout<<"\n"<<c; c=a*b;
cout<<"\n"<<c; c=a/b;
cout<<"\n"<<c; c=++a; cout<<"\n
Incrementation of a by one
"<<c; c=-a;
cout<<"\n Decrementation of a by one "<<c;
}

OUTPUT:
22_mansi_punde_extc_2021PE0362

CONCLUSION:

Hence the program has been executed successfully.


EXPERIMENT NO. 3 – Logical Operators

AIM: To write a program using logical operators

SOFTWARE: Online GDB compiler

THEORY: Logical operators are used to combine two or more


conditions/constraints or to complement the evaluation of the original condition
in consideration. The result of the operation of a logical operator is a Boolean
value either true or false. For example, the logical AND represented as ‘&&’
operator in C or C++ returns true when both the conditions under consideration
are satisfied. Otherwise, it returns false. Therefore, a && b returns true when both
a and b are true (i.e., non-zero).

ALGORITHM:

Step 1: Start

Step 2: Input a=50, b=40, c=30

Step 3: Check the condition (a < b && b < c), if true then print (a < b && b < c) condition
is true else print (a < b && b < c) condition is false

Step 4: Check the condition (a<b||b<c), if true then print (a<b||b<c) condition is
true else print (a<b||b<c) condition is false

Step 5: Check the condition !(a<b||b<c), if true then print !(a<b||b<c) condition is
true else print !(a<b||b<c) condition is false

Step 6: Stop
22_mansi_punde_extc_2021PE0362
PROGRAM:

//Logical Operators
#include<iost
ream.h> int
main ()
{ int a, b, c; a =
50; b = 40; c =
30; if (a < b && b
< c)
{
cout << "\n(a<b&&b<c) condition is true";
}
else
{
cout << "\n(a<b&&b<c) condition is false";
}
if (a < b || b < c)
{
cout << "\n(a<b||b<c) condition is true";
}
else
{
cout << "\n(a<b||b<c) condition is false";
}
if (!(a < b || b < c))
{
cout << "\n!(a<b||b<c) condition is true";
}
else
{
cout << "\n!(a<b||b<c) condition is false";
}
return 0;
}
22_mansi_punde_extc_2021PE0362

OUTPUT:

CONCLUSION:

Hence the program has been executed successfully.


EXPERIMENT NO. 4

AIM: To write a program using bitwise operators

SOFTWARE: Online GDB compiler

THEORY: The Bitwise operators are used to perform bit-level operations on the
operands. The operators are first converted to bit-level and then the calculation is
performed on the operands. The mathematical operations such as addition,
subtraction, multiplication, etc. can be performed at bit-level for faster processing.
For example, the bitwise
AND represented as & operator in C or C++ takes two numbers as operands and
does AND on every bit of two numbers. The result of AND is 1 only if both bits are
1.

ALGORITHM:

Step 1: Start

Step 2: Input a=7, b=5

Step 3: Use AND operator (a&b) and print its value

Step 4: Use OR operator (a|b) and print its value

Step 5: Use XOR operator (a^b) and print its value

Step 6: Use NOT operator (~a) and print its value

Step 7: Use NOT operator (~b) and print its value

Step 8: Use right shift operator (a>>b) and print its value

Step 9: Use left shift operator (a<<b) and print its value

Step 10: Stop


22_mansi_punde_extc_2021PE0362

FLOWCHART:

PROGRAM:
//Bitwise Operators
#include<iostream.h> #include<conio.h> int main()
{ int a = 7; int b = 5; cout<<"Bitwise Operators\n";
cout<<"a&b="<<(a&b)<<"\n";

cout<<"a|b="<<(a|b)<<"\n"; cout<<"a^b="<<(a^b)<<"\n";
cout<<"~a="<<(~a)<<"\n"; cout<<"~b="<<(~b)<<"\n";
cout<<"a>>b="<<(a>>b)<<"\n"; cout<<"a<<b="<<(a<<b)<<"\n";
}

OUTPUT:
22_mansi_punde_extc_2021PE0362

CONCLUSION:

Hence the program has been executed successfully.


EXPERIMENT NO. 5

AIM: To write a program using if-else statements

SOFTWARE: Online GDB compiler

THEORY: The if statement alone tells us that if a condition is true, it will execute a
block of statements and if the condition is false, it won’t. But what if we want to
do something else if the condition is false. Here comes the C else statement. We
can use the else statement with if statement to execute a block of code when the
condition is false.

ALGORITHM:

Step 1: Start

Step 2: Take two Input and Read the Numbers

Step 3: Check that If Number % 2 == 0

If true Then Print : Your selected Number is an Even Number.

Else Print : Your selected Number is an Odd Number.

Step 4: Stop

FLOWCHART:
22_mansi_punde_extc_2021PE0362

PROGRAM:
//Use of if-else #include <iostream.h> #include <conio.h> int main()
{
int number, remainder; cout << "Enter the
number: "; cin >> number; remainder =
number % 2; if (remainder == 0) cout <<
number << " is an even integer " << endl;
else cout << number << " is an odd integer
" << endl; return 0;
}

OUTPUT:

CONCLUSION:

Hence the program has been executed successfully.


22_mansi_punde_extc_2021PE0362

EXPERIMENT NO. 6

AIM: To write a program to check whether number is odd or even

SOFTWARE: Online GDB compiler

THEORY: A number is odd if it has 1 as its rightmost bit in bitwise representation. It


is even if it has 0 as its rightmost bit in bitwise representation. This can be found by
using bitwise AND on the number and 1. If the output obtained is 0, then the
number is even and if the output obtained is 1, then the number is odd.

ALGORITHM:

Step 1: Start

Step 2: Take Input and Read the Numbers

Step 3: Check that If Number % 2 == 0

If true Then Print : Your selected Number is an Even Number.

Else Print : Your selected Number is an Odd Number.

Step 4: Stop

FLOWCHART:
PROGRAM:
22_mansi_punde_extc_2021PE0362

//Write a program to check whether number is odd or even


#include<iostream.
h> int main()
{
int n;
cout<<
"Enter an
integer: "; cin>>
n; if( n
% 2 == 0)
cout<< n << " is even.";
else cout<< n <<
" is odd."; return 0;
}

OUTPUT:

CONCLUSION:

Hence the program has been executed successfully.


EXPERIMENT NO. 7

AIM: To write a program to display weekdays

SOFTWARE: Online GDB compiler

THEORY: Switch case statements are a substitute for long if statements that
compare a variable to several integral values The switch statement is a multiway
branch statement. It provides an easy way to dispatch execution to different parts
of code based on the value of the expression. Thus, for calling a particular day
using number assigned we can use switch case to find the probable weekday for
the given number.

ALGORITHM:

Step1: START

Step2: Declare main function as int.

Step3: Declare variable as int N.

Step4: Print “Enter a number for

Weekdays:”

Step5: Scan the value as cin>>N. Step6:


Write the switch case as switch ()

Step7: Declare case 1:

Step8: Print “MONDAY”

Step9: Break the statement using break.

Step10: Declare case 2:

Step11: Print “TUESDAY”


22_mansi_punde_extc_2021PE0362

Step12: Break the statement using break.

Step13: Declare case 3:

Step14: Print “WEDNESDAY”


Step15: Break the
statement using break.

Step16: Declare case 4:

Step17: Print “THURSDAY”

Step18: Break the statement using break.

Step19: Declare case 5:

Step20: Print “FRIDAY”

Step21: Break the statement using break.

Step22: Declare case 6:

Step23: Print “SATURDAY”

Step24: Break the statement using break.

Step25: Declare case 7:

Step26: Print “SUNDAY”

Step27: Break the statement using break.

Step28: getch()

Step29: STOP
22_mansi_punde_extc_2021PE0362

PROGRAM:

//Program to display weekdays

#include <iostream.h> #include<conio.h>


int main()
{
int week; cout<<"Enter the number
of week = "; cin>>week; switch(week)
{
case 1:
cout<<"Monday"; break;
case 2:
cout<<"Tuesday"
; break; case
3:

cout<<"Wednesday";
break; case 4:
cout<<"Thrusday";
break; case 5:
cout<<"Friday"; break;
case 6:
cout<<"Saturday";
break; case 7:
cout<<"Sunday";
break; default:
cout<<"\nInvalid number";
}
return 0;
}

OUTPUT:
CONCLUSION:

Hence the program has been executed successfully.


22_mansi_punde_extc_2021PE0362

EXPERIMENT NO. 8

AIM: To write a program to print numbers from 1 to 10

SOFTWARE: Online GDB compiler

THEORY: A for loop is a repetition control structure that allows you to efficiently
write a loop that needs to execute a specific number of times. To display the
numbers from 1 to 10 a for loop is used along with specific condition and if the
condition is true the loop continues and if the statement is false the for loop is
closed and the program is executed successfully.

ALGORITHM:

Step1: START

Step2: Declare main function as int main()

Step3: Declare variable int count

Step4: Write for loop as for(count = 1; count <= 10; count++)

Step5: Print statement printf("%d ", count);

Step6: Print statement printf("\n");

Step7: return the value as 0

Step8: STOP

FLOWCHART:
PROGRAM:

//Write a program to print numbers from 1 to 10

#include <iostream.h>
#include

<stdio.h>
22_mansi_punde_extc_2021PE0362

#include

<conio.h> int

main ()

{
int count; for(count

= 1; count <= 10;

count++) printf("%d ", count);

printf("\n"); return 0;

OUTPUT:

CONCLUSION:

Hence the program has been executed successfully.


EXPERIMENT NO. 9

AIM: To write a program to display table of any number

SOFTWARE: Online GDB compiler

THEORY: The Program takes the input from the user,and muotiplies it with the
value of i which starts from 1 and later goes on incrementing till i=10 then stops.

ALGORITHM:

Step 1: Start

Step 2: Declare i=1,n as two variables.

Step 3: Enter the Number for which you want to find the table of which would
save in 'n'.

Step 4: For i=1;i<=10;++i

Step 5: n*i

Step 6: Print the numbers

Step 7: Stop

FLOWCHART:
22_mansi_punde_extc_2021PE0362
PROGRAM:

//Write a program to display table of any number

#include<iostream.h>

#include<stdio.h>

#include<conio.h> int main

()

cout<<"Enter any number:";

cin>> n; for(i = 1; i

<= 10; ++i)

cout<<"\n"<<n<<" *
"<<i<<" = "<<n*i; return

0;

OUTPUT:
22_mansi_punde_extc_2021PE0362

CONCLUSION:

Hence the program has been executed successfully.


EXPERIMENT NO. 10

AIM: To write a program

SOFTWARE: Online GDB compiler

THEORY: A C++ class combines data and methods for manipulating the data into one.
Classes also determine the forms of objects. The data and methods contained in a
class are known as class members. A class is a user-defined data type. To access the
class members, we use an instance of the class. You can see a class as a blueprint for
an object.

ALGORITHM:

Step 1: Start

Step 2: Declare x and y as two variables.

Step 3: Enter the Two Numbers to be added.

Step 4: x+y

Step 5: Print the result

Step 6: Stop

FLOWCHART:
22_mansi_punde_extc_2021PE0362

PROGRAM:

#include <stdio.h>
#include
<iostream.h>
#include
<conio.h>
class math
{ int
x, y;
pu
blic
:
voi
d
inp
ut()
{
cout<<"Input two integers\n"; cin>>x>>y;
}
void add()
{
cout<<"Result:"<<x + y;
}
};

int main()
{
math s;
s.input();
s.add(); return
0;
}
OUTPUT:

CONCLUSION:

Hence the program has been executed successfully.


22_mansi_punde_extc_2021PE0362

EXPERIMENT NO. 11

AIM: To write a program to find small and large number from given array

SOFTWARE: Online GDB compiler

THEORY: An array in C++ or be it in any programming language is a collection of


similar data items stored at contiguous memory locations and elements can be
accessed randomly using indices of an array. They can be used to store collection
of primitive data types such as int, float, double, char, etc of any particular type.
To add to it, an array in C/C++ can store derived data types such as the structures,
pointers etc.

We can use normal variables (v1, v2, v3, ...) when we have a small
number of objects, but if we want to store a large number of instances, it
becomes difficult to manage them with normal variables. The idea of an array is to
represent many instances in one variable.

ALGORITHM:

Step 1: Start

Step 2: Read a[50], i, n, small, large

Step 3: Print Enter the array size

Step 4: for(i=0, i<n, i++)

Read a[i] Step 5: small

= a[0]

large = a[0]

Step 6: for(i=0, i<n, i++)


{
if(a[i]<small)
small = a[i];
if(a[i]>large) large
= a[i];
}

Step 7: Print Largest value is

Step 8: Print Smallest value is


22_mansi_punde_extc_2021PE0362

Step 9: Stop
Stop

PROGRAM:

//write a program to find small and large number from given


array
#include <iostream.h>
int main()
{ int a[50], i, n, small, large;
cout<<"Enter the array size: ";
cin>>n; cout<<"Enter
elements of array: ";
for(i=0;i<n;i++) cin>>a[i];
small = a[0]; large = a[0];
for(i=0;i<n;i++)
{
if(a[i]<sm
all)
small =
a[i];
if(a[i]>larg
e)
large =
a[i];
}
cout<<"Largest value is:"<<large<<endl; cout<<"Smallest
value is:"<<small<<endl; return
0;
}

OUTPUT:

CONCLUSION:
22_mansi_punde_extc_2021PE0362

Hence the program has been executed successfully.


EXPERIMENT NO. 12

AIM: To write a program to swap two numbers using function

SOFTWARE: Online GDB compiler

THEORY: In swapping of two numbers without using temporary variables we


take any two variables a and b and we store the two numbers to be swapped
in a and b. Then we use addition and subtraction operation to swap two
numbers. The idea is to get a sum in one of the two given numbers. The
numbers can then be swapped using the sum and subtraction from the sum.

ALGORITHM:

Step 1: Start

Step 2: Read a, b i.e., the two numbers to be swapped

Step 3: Print values of a and b before swapping

Step 4: swap(a, b)

Step 5: b = a + b

Step 6: a = b - a

Step 7: b = b - a

Step 8: Print values of a and b after swapping

Stap 9: Stop

FLOWCHART:
22_mansi_punde_extc_2021PE0362

PROGRAM:

//write a program to swap two numbers


using function #include <iostream.h>
#include <conio.h> void swap(int a, int b) {
b = a + b; a = b - a; b = b - a;
cout<<"\nAfter swapping: ";
cout<<"a="<<a; cout<<"\tb="<<b;
}
int main()
{
i
n
t

a
,
b
;
cout<<"Enter the two numbers to be swapped: ";
cin>>a>>b; cout<<"a="<<a; cout<<"\t b="<<b; swap(a,b);
}

OUTPUT:
CONCLUSION:

Hence the program has been executed successfully.


22_mansi_punde_extc_2021PE0362

EXPERIMENT NO. 13
AIM: To write a program to swap two numbers using call by reference, call by

address and call by value

SOFTWARE: Online GDB compiler

THEORY: Call by reference method copies the address of an argument into the
formal parameter. In this method, the address is used to access the actual argument
used in the function call. It means that changes made in the parameter alter the
passing argument. In this method, the memory allocation is the same as the actual
parameters. All the operation in the function are performed on the value stored at
the address of the actual parameter, and the modified value will be stored at the
same address.

Call By Address is also known as Call By Pointers. In this method, the programmer
passes the addresses of the actual arguments to the formal parameters. Then, the
function uses the addresses to access the actual arguments. In other words, the
changes made to the formal parameters affect the actual arguments. To pass the
value by pointer, the argument pointers are passed to the functions similar to any
other value.

Call by value method copies the value of an argument into the formal parameter of
that function. Therefore, changes made to the parameter of the main function do
not affect the argument. In this parameter passing method, values of actual
parameters are copied to function’s formal parameters, and the parameters are
stored in different memory locations. So any changes made inside functions are
not reflected in actual parameters of the caller.

ALGORITHM:

Step 1: Start.

Step 2: Initialize a=100, b=200.

Step 3: Print “Call by value”.

Step 4: Print “Before swapping a=(value of a), b=(value of b)”.

Step 5: Call function “swapval” with a and b as arguments.


Step 6: Print “After swapping a=(value of a), b=(value of b)”.

Step 7: Print “Call by reference”.

Step 8: Print “Before swapping a=(value of a), b=(value of b)”.

Step 9: Call function “swapref” with a and b as arguments.

Step 10: Print “After swapping a=(value of a), b=(value of b)”.

Step 11: Print “Call by address”.

Step 12: Print “Before swapping a=(value of a), b=(value of b)”.

Step 13: Call function “swapadr” with &a and &b as arguments.

Step 14: Print “After swapping a=(value of a), b=(value of b)”.

Step 15: Create a function “swapval” to swap using call by value initializing

parameters x, y. Step 16: Initialize z=x then x=y then y=z.

Step 17: Create a function “swapref” to swap using call by reference initializing
parameters &x, &y.

Step 18: Initialize z=x then x=y then y=z.

Step 19: Create a function “swapadr” to swap using call by address initializing
parameters *x, *y.

Step 20: Initialize z = *x then *x = *y then *y = z.

Step 21: Stop.


22_mansi_punde_extc_2021PE0362

FLOWCHART:
22_mansi_punde_extc_2021PE0362
*y = z

Print “After swapping a=(value


of a), b=(value of b)”.

Stop
22_mansi_punde_extc_2021PE0362

PROGRAM:

//PROGRAM:(CALL BY VALUE, CALL BY REFERENCE AND CALL BY ADDRESS)


#include<iostream
.h>
#include<conio.h>
void swapval(int,int);
void swapref(int
&x,int &y); void
swapadr(int*,int*)
; void main()
{
int a=100,b=200; clrscr();
cout<<"Call by value"; cout<<"\n
Before swapping a="<<a<<"
b="<<b; swapval(a,b); cout<<"\n
After swapping a="<<a<<"
b="<<b; cout<<"\n\nCall by
Reference"; cout<<"\n Before
swapping a="<<a<<" b="<<b;
swapref(a,b); cout<<"\n After
swapping a="<<a<<" b="<<b;
cout<<"\n\nCall by Address";
cout<<"\n Before swapping
a="<<a<<" b="<<b;
swapadr(&a,&b);
cout<<"\n AfterSwapping a="<<a<<" b="<<b; getch();
}
void swapval(int x,int y)
{
int z=x; x=y; y=z;
}
void swapref(int &x, int &y)
{
i
n
t
z
=
x
;
x
=
y
;
y
=
z
;

void swapadr( int*x,int*y)


{
int z=*x; *x=*y;
*y=z;
}
OUTPUT:

CONCLUSION:

Hence the program has been executed successfully.


22_mansi_punde_extc_2021PE0362

EXPERIMENT NO. 14

AIM: To write a program using

SOFTWARE: Online GDB compiler

THEORY: A constructor in C++ is a special ‘MEMBER FUNCTION’ having the same

name as that of its class which is used to initialize some valid values to the data

members of an object. It is executed automatically whenever an object of a class

is created. The only restriction that applies to the constructor is that it must not

have a return type or void. It is because the constructor is automatically called by

the compiler and it is normally used to INITIALIZE VALUES. The compiler

distinguishes the constructor from other member functions of a class by its name

which is the same as that of its class.

ALGORITHM:

Step 1: Start.

Step 2: Introduce class named “integer”.

Step 3: Initialize x, y.

Step 4: Create public function called “integer” under class “integer”.

Step 5: Create public function called “integer” with parameters (int x1, int y1) under

class

“integer”.

Step 6: x=x1 and y=y1.

Step 7: Create function “show” under class “integer”.

Step 8: Print ”x is = (value of x)” under function “show”.

Step 9: Print “y is = (value of y)” under function “show”.


Step 10: Call function “integer i” for first integer function with no parameters, in
main function.

Step 11: Call function “integer j” with (10,20) as arguments for second integer
function with (int x1, int y1) as parameters, in main function.

Step 12: Call function show to print values of x and y, in main function.

Step 13: Stop.


22_mansi_punde_extc_2021PE0362
//Constructor #include <iostream.h> #include<conio.h> class integer { int x; int y; public: integer()
{

}
integer(int x1,int y1)
{
x=x1;
y=y1;
}
void show()
{
cout<<"x is = "<<x; cout<<"\ty is
= "<<y;
} };
void main()
{
integer i; integer j(10,20);
j.show();
}
OUTPUT:

CONCLUSION:

Hence the program has been executed successfully.


22_mansi_punde_extc_2021PE0362

EXPERIMENT NO. 15

AIM: To write a program using

SOFTWARE: Online GDB compiler

THEORY: Copy Constructor is a type of constructor which is used to create a


copy of an already existing object of a class type. It is usually of the form X
(X&), where X is the class name. The compiler provides a default Copy
Constructor to all the classes. As it is used to create an object, hence it is called
a constructor. And, it creates a new object, which is exact copy of the existing
copy, hence it is called copy constructor.

ALGORITHM:

Step 1: Start

Step 2: Declare a class integer.

Step 3: Declare the variable x and y.

Step 4: Create a constructor and initialize the variable.

Step 5: Create a copy constructor by passing object as a parameters.

Step 6: Create a function show to display the value of variable.

Step 7: Declare a Destructor.

Step 8: Create a function show to display the value of variable.

Step 9: Call the constructor ( i ) by passing previous object as a parameter.

Step 10: Create an object k of class integer.

Step 11: Assign k with the values of j.

Step 12: Call the function show of object ( j ) Step


13: Call the function show of object ( i ) Step 14:

Call the function show of object ( k )

Step 15: Stop.


Call the function show of object ( k )

PROGRAM: Stop

#include<iostream.h>
#include <conio.h> class
integer
{
in
t
x;
in
t
y;
p
u
bl
ic
:
in
t
e
g
e
r(
)
{

}
integer(int x1, int y1)
{
x=x
1;
y=y
1;
}
integer(integer &m)
{

x=m.x; y=m.y;
}
void show()
{
22_mansi_punde_extc_2021PE0362

cout<<"\tx is = "<<x; cout<<"\ty


is = "<<y;
}
};
void main()
{
integer j(10,20);
integer i(j);
integer k=j;
j.show();
i.show();

k.show(); getch();
}
OUTPUT:

CONCLUSION:

Hence the program has been executed successfully.


EXPERIMENT NO. 16

AIM: To write a program using

SOFTWARE: Online GDB compiler

THEORY: Destructor is a special class function which destroys the object as


soon as the scope of object ends. The destructor is called automatically by
the compiler when the object goes out of scope. It is necessary to cleanup
for a class object and its class members when the object is destroyed.
ALGORITHM:

Step 1: Start

Step 2: Declare a class integer.

Step 3: Declare a variable x & y

Step 4: Create a constructor and initialize the variable.

Step 5: Declare a Destructor.

Step 6: Create a function show to display the value of variable.

Step 7: Create a main function.

Step 8 : Call the constructor of class integer with the required parameters.

Step 9: Call the function show.

Step 10: Stop.

P
R
O
G
R
A
M
:
22_mansi_punde_extc_2021PE0362

//Dest ructor
#include
<iostrea
m.h>
#include
<conio.h
> class
integer
{
i
n
t

x
;
i
n
t

y
;

p
u
b
22_mansi_punde_extc_2021PE0362

ic
:
in
t
e
g
e
r(
)
{

}
integer(int x1,int y1)
{
x=x
1;
y=y
1;
}
~integer()
{
cout<<"\nExit";
}
void show()
{
cout<<"\tx is = "<<x; cout<<"\ty
is = "<<y;
}
};
void main()
{
integer j(10,20);
j.show(); getch();
}
OUTPUT:
CONCLUSION:

Hence the program has been executed successfully.


22_mansi_punde_extc_2021PE0362

EXPERIMENT NO. 17

AIM: To write a program using

SOFTWARE: Online GDB compiler

THEORY: Functions in C++ have an ability to assign default values for function. If
we donot pass values for such arguments then their default values are used. If we
pass values for such arguments then the default values are overridden and the
passed values are used.

ALGORITHM:

Step 1: Start

Step 2: Read values of p,n,r

Step 3: Print default argument p=1000.00, n=2, r=5.0

Step 4: Print simple interest with 3 default arguments i.e. si()

Step 5: Print simple interest with 2 default arguments i.e. si(p)

Step 6: Print simple interest with 1 default arguments i.e. si(p,n)

Step 7: Print simple interest without default arguments i.e. si(p,n,r)

Step 8: Read pr,no,ra

Step 9: Return value for [(pr*no*ra)/100]

Step 10: Stop

FLOWCHART:
PROGRAM:

//Simple interest using function


//Function with default argument
#include <iostream.h>
#include <conio.h>
float si(float p=1000.00, int n=2, float r=5.0); void
main() { clrscr(); float p,r; int n; cout<<"\nEnter
principal amount:"; cin>>p; cout<<"\nEnter number
of year:"; cin>>n; cout<<"\nEnter rate of interest:";
cin>>r; cout<<"\ndefault argument p=1000.00, n=2
& r=5.0"; cout<<"\nsimple interest with 3 default
arguments = "<<si(); cout<<"\nsimple interest with 2
default arguments = "<<si(p); cout<<"\nsimple
interest with 1 default argument = "<<si(p,n);
cout<<"\nsimple interest without default argument
= "<<si(p,n,r); getch();
}
float si(float pr,int no,float ra)
{
return((pr*no*ra)/100);
}
OUTPUT:
22_mansi_punde_extc_2021PE0362

CONCLUSION:

Hence the program has been executed successfully.


EXPERIMENT NO. 18

AIM: To write a program

SOFTWARE: Online GDB compiler

THEORY: C++ allows to create different functions with the same name whereas in
C every function in a program has to have a unique name. C++ compiler knows
which of the absolute() should be called when call is made. It decides that from
the type of argument passed. For example, if an int is being passed the integer
version of absolute() gets called.

ALGORITHM:

Step 1: Start

Step 2: Step 1: Start

Step 2: Read float var for absolute value. (float type of argument is being passed
during function call. Therefore, float version of absolute() gets called.)

Step 3: if (var<0.0), var=-var

Step 4: Return value of var

Step 5: Read int var for absolute value. (int type of argument is being passed
during function call. Therefore, int version of absolute() gets called.)

Step 6: if (var<0), var=-var

Step 7: Return value of var

Step 8: Print Absolute value of -5

Step 9: Print Absolute value of 5.5

Step 10: Stop


22_mansi_punde_extc_2021PE0362
PROGRAM:

//Overloading using different types of parameters


#include <iostream.h> float absolute(float
var)
{
if(var
<0.0)
var=-
var;
retur
n var;
}

int absolute(int var)


{
if(var
<0)
var=-
var;
retur
n var;
}

int main()
{
cout<<"Absolute value of -5 =\t"<<absolute(-5)<<endl;
cout<<"Absolute value of 5.5 =\t"<<absolute(5.5f)<<endl;
return
0;
}
OUTPUT:
22_mansi_punde_extc_2021PE0362

CONCLUSION:

Hence the program has been executed successfully.


EXPERIMENT NO. 19

AIM: To write a program

SOFTWARE: Online GDB compiler

THEORY: Function Overloading is defined as the process of having two or more


function with the same name, but different in parameters is known as function
overloading in C++. In function overloading, the function is redefined by using
either different types of arguments or a different number of arguments. It is
only through these differences compiler can differentiate between the
functions.

The advantage of Function overloading is that it increases the


readability of the program because you don't need to use different names for the
same action.

ALGORITHM:

Step 1: Start

Step 2: Declare two parameters , int var1 , double var2

Step 3: Print “Integer number:” and scan the value as var1

Step 4: Print “Double number:” and scan the value as var2

Step 5: Declare function with double type single parameter , double var

Step 6: Print “Double number:” and scan the value as var.

Step 7: Declare function with int type single parameter , int var

Step 8: Print “Integer number:” and scan the value as var

Step 9: Declare main function , int main()

Step 10: Declare int a=5; double b=5.5;

Step 11: Call the function with int type parameter , display(a);
22_mansi_punde_extc_2021PE0362

Step 12: Call the function with double type parameter , display(b);

Step 13: Call the function with two parameters , display(a,b);

Step 14: Stop.


22_mansi_punde_extc_2021PE0362

PROGRAM:

//Overloading using different number of parameters

#include<stdio.h>

#include<conio.h>
#include<iostream.h>

//Function with two parameter void


display(int var1, double var2)
{
cout<<"integer number: "<<var1; cout<<"\tand
double number: "<<var2<<endl;
}

//function with double type single parameter void display(double


var)
{
cout<<"Double number: "<<var<<endl;
}

//function with int type single parameter void


display(int var)
{
cout<<"integer number: "<<var<<endl;
}

int main()
{ int
a=5; double
b=5.5; //
calling
functions
display(a
);
display(b
);
display(a
,b);
return 0;
}
OUTPUT:
CONCLUSION:

Hence the program has been executed successfully.


22_mansi_punde_extc_2021PE0362

EXPERIMENT NO.21

AIM: To write a program to generate first n terms of Fibonacci sequence

SOFTWARE: Online GDB compiler

THEORY: The Fibonacci numbers are the numbers in the following integer
sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……...
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence
relation.

ALGORITHM:

Step 1: Start

Step 2: Declare function as “fib” and under that declare “int n”

Step 3: Declare variables as f0, f1, f, count=0

Step 4: Initialize the variables , f0=0, f1=1

Step 5: Use while loop and under that declare the condition as count<n

Step 6: Declare the following conditions under while loop:

 cout<<f0<<"\t";
 count++;
 f=f0+f1;
 f0=f1;
 f1=f;
Step 7: Declare main function, int main()
Step 8: Declare variable “terms”
Step 9: Enter the number of terms of Fibonacci series to be printed
Step 10: Scan the value as “terms”
Step 11: Declare the function ,
fib(terms) Step 12:
Stop.
22_mansi_punde_extc_2021PE0362

PROGRAM:

//Write a C++
program to
generate first n
terms of
Fibonacci
sequence
#include<iostrea
m.h> void fib(int
n)
{
int
f0,f1,f,count=0;
f0=0; f1=1;
while(count<n)
{

cout<<f0
<<"\t";
count++;
f=f0+f1;
f0=f1;
f1=f;

int main()
{ int terms;
cout<<"Enter
how many terms
to be printed:";
cin>>terms;
fib(terms); return 0;
}
}
}
22_mansi_punde_extc_2021PE0362

OUTPUT:

CONCLUSION:

Hence the program has been executed successfully.


EXPERIMENT NO. 22

AIM: To write a program which illustrates multiple inheritance

SOFTWARE: Online GDB compiler

THEORY: Multiple Inheritance is a feature of C++ where a class can inherit from more
than one classes. i.e., one sub class is inherited from more than one base classes.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the base class student.

Step 3: Declare and define the function to get the student details.

Step 4: Declare the other class sports.

Step 5: Declare and define the function to get and read the sports mark.

Step 6: Create the class statement derived from student and sports.

Step 7: Declare and define the function to display and find out the total and
average.

Step 8: Declare the derived class object, call the functions and display.

Step 9: Stop the program.


22_mansi_punde_extc_2021PE0362
PROGRAM:

//Write a C++ Program that illustrate multipe inheritance


#include<iostream.h>
#include<conio.h> class student
{
prote
cted: int
rno,
m1,m
2;
publi
c:
void
get()
{
cout<<"Enter the Roll no :"; cin>>rno;
cout<<"Enter the two marks :"; cin>>m1>>m2;
}
};

class sports
{
protected:
int sm; //sm = Sports
mark public: void getsm()
{
cout<<"\nEnter the sports mark :"; cin>>sm;
}
};

class statement:public student,public sports


{

int tot,avg;
public:
22_mansi_punde_extc_2021PE0362

void
display()
{
tot=(m1+m2+sm); avg=tot/3; cout<<"\n\n\tRoll No
: "<<rno<<"\n\tTotal : "<<tot; cout<<"\n\tAverage
:"<<avg;
}
};

void main()
{
clrscr();

statement
obj; obj.get();
obj.getsm();
obj.display(); getch();
}

OUTPUT:

CONCLUSION:

Hence the program has been executed successfully.


EXPERIMENT NO. 23

AIM: To write a program which illustrates single inheritance

SOFTWARE: Online GDB compiler

THEORY: In single inheritance, a class is allowed to inherit from only one class.

i.e. one sub class is inherited by one base class only.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the base class emp.

Step 3: Define and declare the function to get the employee details.

Step 4: Declare the derived class salary.

Step 5: Declare and define the function to get the salary details.

Step 6: Define the function to calculate and find the net pay.

Step 7: Define the function display.

Step 8: Create the derived class object.

Step 9: Read the number of employees.

Step 10: Call the function get and calculate to each employee.

Step 11: Call the display.

Step 12: Stop the program.


that illustrate single inheritance
#include<iostream.h>
#include<coni o.h>
class A

{
protected: int
a,b;
publ
ic:
void
get()
{
cout<<"Enter any two integer values: "; cin>>a>>b;
}
};

class B: public A
{
int c; public: void add()
{
c=a+b; cout<<a<<"+"<<b<<"="<<c;
} };
int main()
{
B b; b.get();
b.add(); return
0;
}
OUTPUT:
22_mansi_punde_extc_2021PE0362
CONCLUSION:

Hence the program has been executed successfully.


EXPERIMENT NO. 24

AIM: To write a program that illustrate multi-level inheritance

SOFTWARE: Online GDB compiler

THEORY: In multi-level inheritance, a derived class is created from another derived


class.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the base class student.

Step 3: Declare and define the function to get the student details.

Step 4: Declare the other class sports.

Step 5: Declare and define the function to read the sports mark.

Step 6: Create the class statement derived from student and sports.

Step 7: Declare and define the function to display and find out the total and
average.

Step 8: Declare the derived class object, call the functions and display.

Step 9: Stop the program.


22_mansi_punde_extc_2021PE0362

PROGRAM: //Write a C++ Program that illustrate multi-level inheritance


#include<iostream.h>
#include<conio.h> class top //base class
{
public
: int
a;
void
getda
ta()
{
cout<<"Enter first Number: "; cin>>a;
}
void putdata()
{
cout<<"\nFirst Number Is: "<<a;
}
};
//First level inheritance
class middle :public top // class middle is derived_1
{
public
: int b;
void
squar
e()
{
getdata(); b=a*a;
cout<<"\n\nSquare: "<<b;
}
};
//Second level inheritance class bottom
:public middle // class bottom is derived_2
{
p
u
b
l
i
c
22_mansi_punde_extc_2021PE0362
:i
n
t
c
;v
o
i
d
c
u
b
e
(
) {
square(); c=b*a; cout<<"\n\nCube:
"<<c;
} };
int main() {
c
l
r
s
c
r
(
)
;
b
o
t
t
o
m
b
1
;
b
1
.
c
u
b
e
(
);
getch();
}
OUTPUT:

CONCLUSION:

Hence the program has been executed successfully.


22_mansi_punde_extc_2021PE0362

EXPERIMENT NO. 25

AIM: To write a program that illustrate Hierarchical inheritance

SOFTWARE: Online GDB compiler

THEORY: In hierarchical inheritance, more than one sub class is inherited from a
single base class. i.e. more than one derived class is created from a single base
class.

ALGORITHM:

Step 1: Start

Step 2: Declare the vales of a

Step 3: Square the value of a

Step 4: Cube the value of a

Step 5: Print the value of both cases a

Step 6: Stop.

FLOWCHART:
PROGRAM:

//Write a C++ Program that illustrate


Hierarchical inheritance
#include<iostream.h>
#include<conio.h> class A //Base Class
{
public: int

a, b;
void getnumber()

{
cout<<"\nEnter
Number: "; cin>>a;
} };
class B : public A //Derived Class 1
{
public: void
square()
{
getnumber(); //Call Base class property cout<<"\nSquare
of the number: "<<(a*a); cout<<"\n";
} };
class C :public A //Derived Class 2
{
public: void cube()
{
getnumber(); //Call Base class property cout<<"\nCube
of the number: "<<(a*a*a); cout<<"\n";
} };
int main()
{ clrscr();
B b1; //b1 is object of Derived class 1
b1.square(); //call member function
of class B C c1; //c1 is object of
Derived class 2
c1.cube(); //call member function of
class C
22_mansi_punde_extc_2021PE0362
getch();
}
OUTPUT:

CONCLUSION:

Hence the program has been executed successfully.


43_mansi_punde_2021PE0362

JAVAJOURNAL

NAME: MANSI PUNDE

ROLL NO: 22

CLASS: DSE BRANCH:

EXTC

DIV: B

DATE:

1
EXPERIMENT NO:1 JAVA CONTROL STATEMENT

Aim: To write a program for control statements in java.

Software: Eclipse.

Theory: Java is a general-purpose, class-based, object-oriented programming


language designed for having lesser implementation dependencies. It is a
computing platform for application development. Java is fast, secure, and
reliable, therefore. It is widely used for developing Java applications in laptops,
data centers, game consoles, scientific supercomputers, cell phones, etc.

Java compiler executes the code from top to bottom. The statements in the code
are executed according to the order in which they appear. However, java provides
statements that can be used to control the flow of Java code. Such statements
are called control flow statements. It is one of the fundamental features of Java,
which provides a smooth flow of program.

Java provides three types of control flow statements.

1. Decision Making statements o if statements o switch statement

2. Loop statements

o do while loop o

while loop o for loop

o for-each
loop

3. Jump statements

o break statement

2
o continue statement
EXPERIMENT NO:1.1 IF DEMO STATEMENT.

Aim: To write a program using if statement demo in java.

Software: Eclipse.

Program: public class


IfElseDemo {

public static void main(String[] args) { // TODO

Auto-generated method stub int a=10,

b=20; if(a>b)

System.out.println("a>b");
} else
{

System.out.println("b>a");
}

}
}

Output:

3
43_Anisha_Bhapkar_2021PE0618
Conclusion:

Thus, we have successfully executed the if-demo statement in java.


The output for the program for the numbers of voters not eligible is obtained on
the output window.
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT NO:1.2 IF-ELSE DEMO.

Aim:To write a program using if-else statement demo for calculating the bigger
and smaller number in java.

Software: Eclipse.

Program: public class


IfElseDemo {

public static void main(String[] args) { //

TODO Auto-generated method stub int

a=10, b=20; if(a>b)

System.out.println("a>b");

} else

System.out.println("b>a");

Output:

5
43_Anisha_Bhapkar_2021PE0618

Conclusion:

Thus, by the above experiment we have understood how to write a


program using if-else statement demo for calculating the bigger and smaller number
in java and successfully executed the code.
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT NO:1.3 FOR STATEMENT.

Aim:To write a program for executing for statement.

Software: Eclipse.

Program: public class


ForDemo {

public static void main(String[] args)

// TODO Auto-generated method stub int

i=1;

System.out.println("Odd Numbers between 1 to 20 are");

for(;i<=20;i=i+2)

System.out.println(i);

7
43_Anisha_Bhapkar_2021PE0618

Output:

Conclusion:

Thus, by the above experiment we have successfully executed the


code and implemented the for statement and the output for the loop is obtained on
the output window.
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT NO 1.4 WHILE STATEMENT.

Aim:To write a program for while statement in java.

Software: Eclipse.

Program: public class

WhileDemo {

public static void main(String[] args)

// TODO Auto-generated method stub

int i=1; while(i<=20)

System.out.println(i);

i=i+2;

9
43_Anisha_Bhapkar_2021PE0618
}

Output:

Conclusion:

Thus, we have successfully executed the program with the control


statement and the output for the respective for loop is displayed on the output
window.
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT 1.5 DO WHILE DEMO.

Aim: To write a program to execute do-while statement in java.

Software: Eclipse.

Program:
class DoWhileDemo
{ public static void main(String args[])
{ int i=2;// setting the value to 2 do
{
System.out.println(i);// displaxy i i=i+2;
// increment the value by 2
}while(i<=20);
}
}

Output:

Conclusion:

Thus, the program for the do-while statement and the loop is

11
executed successfully. The output for the loop is displayed on the output window.

12
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT NO 1.6 FIBONACCI TERMS.

Aim:To write a program for displaying first 10 terms for Fibonacci series.

Software: Eclipse

Program:
import java.util.*; class Fibonacci
{ public static void main (String args [])
{ int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+"
"+n2);

for(i=2;i<count;++i)
{ n3=n1+n2
;
System.out.print("
"+n3); n1=n2; n2=n3;
}
}

} Output:

13
Conclusion:

Thus, the program for Fibonacci series is executed successfully and the terms are

displayed on the window screen.

EXPERIMENT NO: 2 JAVA ARRAY

14
43_Anisha_Bhapkar_2021PE0618

Aim:To write a program for creating arrays and performing different


operation for the same in java.

Software: Eclipse.

Theory: Java array is an object which contains elements of a similar data type.
Additionally, the elements of an array are stored in a contiguous memory location.
It is a data structure where we store similar elements. We can store only a fixed
set of elements in a Java array. Array in Java is index-based, the first element of
the array is stored at the 0th index, 2nd element is stored on 1st index and so
on.Unlike C/C++, we can get the length of the array using the length member. In
C/C++, we need to use the size of operator.In Java, array is an object of a
dynamically generated class. Java array inherits the Object class, and implements
the Serializable as well as Cloneable interfaces. We can store primitive values or
objects in an array in Java. Like C/C++, we can also create single dimentional or
multidimentional arrays in Java.

EXPERIMENT NO:2A LARGEST JAVA

Aim:To write a program for finding the largest number for the given array in java.

15
Software: Eclipse.

Program: public class


Largest {

public static void main(String[] args)

// TODO Auto-generated method stub

int i, max; int a[]=

{10,20,30,40,50};

System.out.println("Array elements are: "); for(i=0;i<5;i++)

System.out.println(a[i]+"¥t");

} max=a[0]; for(i=1;i<5;i++

{ if(a[i]>max)

max=a[i];

16
43_Anisha_Bhapkar_2021PE0618
}

System.out.println("¥nLargestelementfromthearrayis"+max);

Output:

Conclusion:

Thus, the program for the java array is executed successfully. The
largest array element for the array is displayed on the output screen.

17
43_Anisha_Bhapkar_2021PE0618
EXPERIMENT NO: 2B 2-D ARRAY ADDITION.

Aim: To write a program for creating a 2-D array in java.

Software: Eclipse.

Program: public class


TwoDArrayAddition {

public static void main(String[] args)

// TODO Auto-generated method stub

int i, j; int add[][]=new int[3][3]; int set1[][]=

{ {1,3,5},{7,9,11},{13,15,17} }; int set2[][]= {


{2,4,6},{8,10,12},{14,16,18} };

System.out.print("The first 3X3 matrix is:¥n");


for(i=0;i<3;i++)

{
for(j=0;j<3;j++)
System.out.print(set1[i][j]+"¥t");
System.out.println();

System.out.println();

System.out.println("The second 3X3 matrix is:¥n");


for(i=0;i<3;i++)

{
for(j=0;j<3;j++)

18
43_Anisha_Bhapkar_2021PE0618
System.out.print(set2[i][j]+"¥t");
System.out.println();

}
System.out.println(); for(i=0;i<3;i++)

{ for(j=0;j<3;j++)

add[i][j]=set1[i][j] + set2[i][j];

System.out.println("The resultant addition 3X3 matrix is:

"); for(i=0;i<3;i++)
{ for(j=0;j<3;j++)

System.out.print(add[i][j]+"¥t");
System.out.println();

Output:

19
43_Anisha_Bhapkar_2021PE0618

Conclusion:

Thus, the program for the 2-D array is created successfully. The addition of
two input arrays is obtained in the output window.

20
43_Anisha_Bhapkar_2021PE0618
EXPERIMENT NO:3 JAVA CONSTRUCTORS.

Aim: To write a program for creating the default constructors in java.

Software: Eclipse.

Theory: A constructor initializes an object when it is created. It has the same name
as its class and is syntactically similar to a method. However, constructors have no
explicit return type. Typically, you will use a constructor to give initial values to the
instance variables defined by the class, or to perform any other start-up
procedures required to create a fully formed object. All classes have constructors,
whether you define one or not, because Java automatically provides a default
constructor that initializes all member variables to zero. However, once you define
your own constructor, the default constructor is no longer used.

21
43_Anisha_Bhapkar_2021PE0618
EXPERIMENT NO: 3A DEFAULT CONSTRUCTORS.
Aim: To write a program multiplication of numbers using the default constructors in
java.

Software: Eclipse.

Program: public class


DefaultConstructorDemo

int x, y;

DefaultConstructorDemo()

{ x=10; y=3;

} void

mul()

{ int m= x*y;

System.out.println("Valueaftermultiplicationof2numbersis
"+m);

public static void main(String[] args)

// TODO Auto-generated method stub

DefaultConstructorDemo obj= new DefaultConstructorDemo();

obj.mul();

22
43_Anisha_Bhapkar_2021PE0618
}

Output:

Conclusion:

Thus, the program for the default constructors is created and executed
successfully. The multiplication for the input two numbers is displayed on the output
screen.

23
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT NO: 3B OVERLOADED CONSTRUCTOR.

Aim: To write a program for creating an overloaded constructor in java.

Software: Eclipse.

Program:
public class OverloadedConstructorDemo
{ int num1, num2;

OverloadedConstructorDemo(int a)

num1=num2=a;

OverloadedConstructorDemo(int a, int b)
{ num1=a; num2=b;

public static void main(String[] args)

// TODO Auto-generated method stub

OverloadedConstructorDemo p1= new


OverloadedConstructorDemo(5);

OverloadedConstructorDemo p2= new


OverloadedConstructorDemo(5,6);
p1.calculate();

p2.calculate();

24
43_Anisha_Bhapkar_2021PE0618
} void calculate()

{ int result=num1*num2;

System.out.println("The product of numbers= "+result);

Output:

Conclusion:

Thus, the program for the overloaded constructor is created successfully.


The product of the numbers along with the new product after the overloading is
obtained on the output window.

25
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT NO:4 JAVA INHERITANCE.

Aim: To write the program for the java inheritance.

Software: Eclipse.

Theory: Inheritance in Java is a mechanism in which one object acquires all the
properties and behaviours of a parent object. It is an important part of OOPs
(Object Oriented programming system).The idea behind inheritance in Java is that
you can create new classes that are built upon existing classes. When you inherit
from an existing class, you can reuse methods and fields of the parent class.
Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child
relationship.

26
43_Anisha_Bhapkar_2021PE0618
EXPERIMENT NO:4A HEIR INHERITANCE.

Aim: To write a program for heir inheritance in java for creating the steps to drive.

Theory: Eclipse.

Program: class
Vehicle

{ public void start()

System.out.println("Starting a vehicle");

class Car extends Vehicle

{ public void drive()

System.out.println("Driving a Car");

class Aircraft extends Vehicle

{ public void fly()

27
43_Anisha_Bhapkar_2021PE0618
System.out.println("Flying a plane");

public class Heir_inh { public static void

main(String args[])

Car c = new Car();

c.start();

c.drive();

System.out.println();

Aircraft j = new Aircraft ();

j.start();

j.fly();

Output:

28
43_Anisha_Bhapkar_2021PE0618

Conclusion:

Thus, the program for the heir inheritance in java is executed


successfully. The steps for driving a car using the inheritance feature is displayed on
the output window.

29
43_Anisha_Bhapkar_2021PE0618
EXPERIMENT NO. 5 METHOD OVERLOADING, METHOD
OVERRIDING.

Aim: To write a program for method overloading and method overriding in JAVA.

Software: Eclipse.

Theory: If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in Java.In other words, If a subclass
provides the specific implementation of the method that has been declared by one
of its parent class, it is known as method overriding.If a class has multiple methods
having same name but different in parameters, it is known as Method
Overloading.If we have to perform only one operation, having same name of the
methods increases the readability of the program.

30
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT NO. 5.1 - METHOD BY DEMO

Aim:To write a program for addition of two numbers without parameter using
the method overloading.

Software: Eclipse.

Program: class
MethodDemo

{ void no_para()

{ int j=5; int k=6; int

s=

j+k;

System.out.println("sum of the two numbers in a method


without parameters are = " + s); } public

static void main(String

args[])

MethodDemo obj = new MethodDemo(); obj.no_para();

// Invoking the no_para method

31
43_Anisha_Bhapkar_2021PE0618

OUTPUT:

Conclusion:

Thus, the program for method overloading is executed successfully. The


addition of two numbers is 11 is displayed on the output window.

32
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT NO. 5.2 - METHOD BY PARA DEMO

Aim:To write a program for creating the method by para demo.

Software:Eclipse.

Program: class
MethodParaDemo

{ int with_para(int j, int k)

{ int s= j+k; return

s;

} public static void main(String args[])

MethodParaDemo get_sum = new MethodParaDemo(); int

sum=get_sum.with_para(7,8);

System.out.println("TheSumreturnedfromthewith_para()method is "+
sum);

OUTPUT:

Conclusion:

Thus, the program for method by para demo is executed successfully.

33
43_Anisha_Bhapkar_2021PE0618
The sum returned the value is displayed as 15 in the output window.

34
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT NO. 5.3 - METHOD BY OVERLOAD DEMO

Aim:To write a program to study the method by overload in java.

Software:Eclipse.

Program: class
MethodOverloadDemo

{ void sum(int a, int b)

{ int sum=a+b;
System.out.println("Sum of two integers= "+sum); } void

sum(float x, float

y)

{ float

sum=x+y;

System.out.println("Sum of two floats= "+sum);

public static void main(String args[])

MethodOverloadDemo obj = new MethodOverloadDemo();

obj.sum(20,25); obj.sum(15.5f,20.5f);

35
43_Anisha_Bhapkar_2021PE0618

Conclusion:

Thus, the program for the method overload is executed successfully. The
sum of two integers along with the float value is displayed in the window screen.

36
43_Anisha_Bhapkar_2021PE0618
METHOD OVERRIDE IN JAVA.

Aim:To write a program using the method override function in java.

Software:Eclipse.

PROGRAM:
class SuperClass

{ void display()

System.out.println("This is a super class method");

} } class SubClass extends

SuperClass

{ void display()

System.out.println("This is a sub class method");

} } class

MethodOverrideDemo

{ public static void main(String ar[])

SuperClass objsuper=new SuperClass(); SubClass objsub=new SubClass();


objsuper.display(); objsub.display();

37
43_Anisha_Bhapkar_2021PE0618
OUTPUT:

Conclusion:

Thus, the program for the method override is executed successfully. The class
and the superclass is displayed on the output window.

38
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT NO. 5.4 - METHOD BY REFERENCE DEMO

Aim:To write a program for method by reference in java.

Software:Eclipse.

Program: class
MethodByRefDemo

{ int a=10; void check(MethodByRefDemo

m)

m.a = m.a+5;

System.out.println("Value of a inside method definition "+a);

} public static void main(String args[])

MethodByRefDemo obj = new MethodByRefDemo();

System.out.println("Valueofabeforemethodinvocation"+obj.a);

obj.check(obj);

System.out.println("Valueofaaftermethodinvocation"+obj.a);

OUTPUT:

39
43_Anisha_Bhapkar_2021PE0618

Conclusion:

Thus, the program for method by reference is executed succesfully. The


values for invocation before and after and the value in method definition is displayed
in the output screen.

40
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT NO. 5.5- METHOD BY VALUE DEMO

Aim:To write a program for method by value using java.

Software:Eclipse.

Program: class
MethodByValueDemo

{ void check(int a)

a=a+5;

System.out.println("Value of a inside method definition "+a);

} public static void main(String args[])

MethodByValueDemo obj = new MethodByValueDemo(); int

a=10;

System.out.println("Value of a before method invocation "+a);

obj.check(a);

System.out.println("Value of a after method invocation "+a);

OUTPUT:

41
43_Anisha_Bhapkar_2021PE0618

Conclusion:

Thus, the program for method by value is executed successfully. The value for
the invocation before and after and the inside method definition is displayed on the
output screen.

42
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT NO. 6 JAVA STRINGS

Aim:To write a program for string operations in JAVA

Software:Eclipse.

Theory: In Java, string is basically an object that represents sequence of char


values. An array of characters works same as Java string. Java String class provides
a lot of methods to perform operations on strings such as compare(), concat(),
equals(), split(), length(), replace(), compare To(), intern(), substring() etc. The
java.lang.Stringclassimplements Serializable, Comparable and CharSequence interf
aces.

43
43_Anisha_Bhapkar_2021PE0618
Software:

EXPERIMENT NO. 6.1 - TO LOWER

To write a program for lowering a string in java.

Eclipse.

PROGRAM: public class

ToLower {

public static void main(String args[])

String name = "siddesh";

System.out.print("Name after conversion is " +


name.toLowerCase());

OUTPUT:

44
43_Anisha_Bhapkar_2021PE0618

Conclusion:

Thus the program for lowering of the input of java string is executed
successfully. Thus, the name of the given input is in lower case and is displayed on
the output window.

45
EXPERIMENT NO. 6.2 - TO UPPER

To write a program using the uppercase in java string.

Eclipse.

PROGRAM:
public class ToUpper

{ public static void main(String args[])

String name = "siddesh";

System.out.print("Name after conversion is " +name.toUpperCase());

OUTPUT:

Conclusion:

43_Anisha_Bhapkar_2021PE0618
Thus, the program for the upper casing of java string is executed
successfully. The name in the input is displayed as upper case in the output
window.

46
47
EXPERIMENT NO. 6.3 – TRIM

To write a program in java string for trimming.

Software: Eclipse.

PROGRAM: public
class Trim

{ public static void main(String

args[])

String str = " B C D E ";

System.out.println("Before using trim¥n");

System.out.println(str);

System.out.println("After using trim");

System.out.println(str.trim() );

OUTPUT:

Conclusion: Thus, the program for the trimming of the string is executed
successfully. The input string is trimmed and the output i.e. B C D E without the
space s displayed on the output window.

48
EXPERIMENT NO. 6.4 – CHARACTER

To write a program for finding the position of character in a given string in


java.

Software: Eclipse.

PROGRAM:
public class CharAt

{ public static void main(String

args[])

String str = "SE EXTC A division";

System.out.println("The character at index 5 is: " +


str.charAt(5));

}
}

OUTPUT:

Conclusion:

49
Thus, the program for finding the character is executed successfully. The 5th
character for the given string i.e. T is displayed on the output window.

50
EXPERIMENT NO. 6.5 - CLASS STRING ARRAY

Aim: To write a program for class string array in java.

Software: Eclipse.

PROGRAM: public class


ClassStrArray {

public static void main(String s[])

{ int

i;

String team[] = {"ABC","PQR","XYZ"};

for(i=0; i<team.length;i++)

System.out.print("Value at Index " + i + " is :


"+team[i]);

51
OUTPUT:

Conclusion:

Thus, the program for class string array is executed successfully. Thus,
according to the position the strings in the array are displayed in the output
window.

52
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT NO. 6.6 – CONCATE

To write a program for concatenation of strings in java.

Software: Eclipse.

PROGRAM:
public class Concat{ public static void main(String

args[]) {

String str = "Hello";

String str1= "Java";

System.out.print(str.concat(str1));

OUTPUT:

Conclusion:

53
43_Anisha_Bhapkar_2021PE0618

Thus, the program for the concatenation of the strings is executed


successfully. The two strings hello and java are concatenated as hellojava is
displayed on the output screen.

EXPERIMENT NO. 6.7 - CONCATE USING PLUS

To write a program for concatenating using plus for the string in java.

Software: Eclipse.

PROGRAM: public class


ConcateUsingPlus {

public static void main(String args[]) {

String FName= "EXTC";

String SName = "Engineering";

System.out.println (FName + " " + SName);

54
43_Anisha_Bhapkar_2021PE0618

OUTPUT:

Conclusion:

Thus, the program for the concatenation using plus for string is
executed successfully. Thus, the two strings EXTC and Engineering are
concatenated with space in the output.

55
43_Anisha_Bhapkar_2021PE0618

EXPERIMENT NO. 6.8 – REPLACE

To write a program for replacing the string in java.

Software: Eclipse.

PROGRAM:
public class Replace

{ public static void main(String

args[])

String str = "Hello Java Hello";

System.out.println(str.replace('H', ' '));

System.out.println(str.replaceAll("Hello", "Hi"));

System.out.println(str.replaceFirst("Hello", "Hi"));

56
43_Anisha_Bhapkar_2021PE0618

OUTPUT:

57
43_Anisha_Bhapkar_2021PE0618

Conclusion:

Thus, the program for the replacing of string is executed successfully. The
Hello Java Hello string is replaced with the letters in between this is displayed on
the output window.

58
EXPERIMENT 6.9 - STRING LENGTH

Aim: To write a program for calculating the length of a string using java.

Software: Eclipse.

PROGRAM: public class


Strlength {

public static void main(String args[])

String str="Viraaj"; int

x = str.length();

System.out.println("Length of the String str is " + x);

OUTPUT:

Conclusion:

59
Thus, the program for calculating the length is executed successfully. The
length of the given input string is 6 is displayed in the output window.

60
EXPERIMENT NO. 6.10 – SUBSTRING

Aim: To write a program for substring in java.

Software: Eclipse.

PROGRAM: public class


SubString {

public static void main(String args[])

String str = "VIRAJRANE";

System.out.println(str.substring(0,6)); //COME

System.out.println (str.substring(6));//WEL

OUTPUT:

61
Conclusion:

Thus, the program for writing the substring is executed successfully. The
input string is displayed as substring in the output window.

62
63
64

You might also like