22 C++and Java Mansi Punde
22 C++and Java Mansi Punde
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
EXPERIMENT NO. 1
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 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:
22_mansi_punde_2021PE0362
EXPERIMENT NO. 2
ALGORITHM:
Step 1: Start
Step 2: Read a, b
Step 3: c = a+b
5: c = a-b
Step 7: c = a*b
Step 9: c = a/b
//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:
ALGORITHM:
Step 1: Start
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:
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 8: Use right shift operator (a>>b) and print its value
Step 9: Use left shift operator (a<<b) and print its value
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:
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 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:
EXPERIMENT NO. 6
ALGORITHM:
Step 1: Start
Step 4: Stop
FLOWCHART:
PROGRAM:
22_mansi_punde_extc_2021PE0362
OUTPUT:
CONCLUSION:
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
Weekdays:”
Step28: getch()
Step29: STOP
22_mansi_punde_extc_2021PE0362
PROGRAM:
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:
EXPERIMENT NO. 8
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
Step8: STOP
FLOWCHART:
PROGRAM:
#include <iostream.h>
#include
<stdio.h>
22_mansi_punde_extc_2021PE0362
#include
<conio.h> int
main ()
{
int count; for(count
printf("\n"); return 0;
OUTPUT:
CONCLUSION:
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 3: Enter the Number for which you want to find the table of which would
save in 'n'.
Step 5: n*i
Step 7: Stop
FLOWCHART:
22_mansi_punde_extc_2021PE0362
PROGRAM:
#include<iostream.h>
#include<stdio.h>
()
cin>> n; for(i = 1; i
cout<<"\n"<<n<<" *
"<<i<<" = "<<n*i; return
0;
OUTPUT:
22_mansi_punde_extc_2021PE0362
CONCLUSION:
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 4: x+y
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:
EXPERIMENT NO. 11
AIM: To write a program to find small and large number from given array
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
= a[0]
large = a[0]
Step 9: Stop
Stop
PROGRAM:
OUTPUT:
CONCLUSION:
22_mansi_punde_extc_2021PE0362
ALGORITHM:
Step 1: Start
Step 4: swap(a, b)
Step 5: b = a + b
Step 6: a = b - a
Step 7: b = b - a
Stap 9: Stop
FLOWCHART:
22_mansi_punde_extc_2021PE0362
PROGRAM:
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:
EXPERIMENT NO. 13
AIM: To write a program to swap two numbers using call by reference, call by
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 13: Call function “swapadr” with &a and &b as arguments.
Step 15: Create a function “swapval” to swap using call by value initializing
Step 17: Create a function “swapref” to swap using call by reference initializing
parameters &x, &y.
Step 19: Create a function “swapadr” to swap using call by address initializing
parameters *x, *y.
FLOWCHART:
22_mansi_punde_extc_2021PE0362
*y = z
Stop
22_mansi_punde_extc_2021PE0362
PROGRAM:
CONCLUSION:
EXPERIMENT NO. 14
name as that of its class which is used to initialize some valid values to the data
is created. The only restriction that applies to the constructor is that it must not
distinguishes the constructor from other member functions of a class by its name
ALGORITHM:
Step 1: Start.
Step 3: Initialize x, y.
Step 5: Create public function called “integer” with parameters (int x1, int y1) under
class
“integer”.
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.
}
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:
EXPERIMENT NO. 15
ALGORITHM:
Step 1: Start
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
k.show(); getch();
}
OUTPUT:
CONCLUSION:
Step 1: Start
Step 8 : Call the constructor of class integer with the required parameters.
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:
EXPERIMENT NO. 17
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
FLOWCHART:
PROGRAM:
CONCLUSION:
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: Read float var for absolute value. (float type of argument is being passed
during function call. Therefore, float version of absolute() gets called.)
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.)
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:
ALGORITHM:
Step 1: Start
Step 5: Declare function with double type single parameter , double var
Step 7: Declare function with int type single parameter , int var
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);
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int main()
{ int
a=5; double
b=5.5; //
calling
functions
display(a
);
display(b
);
display(a
,b);
return 0;
}
OUTPUT:
CONCLUSION:
EXPERIMENT NO.21
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 5: Use while loop and under that declare the condition as count<n
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:
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 3: Declare and define the function to get the student details.
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.
class sports
{
protected:
int sm; //sm = Sports
mark public: void getsm()
{
cout<<"\nEnter the sports mark :"; cin>>sm;
}
};
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:
THEORY: In single inheritance, a class is allowed to inherit from only one class.
ALGORITHM:
Step 3: Define and declare the function to get the employee details.
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 10: Call the function get and calculate to each employee.
{
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:
ALGORITHM:
Step 3: Declare and define the function to get the student details.
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.
CONCLUSION:
EXPERIMENT NO. 25
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 6: Stop.
FLOWCHART:
PROGRAM:
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:
JAVAJOURNAL
ROLL NO: 22
EXTC
DIV: B
DATE:
1
EXPERIMENT NO:1 JAVA CONTROL STATEMENT
Software: Eclipse.
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.
2. Loop statements
o do while loop o
o for-each
loop
3. Jump statements
o break statement
2
o continue statement
EXPERIMENT NO:1.1 IF DEMO STATEMENT.
Software: Eclipse.
b=20; if(a>b)
System.out.println("a>b");
} else
{
System.out.println("b>a");
}
}
}
Output:
3
43_Anisha_Bhapkar_2021PE0618
Conclusion:
Aim:To write a program using if-else statement demo for calculating the bigger
and smaller number in java.
Software: Eclipse.
System.out.println("a>b");
} else
System.out.println("b>a");
Output:
5
43_Anisha_Bhapkar_2021PE0618
Conclusion:
Software: Eclipse.
i=1;
for(;i<=20;i=i+2)
System.out.println(i);
7
43_Anisha_Bhapkar_2021PE0618
Output:
Conclusion:
Software: Eclipse.
WhileDemo {
System.out.println(i);
i=i+2;
9
43_Anisha_Bhapkar_2021PE0618
}
Output:
Conclusion:
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
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
14
43_Anisha_Bhapkar_2021PE0618
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.
Aim:To write a program for finding the largest number for the given array in java.
15
Software: Eclipse.
{10,20,30,40,50};
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.
Software: Eclipse.
{
for(j=0;j<3;j++)
System.out.print(set1[i][j]+"¥t");
System.out.println();
System.out.println();
{
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];
"); 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.
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.
int x, y;
DefaultConstructorDemo()
{ x=10; y=3;
} void
mul()
{ int m= x*y;
System.out.println("Valueaftermultiplicationof2numbersis
"+m);
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
Software: Eclipse.
Program:
public class OverloadedConstructorDemo
{ int num1, num2;
OverloadedConstructorDemo(int a)
num1=num2=a;
OverloadedConstructorDemo(int a, int b)
{ num1=a; num2=b;
p2.calculate();
24
43_Anisha_Bhapkar_2021PE0618
} void calculate()
{ int result=num1*num2;
Output:
Conclusion:
25
43_Anisha_Bhapkar_2021PE0618
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
System.out.println("Starting a vehicle");
System.out.println("Driving a Car");
27
43_Anisha_Bhapkar_2021PE0618
System.out.println("Flying a plane");
main(String args[])
c.start();
c.drive();
System.out.println();
j.start();
j.fly();
Output:
28
43_Anisha_Bhapkar_2021PE0618
Conclusion:
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
Aim:To write a program for addition of two numbers without parameter using
the method overloading.
Software: Eclipse.
Program: class
MethodDemo
{ void no_para()
s=
j+k;
args[])
31
43_Anisha_Bhapkar_2021PE0618
OUTPUT:
Conclusion:
32
43_Anisha_Bhapkar_2021PE0618
Software:Eclipse.
Program: class
MethodParaDemo
s;
sum=get_sum.with_para(7,8);
System.out.println("TheSumreturnedfromthewith_para()method is "+
sum);
OUTPUT:
Conclusion:
33
43_Anisha_Bhapkar_2021PE0618
The sum returned the value is displayed as 15 in the output window.
34
43_Anisha_Bhapkar_2021PE0618
Software:Eclipse.
Program: class
MethodOverloadDemo
{ int sum=a+b;
System.out.println("Sum of two integers= "+sum); } void
sum(float x, float
y)
{ float
sum=x+y;
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.
Software:Eclipse.
PROGRAM:
class SuperClass
{ void display()
SuperClass
{ void display()
} } class
MethodOverrideDemo
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
Software:Eclipse.
Program: class
MethodByRefDemo
m)
m.a = m.a+5;
System.out.println("Valueofabeforemethodinvocation"+obj.a);
obj.check(obj);
System.out.println("Valueofaaftermethodinvocation"+obj.a);
OUTPUT:
39
43_Anisha_Bhapkar_2021PE0618
Conclusion:
40
43_Anisha_Bhapkar_2021PE0618
Software:Eclipse.
Program: class
MethodByValueDemo
{ void check(int a)
a=a+5;
a=10;
obj.check(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
Software:Eclipse.
43
43_Anisha_Bhapkar_2021PE0618
Software:
Eclipse.
ToLower {
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
Eclipse.
PROGRAM:
public class ToUpper
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
Software: Eclipse.
PROGRAM: public
class Trim
args[])
System.out.println(str);
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
Software: Eclipse.
PROGRAM:
public class CharAt
args[])
}
}
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
Software: Eclipse.
{ int
i;
for(i=0; i<team.length;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
Software: Eclipse.
PROGRAM:
public class Concat{ public static void main(String
args[]) {
System.out.print(str.concat(str1));
OUTPUT:
Conclusion:
53
43_Anisha_Bhapkar_2021PE0618
To write a program for concatenating using plus for the string in java.
Software: Eclipse.
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
Software: Eclipse.
PROGRAM:
public class Replace
args[])
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.
x = str.length();
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
Software: Eclipse.
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