Lecture 7- Classes and Objects in Java and Solved problems

IIT Roorkee July 201
61 followers
Jan 3, 2025
[Music]
[Music]
so welcome to lecture 7 classes and
objects so in the last class as we had
seen a program right multiplication of
two complex
numbers all right using class and
objects so there we had a keyword called
Static all right so what is the
difference between static variable and
the ordinary variable all right so when
I'm talking about ordinary variable it
is limited to certain scope right so
which is well defined whereas when I'm
talking about the scope of a static
variable all right so that extends
throughout the program we'll see with an
example all right so that you can
understand in a better way so here you
go right so for
example here I have let us say a
function all right so it's a simple
program right so here you have a c
program all right so int main I I take
it as a c you can change it to C+ plus
also not a problem so int
main right so here you have a function
right so int
function right whose return type is
integer so int count is equal to
zero which is not very difficult count
equal to zero and then line number 12 I
am using count
Plus+ all right and then I have return
count I take an example of a procedural
oriented language
C all right so you can just put C out
when you put C out that's all that is
called
C++ now I'm calling the function all
right when I'm calling the function you
can tell me what will happen all right
so function when I call so count is zero
count Plus+ so I can expect the output
what one good so you'll get the output
one all right and next again I'm
calling all right the new line again I'm
calling the function so what will happen
it goes count zero count Plus+ count is
zero count Plus+ return
count all right 0 0 + 1 is 1 so I have
to get two ones as output all right so I
have to get two ones as output so you
can see
that all right so I'll get two ones as
output okay so what I will do shall I
change this
variable all right with stating we'll
see what will happen all right so when I
change this to static so let us see what
is happening yes same program I have put
static so that you can understand all
right those who are learning first time
about static variable this program will
give you an idea so now as usual I'm
calling the function so first one count
zero count Plus plus so now count is one
all right so here I'm writing count is
one all right count plus plus count is
so return count so it will return one
all right so now when I use this
variable right static variable the
keyword static now my count is one that
is why I separately wrote last time I
did not write anything all right so now
my count is one so again when I am
calling the function in line number five
all right in line number five when I am
again calling this function
static int right your count is one count
Plus+ so now you will get the output
two all right so this is the output I
can expect return count so this is the
output I can
expect all right so you can see that
when I run this code I can get the
output like this so this is the
difference between a simple variable on
static variable so this program these
two programs will be be useful to
understand what is the meaning of the
key word static so if you look at the
last class we have use the keyword
called Static now you understand based
on these two programs you can understand
so without static variable right line
number 11 if I don't use static I am
getting output as one one if I use
static all right so count will be
considered as one before going to line
number five right and then when I it is
calling the function again the count
Plus+ 1 + 1 is becoming two and here we
are getting the output one and two right
so this is how the static variable is
being defined so now the same complex
number problem right the multiplication
of two complex number problem so let us
consider using we will write a program
on Java all right so here you go so
these are all the utilities
I am importing in Java now I create
almost the same the syntax is almost
same few things here and there there
will be a change so class complex you
have double real and double imaginary
all right you have two member data or
data member and then set values the same
like what we had seen in
C++ set values and here the
multiplication right so here you can't
have the reference operator in Java you
can't have the reference operator and
star operator all right when I Define
complex C1 all right so C1 is an object
so I can print the identity hashmap all
right so suppose I print let us say
system.out.print and C1 I will get the
identity Ash map right whereas in the
case of C++ you have to slightly use the
address operator slightly change the
syntax I mean to say so you will have
the address operator
so here complex C1 and here you have
complex C2 and you're having the
result which is an object whose class is
complex right so dynamically allocating
a memory using new operator and this we
are going to study right this syntax you
please note this is nothing but the
Constructor all right so line number 22
you are dynamically ating memory for
result dynamic memory allocation complex
result you put new operator so
dynamically allocate the memory and you
have complex followed by the function
notation you call it as Constructor all
right so now as usual multiplication of
two complex numbers what we had seen in
C++ the same syntax you are writing and
return result all right so few things
are changing so here we are not using
the address operator like number 21
whereas we have used if you recall we
use the address operator all right and
complex result so here you
go this is right the new syntax in Java
right so few things here and there there
will be a change and then display right
the vo display same member function you
call it as method right the same member
function what we have used in C++ so
here it will be the method and the
method will give you the real plus I
into imaginary all right and then I go
to the main program all right so main
program so it's a driver class the
driver class is main because you have
the main almost like in C++ right int
main so here the syntax is static you
know the meaning of Statics void main so
this we'll see slightly later right
string arguments string of
arguments all right so now this function
this inbuilt one is for taking the input
from the
user all right so this is the syntax
that you are writing that means you are
going to take some input from the user
in fact line number 47 and 48 we are
taking the input from the user for this
so this inbuilt statement is required in
line number
39 let us create two objects right let
us instantiate two objects C1 and C2 C1
whose class is complex and then here you
use a new
operator right and then you use the
Constructor and then another object so
dynamically you are allocating memory
for C1 and C2 that is a meaning okay so
now enter your real and
imaginary right enter your real and
imaginary or the first complex number so
that you are taking the input from the
user this is what you use the scanner
all right so it's like a scanner for
scene all right SC do next
Double next
int that means the integer data type
next Double so so this function the inbu
function using your object SC under the
class
scanner right so here you go you are
inbuilt all right this SC and scanner
they are all the inbuilt class
all right so which is useful this SC
object is invoking next Double function
next Double method inil method so that
means whatever the user is entering will
be stored in real one similarly next
line line number
48 right this SC the object is invoking
the inil method next
Double it will be assigned to imaginary
one all right so you are taking the
input from the user user and then here
C1 is invoking set values exactly like
what we had studied in
C++ okay so real one you are passing
imaginary one so assume that it is 2.0 +
3.0 I something like that so real 1 is
2.0 and imaginary one is 3.0 similarly
the second all right let us say 4 + 5 I
4.0 + 5.0
I so real two 4.0 and IM inary
2 right 5.0 so take the input from the
user and set
values so then the result method right
the result
object
who class is
complex all right so here you have so
here the
complex is
invoking right the class complex so
which is invoking the multiply
Method All right so multiply method is
being invoked here you are passing C1
and C2 as the arguments exactly like
what we have studied in C++ almost
similar all right so the complex so it
is more or less that it is like as if
you have created some objects so that
object is invoking multiply so this
syntax is also valid or you create some
complex let us say
C3 all right so that C3 will invoke the
multiply that is also possible all right
or otherwise simply you put the class
name of the class do multiply C1 comma
C2 right so then the resultant will be
stored in result that is the meaning
then result of the multiplication so
result. display will show result do real
and result do imaginary right and then
SC do close will close the file because
you are taking the input using SC and
then you do SC do close right this
object is invoking the close it's a kind
of file closing right so now when I run
the code I use assume that even if you
put two three it is assumed that you put
2.0 and 3.0 4.0 and 5.0 same input we
will give and you can see the result of
multiplication you will get this is the
output okay so how to create a class
right and how can you define a class and
how to create objects right all these we
have studied with the help of C++ as
well as a Java program also now you are
familiar with the static keyword right
so we will see one more case study right
one more problem so write a C++ program
to add two distances where feet and
inches are given right so you are given
a feet let us say 9 and seven
all right another one is8 and
six all right so these is
feet and these are all
inches so if you given this feet and
inches I want the output of addition
right so 12 in is 1
ft so 7 + 6 13 that means 1 ft will be
added 1 + 8 is 9 9 + 9 is 18 so this I
have to get the output on 1 in right
when I'm adding these two I have to get
the output 18 ft and 1 in all right so
this how do you do with the help of
object oriented programming you can
think of so how do you think you can
create a class distance and then you
have to include feet and inches as the
object and then you have to think of
right geter and Setter values display
values right and then the main program
you have to write addition of two
distances
all right so this we will see so let us
consider I have a class distance all
right so include IO stream because I'm
going to use C in C out so therefore
using name space
STD so class distance the name of the
class is distance I am using fet and
Ines right I'm using feet and inches so
here I I have set values set values in F
and in
I in F and in I so feet is equal to F so
whatever you are taking the input that
is being passed it will be stored as
feet and inches under that particular
object so feet is equal to F and inches
is equal to I all right so now here you
can see I have
ADD name of the function
all right name of the member function
and I have an object D whose class is
distance and the return type is also
distance and here I have distance result
so that means I will have to right I
need a two
distances all right so for example D1 +
D2 which I'll store in D3 resultant
distance so now I can use
iier all right without two arguments in
the last program in the multiplication
of two complex numbers we use two
arguments so can you write it in one
argument yes you can do that what it
will
do one object will invoke this add
function all right you will have like
this D1 do add and then you're passing
D2 all right you will call similar to
this so that means means here you have
inches simply inches you're not having
D1 do
inches simply inches and simpol simply
feet so this will be taken care that is
nothing but D1 do in D1 do
feet all right so one object's inches
will be added with another object's
inches all right so for example I have
97
86 right so first what I do I'll add the
inches
so here I will add 7 + 6 what will
happen
13 all right so here I have to use some
logic when it is going Beyond 12 all
right I have to increment my
feet all right so that we will do that
so inches you are adding first so we are
getting result. inches is right now 13
assume that right then what I will do
I'll add the feet 9 + 8 feet plus d do
feet the object that you're are going to
invoke so that is going to be your 9 and
rest is your passing 8 all right so 9 +
8 correct so you will have how much
17 and then so here you have result do
Ines integer division 12 so 13 by 12
integer division what it is 1 all right
so one will be added feet plus D1 do
feet and
result. in /2 divided by 12 13 by 12
integer Division 1 so 17 + 1 so right
now it will be
18 and then what you have to do you have
to do mod all right so 13 mod 12 13 mod
12 is what 1 your result dot inches will
be
1 right your result dot Ines will be 1
13 M 12 so 17 + 1 18 13 mod 12 is 1 so
this will be my resultant for this test
cases this test
case Okay so this you are returning as
result this you are returning as result
so why display you put feet and
inches why display you put feet and
inches so let us see because it is
slightly different code what we have
studied in the case of addition or
multiplication of two complex numbers
all right so here we'll see how we are
going to use this syntax in the main
program so you create three objects one
is two distances D1 plus d D1 plus D2
put it in result right so basically I
want like this result equal to D1 + D2
all our objects so D1 do in D1 do feet
D1 do feet D1 do in D2 feet and D2 do in
so resultant you will get result do feet
and result do Ines okay and then you
have set of variables Right feet inches
feet 1 inches one feet 2 and inches
two okay so now you are entering feet
one and inches one so assume that I
entered 9 and
8 all right I entered 9 and 8 so you are
setting the values feet 1 Ines 1 that
means D1 is invoking set
value right so the set value member
function here you go feet and inches
will be
set okay feet is equal to F and inches
is equal to I so 9 and
8 whatever right so 9 and 8 and second
one I'm giving 8 and
six all right so your Fe entering your
fe2 is eight and and inches 2 is six fe2
and inches 2 so you are setting the
values so this will be set so this is
for D1 and this is for D2 because D2 is
invoking here all right set values will
be called so D2 do feet is 8 and D2 do
Ines is
6 so now this is what the syntax I wrote
If you recall right so d D1 do add D2
exactly I wrote this right so D1 do add
D2 you are putting whatever be the
result you are putting as the result
that is the object right so result
object is
under class
distance okay so now what is happening
D1 is
invoking the member function add by
pausing D2 all right so that means you
pausing what 8 and six
you're pausing 8 and
six so when I'm pausing 8 and six all
right so here your D do inches is what
8 right inches feet six right d do
inches is six 8 and six so here I change
98 and
86 all right what about my simple inches
I don't have any object dot inches all
right so which is nothing but this
inches 8 so that means here what is
happening 8 + 6 will be added all right
so here it is nothing but 8 + 6 will be
added so when 8 and six is being added
it is
14 right this is what exactly happening
8 and six is nothing but
14 right D1 is invoking you may ask the
question how this D1 do Ines is going
right so D1 is invoking since D1 is
invoking so here the simple inches plus
d do in D do Ines we know it is 8 inches
is six right so 8 + 6 is being added and
it is nothing but
14 now you add feet right so feet is
nothing but 9 and 8 so 9 and 8 will be
added along with result. in
/2 so here I have 14 14/2 is 1 right so
9 + 8 17 17 + 1 18 18 ft I got still
inches I have not got all right so
inches is nothing but 14 mod 12 14 mod
12 is what 2 all right so that means 18
ft 18 ft 2 in I get as the output right
when I'm adding this 18 and 2 14 mod 12
is right this is what we are telling 14
mod 12 is line number 22 you look at
line number 22 14 mod 12 is 2 so we'll
get two as the output so totally you
will have 18 ft and 2 in as the output
all right so this we are displaying
result. display we are doing all right
so let us see what are the test cases
test cases assume that yeah I give
another test case 159 15t 9 in 18 ft and
7 in when you add this all right so 9 +
7 I will get 16 15 + 18 I will get
33 so 33 + 1 will be added 16 mod 12
will which will be 4 16/2 is 1 so 33 + 1
I will get 34 16 mod 12 I'll get 4 so 34
ft and 4 in I have to get as a output so
if you look at all right so I'm getting
exactly
the whatever the expected output we are
getting this as the output 34 ft and 4
in I hope it is clear this problem is
clear can we write the same program in
Java right so almost same so let us
try so earlier what you can do you try
to write it in both C++ and Java and
then later at one point of time we'll
stick only C++ okay so here you have a
class distance feet and
inches and here you have a member
function same member function what we
have studied in C++ what we have seen in
C++ set
values all right so similar right so
whatever we have done same thing you are
doing distance is a return type all
right so return type is an object
distance add distance D all right so D
is an object you have a distance so
returning object is possible these two
programs whatever program that we see
saying in C++ as well as in Java the
return can you return the object right
so these two programs are telling yes
you can return the
objects and here you go you have a
result object whose class is distance
dynamically you are creating a memory
space with the new operator and distance
is nothing but the Constructor so rest
is similar you are adding inches plus d
in D do in whatever we have studied in
C++ similarly feet plus d do feet and
then result. in /12 integer division so
whatever be the resultant you are
putting over here result do feet
similarly result do Ines is equal to
result do in mod 12 the reminder return
result right so almost the similar
syntax so one here line number 20 there
is a change all right so dynamically you
are creating a object and vo display so
display you are displaying feet and
inches so this is also similar and then
if you look at the class you are not
putting any semicolon the class ends in
line number 32 and here you go I may use
certain input therefore I have an object
SC under the inbu scanner inbu class
scanner all right your program starts
here the class main you call it as
driver class all right you have a main
program starts from here so you are
creating three objects all right so D1
and
D2 all right dynamically creating a
object and the third one is for result
right so D1 D2 new operator Constructor
D2 new operator Constructor and then you
have result all right so feat one all
right so feat one is taking the input
whatever input you are giving or input
that you are getting from the user that
will be stored in feet 1 line number
45 and similarly whatever the input you
are getting from the user you are
storing in inches
one right so feet one and inches one
that is your first object
all right first objects feet one and
inches or feet and inches simply so feet
one and inches will be set all right so
the object D1 is invoking set values
member function all right so feed one
and Ines one whatever you are pausing
all right so that will be set for D1
feet and D1 do in right so this is what
exactly will happen if you look at line
number 14 and 15 D1 do F feet and D1 do
Ines will be set similarly for the
object
D2 right so you are creating an object
D2 it will be set whatever feed 2 you
are taking as a input from the user
whatever inest 2 you are taking a input
from the user so D2 do set values Fe 2
comma in
2 all right so now D1 is invoking ADD
D2 so same
if I do 98 for
D1 and D2 if I have
86 right so D1 the object D1 which is
invoking D2 so that means I'm passing 8
and six 8 ft and 6 in is being passed
this is what exactly happening over here
right so D do
in what is D do Ines
6 all right so D do inches will be added
with inches what is that inches so that
will be paused when the object D1 is
invoking ADD so that means the inches is
nothing but
eight right in the function member
function or you call as a method so in
the method you call line number 21 you
have inches so that eight right so 8 + 6
which is nothing but
14 all right so here you go go you have
14 and feet you are doing feet plus d do
feet so your feet is nothing but 9 and
D2 do feet is nothing but 8 there it is
d. feet so 9 + 8 will be added which is
17 plus you
have some more statement result. in
/12 so 14/2 is 1 all right so one will
be added so here you have 18
and then 14 mod 12 14 mod 12 is
2 so that means 2 18 in 18 ft 2
in all right so this you are
displaying so some of the distance
result is invoking display and then you
have close all right in SC is invoking
close so when I give this as a input
right 15.0 9.0 8.0 and 7.0 so when I add
9 + 7 is 16 all right 15 + 8 is
23 so 23 + 1 24 this will be 4 16 mod 12
is 4 so 24
ft 4 in I have to get this as the
output all right so let us see what you
are going to get as output so you can
see 24 ft and 4 in
right so this we got as an
output so the last two classes what we
have done how to define a class and how
to create an object and we have seen two
case studies all right in C++ as well as
Java all right so now you can able to
write a program so we have not seen the
access modifiers like public private
soon we are going to see and you have
all use the static keyword in the last
class right so now you can able to write
certain basic programs so take these two
as an example and then try to work it
out many examples using many many
problems right using the concepts
whatever you have studied so far right
so with this I'm am concluding today's
class thank you so much
[Music]
