Main

OPPE1 Revision Session-2

PRANAV V: Yes, that's the exact idea I was looking for so there is nothing >> GANGAPRASAD TEWARY: unique events ...

JAVA

Streamed 4 weeks ago

uh good evening uh so we'll be uh solving two questions two programming questions today I'll share my screen and we'll begin uh just let me know if my screen is visible yes sir it isible okay then so I'll read through the question once um and then we'll go into solving the question I'll share I mean I'll give a step by-step explanation just you know try to pause me wherever you are not able to follow so we'll begin so you're given a Java program for a music player and you have to complete the co
de AS required so that is pretty much every single question which You' have been dealing with so you have given a predefined template all you have to do is in the missing pieces as instructed so you're given a class song which has instance variables title artist genre and duration now it also has a Constructor to initialize these instance variables and accessor method to get the values of these instance variable so just by looking at this accessor method you should get an idea that they are priv
ate instance variables right so only if they are private you'll have to use accessor methods to get hold of their values so this is just an interpretation of the question we need not you know bother about it much then you have the class called music playlist which has three methods so the first method is called is long duration which takes as input parameter a duration which is an integer checks if the duration of any song is greater than 300 which is just an arbitrary value and returns true oth
erwise returns false so this is you know taking uh duration as input parameter duration of a song and checks if that particular U duration is greater than 300 seconds or something and returns true otherwise returns false uh then you have this remove long duration songs which is another method that takes a linked hash map as input parameter so U maps are key value I mean they store key value pairs right so hashmap basically stores them based on a hash function as such so if you're done pdsa you w
ould know what hashing and indexing are basically so a hashmap basically has few buckets and decides which bucket a particular value goes to that is a key value pair over here goes to and linked hashmap basically to store the input order so just a random hashmap will not care about the order in which the uh inputs are given a linked hash map will take into account the order as well and it removes all the long songs in this U map so you're going to you're you have an idea that whatever hashmap yo
u give as an input parameter we are going to create an iterator for this uh hashmaps entries and for each of the entries we are going to extract the value because the hashmap uh structure if you see the code template you will know that it takes a integer and a song as the key value pair so we look into that uh while we go in and you'll be creating an iterator accessing the value of the entries if the duration of that value so since the value is a song you're going to check the duration of that p
articular song which is basically its instance variable pass it to this in uh long duration function if it returns true we remove it uh you know from our map else we just you know go to the next itation the hint here is given as use iterator for the entry set of the map so we already know that Maps entries can be uh you know got by using this method entry set so what this gives us is a map. entry type object so let me just uh you know share a Zam board to explain that to you uh yeah is my Zam bo
ard visible now no sir I think it should yes sir yes sir so assume this is your map okay so let us just assume this is our map so these are the elements of a map see this is not the exact structure I'm just giving you an idea of how you should visualize this so each element is going to be an integer let us assume one is one integer and the value I'm going to have as song one okay so this is a song type object so this song has instance variable name title uh genre and duration Etc this is another
song which is having um you know integer two and let us say this is Song 2 Okay so this song will also have instance variables so these are basically the instance variables of the song type object so you need to understand my map is a key value I mean uh a collection of key value pairs okay map is not a collection I'm just using that word to give you an idea that it stores key value pairs it is you know similar to your dictionaries in Python so try to relate it in that manner so now if I want t
o iterate through the entries uh I would be using an iterator for this map so why I'm using an iterator over here is you should have you know by now you should know that that when you try to access the map entries directly and remove them um you know like using a for each Loop some would have tried right so using a for each Loop directly on the map and removing the entries would give you a concurrent modification exception so you cannot directly you know go through the entries of the map and als
o remove them so for that purpose we are using this iterator interface so what this iterator interface does is uh we've used this in collections as well right so iterator you know it basically um creates some uh tool for us to go through the entries of a collection so here the collection we'll be dealing with is a entry set so entry set gives us a set of map. Entry objects right so map. entry objects look like a key value pair so let us assume this is my map. entry object so I'll just draw a box
over here yeah just assume this is my map. entry type objects so this will have a key value I mean I mean I'm just just using k for key and V for Value so these are basically taking a key and a value and this is stored as a map. entry type object and a collection of these type objects are stored in a set so map do entry set so let us just call this m okay if this is your map m. entry set is going to give you a set of such map. entry objects so all this I think you would have covered if you you
know completed your uh grps and PPS you will have a proper idea so here what we do is we are going to create an iterator for such objects and this map. entry uh has a key value of integer Comma Song right so we have to specify that so this is of type integer Comma Song and let us just call it iter so this is equal to I'm going to create an iterator for that particular entry set so let us just assume for now playlist is our map and this playlist has key value pairs as integer Comma Song so now I
want to create the entry set of this playlist first so this particular statement over here I'm not done yet so don't get confused I'm not using iterator till now I'm just creating the entry set so this entry set is going to give me a set of map. Entry type objects so this basically looks like I'll erase this part and show that to you so it will you know create a set of this map. entry type object so this is one map. entry this is another map. entry so these are all I'm just using me for map. ent
ry so this creates a set of such objects so now we are creating an iterator for this set now do you understand so this iterator is created only for collections so inherently we had a map the map got converted to a collection as such by using this entry set so this returns a set of map. Entry objects and now we are going to create an iterator for this set so uh we'll now use the dot iterator for this particular set um I'll just extend this dot iterator so now this should uh give us the iterator w
hich we require for going through all the values I'll just erase this and move this back yeah so now I think this is pretty clear so we are creating like you should just about map you indicated map one song and map entry is like you have like you have drawn the map one thing and map map entry is another thing and map entry is one of the entries of the map itself uh I'm not able to understand what do you try to tell me like you told about map entry and map so map entry is one of the entry of at o
ne if we search at index one or zero something like that then we get one map entry there don't don't confuse yourself too much see this playlist. entry set is going to give you a map. entry type objects together right so you're going to create an iterator for such objects this is basically going to return you a set the name itself suggests right so we are creating an iterator for this set this iterator takes as the generic as map. entry so it is see for uh let me just give you an example over he
re ah so uh let us just assume that uh set is an integer I mean this I have an integer set so if you want to create an iterator for an integer set you would do this right so we give iterator of integers give it a name and just call this number set so assume this number set is my set of integers I call the iterator method on it and I get an it type object you understood this right so this is same we are extending this for entry set so entry set is again a set for which we call this iterator and t
his uh data type over here here we are giving this integer as the mean uh in place of the generic we place integer here we are placing map. entry this map. entry has an integer key and a song value so that is why we give integer commas s okay thank you okay so now we'll go back to the code yeah so that is it for our remove long duration songs and then we also have a display playlist that takes a linked hashmap as input parameter and prints the elements as required by the output so we'll just loo
k at the input test cases right now so uh this is my input test cases so I have a set of five songs which I'm going to give as input and after removing the long duration songs this is my output so you can see the song with id2 and id4 are removed id1 three and five are retained so this should be my output um is there any doubts here shall I proceed with writing the code any doubts for anyone okay then so I'll share the emplate right now yeah is my screen visible yes okay so we'll just go through
the given template one so the tip I would give you while solving op questions also is first look at the code read up till the main method and then go in a bottom up approach so you'll know what you have to fill in so the question already told that we have a class song and we just have one line about so this is our class song right uh is there someone having any doubt uh yeah I'll share the Google Drive Link once I'm done so see uh class song has four private variables so this is what we already
predicted with the accessor methods being given to us so it also has a Constructor and the corresponding uh uh accessor methods uh what we can also see is the two string method having been already defined so uh if you can compare this with the required output which we had you can know that the corresponding song output is only this so if you give system.out.println of a song it triggers the corresponding two- string method of a song right so if you have a song object when you have a song object
and try to print a song object that is uh you know this is the class song and the two string method is present I mean overridden inside class song so when you pass a song object to a print statement this particular method gets triggered and and this method basically returns I mean these values so you can compare it with your output so this part of the output from title is you know similar to this part so all you have to do is include an ID which is basically your uh uh key in your map so you ha
ve to include that particular key and then print the song that's it so when you print the song you get all this all you have to do in your display playlist uh stuff is add another print statement for printing the key uh before this uh song printing so I think your songs play I mean songs class is pretty clear let us move on to the main method so the main method is present inside a class called music player um yeah don't mind this file name I've just created a duplicate that's why and now uh let
us assume uh mean let us analyze this particular part of the code we have a linked hashmap uh being instantiated over here we take inputs and you know we input five songs so that is what our test case gave us and we create a music playlist type object so this is our class which we have to fill in so there are three methods of which only one has been already defined which basically returns if the duration is greater than 300 returns true if it is greater than 300 else returns false and uh so that
is our music player type music playlist type object now we call the remove long duration songs on that object and pass as input parameter the hashmap which we have taken as input over here and once I'm done with the remove long duration songs I make the print statement with the playlist after removing long duration songs as you can see over here in the output so try to compare the output with the statements given inside you will have a better idea so this line of output corresponds to line numb
er 63 in my code and then display playlist uh basically gives me uh the next three lines of my my output over here so these three lines of output are triggered by this particular statement so what triggers the removing of songs is basically line number 62 so now you have an understanding that you have to complete the method given in line number 62 which is remove long duration songs and also complete the method display playlist which is given in line number 64 all others have been concretely def
ined and you need to touch them so let us go back to our music playlist class and Define display playlist and REM long duration songs so now we have an idea of this particular method right so we'll just complete display playlist first that is you know pretty simple to understand as well so let us go back to the question and analyze what display playlist is so it is basically a vo method that takes as uh input a link hashmap and prints the elements so all you have to do is uh Define a void method
create an iterator for this linked hashmap and print out those those particular uh required lines so let us Define public void display playlist just accept this it takes a linked hashmap as input the linked hashmap has an integer key and a song value that we have already seen over here if you can see uh this playlist takes as uh the key an integer that is basically the ID of the song and value which is a song type object itself so we have pass that as input parameter over here let us now Define
the um printing statements as we require so now we know that we are going to iterate through this playlist using the entry set right so that is what I discussed using the jam board so for uh every map. entry um of integer so every map. entry is a key value I mean pair so this is basically my entry and the iterable from which I create create this entry is are uh playlist. entry set so for every map. entry uh which is you know stored in this variable entry from my iterable playlist. entry set I w
ill be printing out something right so this is uh understandable uh this is straight from your PPS and grpa so the colle um questions based on maps you'll be dealing with in this way so you create an entry set for the map this entry set has map. entry type object so for each uh map. entry you have a get key and get Value method which we'll be using over here so you can see the print statement over here takes the first uh element as this uh uh key of my map so we'll uh just you know give system.o
ut.println the first part is going to be my uh key from the map so what I'll do is uh print out the key so that is going to be uh entry. get key and I'll be concatenating it with a comma over here comma and space so let me just provide the comma and space after which we just have to print out the song so I had already told you that get value returns me the song and when I pass a song to The system.out.print Ln statement the corresponding two string method gets called so this two string method ov
er here gets called on this particular uh part of the code so entry. getet value Returns the song in this place so when you print the song The corresponding two string method of the class gets triggered and you get the title artist duration Jer Etc so for this part ID colon one we already gave the I think we have missed out one thing it should be uh ID colon space and then plus entry. getet key so now I think we'll get the proper output yeah so first we print out ID colon and give a space print
out the ID concatenate it with a comma and a space and then print out the song So when you print out the song the two string method is called and the corresponding values take this particular position and this is iterated over for every entry in the playlist. entry set which covers all the key value pairs so even now if you're not able to you know process this try to you know see this is not exactly related but try to compare it with the line we use in Python which is I comma J in dictionary uh
do items try to relate it with that you'll understand in a proper way like we unpack this Tuple right so we unpack the tuples present in dictionary. items this is something similar to it so try to process this in that particular way just uh you know create relations so that you understand if if you not able able to understand till here try to create that relation and then probably you might process this so that is it for our display playlist method now we'll go to the remove long songs which is
the um yeah sir just a small out sir like your display playlist method so we are passing two parameters the first parameter is linked hash right no no it's only one parameter this is the data type okay okay okay yeah got it playlist data type of Link hashmap got it has a link hash got it yeah and then how two string method is getting involved that part is not getting clear like yeah see for we are defining a two string method for a song type object right so it's in this class called song yes see
it this two string resides in this class called song which I'm highlighting yes yes yes yeah yeah so whenever you try to print a song object or for any matter if you try to print an object of a Class A the corresponding two string method of Class A gets trigger so when you print a string so see when you print this particular uh string value over here uh the two string method I mean it is implicit and it knows that you have to directly throw whatever is there inside the print statement to the ou
tput screen but if I give a print of a song so when I create a song type object and pass it to the print statement it does not understand what to do with it right so it gives you some garbage value as the output so to make it cleaner we just want to print out the title r is duration and gen so whenever we want to print a song we are telling the compiler that inherently we try to print the details of that song so we override the printing statement so this is basically the printing state statement
of that class we override that printing statement and we give the title artist duration and genre inside the printing statement so it understands that when I pass a song type object to the print what I intend to have in my output is particularly this line now do you understand yes okay so when I give entry. getet value the entry's value is a song right so entry is my elements in this itable so this is a set as I had already discussed this set has ma. entry objects for each entry object I'm goin
g to uh get the value the value is a song type object and I try to print out that particular song so when I print the song using entry. getet value the two- string method of song class gets triggered so the two string method of song class uh is printing out the details of the song now do you have a proper understanding sir I have a question sir yeah so is this entry. get value we will get an object of object thing now of song we get a song yeah object it's an object yes yeah okay sir so that son
g object when it is passed to the print statement it understands that it has to trigger the corresponding two string method I have a question here like in the integer part that is inte integer type right entry. get value get key key is my integer value is my song so the moment when I enter that entry. gate value I will get both like the number the integer as well as see entry set gives me a set of integer Comma Song values Okay so that is what I'm iterating through using this for each Loop so fo
r every entry I have an integer and a song present inside them so I extract them using get key and get value key is my integer and value is my song now do you understand yes I got okay so we'll move on to the remove long songs method we just look at the uh name given yeah one question like over here you have used the forage kind of thing and like if we use like while like in iterator that we use that iterator do has next can we use that yeah that is there do you want me to explain that as well n
o that I can understand because you wrote that statement iterator I will write that statement and using that iterator I will just say that has yeah basically if you use an iterator you have to use a while loop uh give has next and then move on to iterator do next if there is a next object iterator over here I'll explain that iterator over here once I'm done with that probably you can try using that iterator and display playlist also okay okay okay okay yeah so we'll now move on to remove long so
ng so it is again a public void method uh the name is I'll just copy that name okay I'll accept this so see again I take as input a linked hash map with which is basically having an integer key and a song value so that is what my question also says it takes a linked hashmap as input parameter it iterates and removes all the long songs so we had already discussed how we create an iterator for this particular map so I'll be using the iterator interface for every map. entry uh I'm going to use an i
nteger uh Comma Song over here uh let us just call it an iter and we'll create the iterator for playlist. entry Set uh and call the iterator on this collection so set is basically a collection we are calling the iterator method on this uh collection to get an iterator type object the iterator object is for map. entry type objects so it iterates through such values now do you understand this particular line yes so in line number 10 this iterator line itself like we are creating a reference variab
le iter right yes of iterator class generally as per common understanding now this reference variable must be referring to an object right of its own class or it child class something like that right so so here when we are typing playlist. entry set so it is it is giving me a collection or some sort of collection right yeah and then I'm calling iterator method on top of that yes I'm call see I'm calling an iterator on a set basically entry set gives me a set of values a set of map. Entry values
right call the iterator method on that set which basically is my iterator um type object with this particular generic inside it so iterator is defined as a you know generic interface right so it has it can take any value inside this you can create an iterator for integers is clear so while iterating I will be getting one value at a time right yes yes yes that is what we'll be dealing with so we use a has next this this reference variable is pointing to that object yes this is basically only a na
me to access this type object sir I have a qu sir yeah sir in this uh previous uh function display playlist We are iterating in this also now so what's the difference between this function and uh I just wanted to give you an idea of how the two different methods of iterating through if you use this particular method for modifying the map you'll get an error that is what I want to express see I'll finish this particular piece of code and tell you why this particular uh looping statement I mean th
is for each Loop is not suited for removing elements when you just want to display or just access if you want to have a readon type action on a map then you can use this it I mean this for each Loop without the iterator if you want to update or if you want to remove anything from the map you have to use this particular stuff this is only because you get a concurrent modification exception okay so I I'll discuss that once I'm done with this uh piece of code okay sir thank you sir any other querie
s okay then so now we have our iterator in hand and we already know see uh let me just give another uh Insight give me a minute Is My Jam board visible no no not yet yeah so assume this is my collection initially okay so let us just have a rectangular box uh these are the entries inside the collection so for this particular example this is basically my entry set which has map. entries as each element so all these elements having key value pairs in map. entry type so this is a map. entry this is
another map. entry and so on now when I create an iter my iter basically resides before my first element initially so initially my iter is pointing over here it is not pointing to any element so my set is over here and my iter is pointing before the set so when I call the has next method what this iterator type object does is checks if any element is present in the next index now I mean for our sake we'll uh this might not even be the exact index numbers for for our sake we'll just assume 1 2 3
4 5 are my indexes I'm just using an assumption let's not confuse this is the exact indexing assume okay so assume 1 2 3 4 5 are my indexes so my iterator now exists in an arbitrary zero index now when I call has next it takes zero adds one to it so this is intive adds one to it and checks if any entry is present in the first index over here I have a map. entry so my iterator moves from this position so it adds one and moves from that position to reide in this position now so hash next returned
true and then iterator do next moved my pointer to this POS particular position so this has the syntax of uh let me just you know use if my iter is my object so iter do has next so if this returns true so let us put this inside a while statement so while iter do has next what I intend to do is move to the next element so how I ask it to move to the next element is just by using iter do next now myer. next is returning something right so it returns this object this is a map. entry object so I hav
e to store that map. entry somewhere so I create an object over here mean arbitrary object so map do entry of uh integer Comma Song let us just give it entry is equal to iter do next now do you have an idea of how this works yes sir now that means like first entry it will check if it doesn't present then it will add into that then it will go to the next is that the meaning see uh it is stored in an order only right so there can't be misplaced matching so if if there does not exist any element ov
er here your while loop exists it will be present in a continuous manner only if there is an element here then there is an element here if there is no element over here there can't be any elements after it so once the pointer reaches this particular position so assume your iter is now at this particular position so the iter's pointer is at this position now iter do has next will check for this position there is no object over here so it returns false and your while loop is done so at the end of
the loop theer points to the last object yes the last position where there is no element last position yeah shall we move back to the code yes sir okay just one clarification that it is pointing to the last last object right not the not after like not the null something no no no no no not like that see once it you know we are not even moving it if the has next is false it is still pointing at that particular object only we are not moving the next so it points to the last object for which entry i
s present right okay because while the hash next returns true iter do next is not at all called so it already remains only in that particular object which we currently are in yeah okay yeah so we'll give while iter dot has next this uh you know I've explained that it checks if there is another entry if there is another entry we create that object I mean we store that object in this variable map. entry of integer Comma Song let us just give it entry is equal to iter do next so now this moves the
pointer to that particular element now what we have to check is pass the uh value right so for our sake we can probably store the value in song song is equal to entry. get value and then if uh this long duration of uh song. duration song. get duration then we just remove it I'm moving a bit fast I'll just explain once I'm done so if I leave the flow over here there might I mean I might make some errors okay so I'll explain right now so until line number 12 I've already explained so line number 1
3 I'm basically Al extracting the value from this entry so I told you that each map. entry has a key integer and a value song so I'm storing that value song using get value and for every song I'd already mentioned you have a title an artist a duration and a genre and these are all private variables so you can access them only using their accessor methods so this is something back from our week 2 week three content so try to get uh you know idea from that so you have to use this get duration meth
od which returns an integer duration so I'm passing that song. get duration to my uh is long duration method so this takes as our param input parameter a duration integer so song. get duration returns an integer over here it is passed to this particular method once that method is executed so if this is true I remove that element from my uh map so this iterator itself is created for that map map Maps entries right so when I give it. remove it removes it from my maps memory and now when I try to p
rint that map those entries are vanished so that is how this particular iterator works now coming back if I try to use this same statement so let me just comment this out okay I'll comment this entire part out yeah before I commented any doubts here was pretty clear right the last three lines are not difficult to understand so is long duration like the moment when I'm extracting the number then the song then corresponding duration I have now do I check it is it long duration am I specifying some
thing duration if it is greater than this yeah it is predefined I already gave it in the code if my duration is greater than 300 it returns true then the moment when that if condition becomes true then we I'm removing that item from the map map yes yes so if this method returns true I'm going to remove it from the map if not I go to the next element that's all and map itself it is it is arranging them in the alphabetical ERS not alphabetical not alphabetical input order yes okay input order okay
okay yeah so shall I I mean I'll give you the idea of the erronous code right now so somebody was already asking me about using this for each over here also so I'll comment this so what happens when you use this for each over here so if I try to give for each and you know try to modify my uh stuff uh it might throw my see directly when I give map. remove it will throw a concurrent modification exception see sometimes you might get that exception this is because you're iterating through that par
ticular value see over here I won't get any errors because this is my entry set only um okay I will not write that ER onus code it will confuse you I'll just give you an idea in the jam boo so see this is the exact perfect code which works I mean whenever you want to modify something from your map use this particular ideology so this is the most straightforward and the most uh I would say logical approach to remove an element create an iterator check if there are next elements that is if my map
I mean the iterator has more elements to go through if there are any then perform those actions if not break out of the while loop so this is the most logical approach I can give you and then you have this for each me method which basically uh goes through all the values I mean for just viewing so we directly use the for each using the entry set of the map and print out those particular uh required statements so I'll stop this share go to the jam board so we can we can use the top approach in th
e bottom one now there's no issue yeah there is no issue you can use that as well see when you try to okay I'll show that instead of this I'll show the other approach to that playlist method so we'll Define the uh display playlist using this hash next and stuff so I again I I'm going to create an iterator only so I'll copy this line I create an iterator for the entry set while iter do has next what I do is uh I have to print out the values right so let me just you know store the map. entry first
so map. entry uh of integer Comma Song I'll just name it entry is equal to iter do next so this takes the next value let me take the uh ID and the song separately so I'll give integer ID is equal to or we can directly give int ID that works anyways so it gets get key and song song is equal to entry. get value now I have my ID and my song so all I have to do now is just pass them in my print statement system.out.println uh here I would give ID colon space and then append it with my uh integer ID
so when I pass an integer to a print statement along with a string that integer is type converted to a string and then uh it is per I mean concatenated and then I have a comma and a space and then I print out the song So for this song only I had already mentioned it triggers the two string method and prints it out so once I'm done I move the iter to uh I mean I mean the iter next is called over here so you need not do so once this is done it goes to the next iteration checks if there are any ot
her elements again if there is another element I store it in entry extract the key and value print out the corresponding stuff so this is the other approach so I would name this as method one and this is my U method two this seems to be slightly simpler which one the second method we are extracting the key value separately and then we are printing yeah anything whichever is comfortable for you go for it so always remember if you want to modify something uh rely on this uh iterator getting an ite
rator and going through the elements this is a fail proof method so you can not get an error if you go in this particular manner sir so here again sir in line number 31 and 32 so ID is just a simple variable right which is storing the value of entry. G key like whatever key I am getting back and and this is your value integer is your key is valueing them that's yeah just we simply storing it right yes it it has nothing to do with creation of you know object and all right it is simp the value yes
I'm just initializing a song variable and an integer variable storing the key and storing the value got it uh is this clear to everyone yes yes sir yeah and somebody had asked me for the link give me a minute I'll post the questions link I think it is already available in discourse if I'm not wrong any let us complete all the questions then you can upload in all the questions in the link yeah uh just a minute yeah I think I've provided that exact link can someone check if it is accessible by ev
eryone this particular question is in the week six folder so it's accessible yeah okay uh just a minute okay I'll uh share the next question uh just let me know if my EVS code is visible let me just take up those question statements and then share it's a pretty similar question only so that deals with uh basic collections only it's much simpler than this question I'll just copy that stuff and share sir any modification can be made in these kind of questions modifications I don't get you I mean t
o say these are pretty straight forward questions we have to add it you have to remove it is there any we can we can we make any kind of modification in these uh oh you want to modify the questions inside that uh folder no no sir I want to say we are just getting these kind of questions we have a hashmap we have to remove it we have to add it yeah is there any complex things that we can get or we will be getting these kind of questions only see I cannot guarantee all that stuff so you know this
is the basic idea probably if there is some modification try to iterate up till that element store those values remove that particular element and add the new element that it that's the only approach I can you know try to think of okay if there is let's say you have a particular song you want to modified that songs uh you know Pro some stuff probably you can if there is a mutator method present extract the value probably use the mutator on it and then remove the old entry add the new entry somet
hing like that all that I think you can think on your own see uh there there won't be too complex questions it will be only up to the scope of the course so it will be approach question6 it is very tough like grp in we six oh okay okay yeah is my vs code visible yes sir okay so you have uh class books and a class Library um they have been provided in the bow code you have to implement the two string method in books as required by the output provided in the test case so your test case output is i
n this format so you have to uh implement the two string method so uh let us just look at the main method present the main method already has a list of books in the fantasy genre so this line is already there in system.out.print L so you can see over here this line is already given all we have to print out is this particular stuff so we have to somehow modify our print statement to to get this output so that is what is given uh I mean implement the two string so what this two string does we have
already seen so whenever we want to print a book type object uh when we pass a book type object to the print statement uh this two string method gets called so we have a proper understanding there we'll just modify this once we go inside the code then you have class Library which has a private instance variable called books sorry so books are stores unique records as key value pairs so can someone tell me what this particular idea is about so stores unique records as key value pair again map ok
ay so key value pairs tells you a map what concrete implementation of a map read the line again and try to think of a concrete implementation uh stores unique records s key value P okay then unique is there than hashmap yes that's the exact idea I was looking for so there is nothing specified about the order so you can directly take a hashm if there is another statement which says stores the unique records as key value pairs in the order they are provided then you have to use a linked hashmap th
en linked hashmap yes so all you need to you know remember is which concrete implementation is used for what purpose so you always have to have that idea so if it was like stores the unique records in a sorted order sorted order of let's say book IDs or SBN number or something like that uh then you have to use a tree map all that I think in the grpa there was one question on tree Maps right do you remember that okay anyways we just Mo on so each key is a combination of title and author as given
in the ad book method so we'll look at the ad book method over here yeah so the ad book method has already been provided the I mean the implementation is already given so it combines the title and the author and adds it as a key to my book so my hashmap has a key which is a combination of my title and author stored as a string so title and author are concatenated in this manner using a hyphen that string is used as my key and my value is the corresponding book that the book which a book object w
hich I pass as argument is this idea clear so book is not title right uh I don't get you book is not title right key itself is a long string right it's a concatenation of is a concatenation of two instance variables correct and your value is the entire object itself the book object itself okay which you pass as argument to ad book okay so see this is just given to Pro probably deviate you you don't know even if it is useful to you while you write the code so there are some redundant information
so you have to discard them while you code so once you look at the given template you will know what is the Redundant information what is the useful information so this is a useful information because it tells you that you have to implement the two string so this test case output is a useful information this um uh over here I mean this line over here which tells you that book stores unique record the key value pairs gives you the understanding that it is a hashmap so you know that books is an in
stance variable which has to be defined so the instance variable has not been defined so to define or you know initiate it you know that it is a a hashmap by this particular line and then uh this line over here this each key uh and then add book it is already provided so it might be a redundant information so try to you know abstract the useful information from the question while you uh code then you have this particular line which says complete the Constructor for library and the search by genn
er method to return the list of books so now you come across a new word which says list of books so you know uh um a concrete implementation of list which is you know useful to us is the array list right so we'll be using that particular collection here and U yeah that's pretty much about the question so we'll just move on to this uh statement and look at the provided template so let us read through the given template first so again I'll be using a bottom up approach only I'll look at the entire
code go to the main method and go above to declare the mean Define the methods which are missing so I have a book uh class which has four private variables it has a Constructor and it has accessor method so this is pretty clear now what is missing is the two string method which I have to implement so the output statement over here tells me that I have to give the output in this format so let us look at the main methods last line so it already has this um hyphen which is provided over here so th
is book and then the curly braces inside it I have few values that is what is required to be printed by me so I'll just you know Define that so public you know the uh method signature for two string is return type string uh name as two string in cam case so I'll just put this here and what you'll be returning is this particular string which is required okay so I'll just copy that from the solution I'll not waste time there give me a minute there nothing in the parameter right yeah two string doe
s not take any parameters yes yeah so to save time I've just pasted this particular stuff so this you can you know write intuitively so first you have the book and the curly braces then con cated by title equal to then print the title here you are free to use this do title or directly title because there is no ambiguity and then this back slash apostrophes to you know uh give an escape sequence all that I think we have covered in week one and stuff so that you should understand so two string met
hod basically defines the print statement which you have to perform this print statement which you have that back slash apostrophe is basically to give this particular apostrophe because there are Escape sequences you'll have to use the backs slash and give this aity uh once you're done with that I mean this particular two string method is quite simple you you can understand this on your own is there anyone who wants me to explain this in detail no is there anyone who want me to explain this no
sir okay then so we'll move on to the next part of the code so your class books is now done so the question asks you to uh you know um Define only the two string method for books which we have completed so this particular two string method uh is in correspondence or in accordance with our output test case so we'll move on and uh look through the other parts now you have a class Library which has a missing instance variable which has to be declared as private and we already saw uh in the question
statement let me go back to the question statement it says it has a private instance variable called books which stores unique records as key value pairs so again I'm iterating this which tells me that uh uh books is my instance variable which stores unique records as key value pairs so I'll be defining a concrete implementation of a map so let me just you know for the data type I'll just give hash map of string comma book which is a book type object and stores books I mean uh the instance name
is called books so inside the Constructor I'm just going to initialize this instance variable so what I'll do is uh I'll be storing that object so this do books equals a new hashmap so you are free to you know give just a map over here let me just explain that so even if you just give map of books over here and then give hashmap over here this is acceptable because my map is basically only a sub type of hashmap is basically only a sub type of map so you're free to do anything so even here this
string comma book is not required but if you want to have more clarity you can give it like hashmap is the concrete implementation of map interface right yes it is a concrete implementation of map interface so if you have a map type object you can pass a reference to hashmap to it corre so map is Beav like a pattern class yeah yeah kind of kind of yeah so to avoid confusion and I'll just give hashmap here also so now we are done with the Constructor and the instance variable for library right so
the only thing which we are left out is with this search by Jer so we already know that we have to iterate through this books and only search the books by Jor right so we'll uh create a list first which we have to return we can see that the return type is list of book so we'll create an array list array list of book type objects let us call it uh filtered books is equal to a new array list now even here you can either put the data type here or just leave it out there is no issue I'm just puttin
g there putting it there for clarity so you're creating an array list of books and naming it filtered books you'll be returning this only so return filtered books so what we add to this uh array list is what we are going to look at that so we have to iterate through this hash map look at each book and get the genre of that book check if that is matching with this particular genre so to compare two strings we saw we already know that we have to use the dot equals method right so we cannot directl
y compare them using the double equal to it just compares the you know it won't give you a proper idea we'll use the dot equals method to compare the values exactly so we'll first iterate through that map see again uh in the previous question we used an iterator interface to go through it because we were modifying for printing I mean the display playlist We directly use the for each Loop here again I'm not going to modify my hashmap I'm just extracting the values so I'll directly use a for each
Loop so it is up to you to use a separate iterator there is no harm using an iterator I'm just using the forage to uh reduce my uh code load so here I have for or U see there is one more thing which I wanted to tell you so you can directly use this books. values so this uh directly takes all the values and iterates through it but for your Clarity sake I'll go through the map. entry itself um so for map. entry let me take uh a string and a book uh we'll have entry over here and books. entry set s
o this gives us the set of map. Entry type objects now from this we'll extract the value which is the book so we store that in this variable called book is equal to entry. get value so now we have the book in our hand we have to just check if the Jer is equal to the provided Jer so if book. get Jer so they are private instance variable always use the accessor method book. get Jor equals my uh given Jer which is uh in my input if they are equal what I want to do is add it to my filtered uh books
array list so filtered books. add of book and once this is done once this for each is done I can return the filtered books so I'll give you the other methods as well so this is my method one My Method two is using the values directly so that was something which we have not looked at till now because in the previous approach we required both the key and the value so here I just require the value so what I'll do is use for book uh type object which I'm going to name as book I'm going to extract th
e values directly so books. values gives me all the values of uh this particular map so this is my map this is my DOT value so you can relate this to your for I in in dictionary do uh values something like that so you can you know uh uh related to that in Python so this is something like that so for every uh book entry I'm going to check if my uh book. getet Jer um do equals my input Jer then I add it to my filtered books filter books. add of book so this is my method two now I also have another
method which is my uh iterator method I'll explain that as well so that you have a proper idea of collections so once I'm done with that I think you would you know be confident with all the can you explain the method two one one second please method two again sure yeah so here I require only the value I'm not bothered about the key right so uh in the previous question we were bothered even about the key because we were printing the ID of the song here I'm not bothered about the key so I need no
t have this map. entry itself this key is going to be my occupying memory redundantly so why uh you know have that pain of storing that key as well so to avoid that we directly use books. values so books. values extracts the values from my uh map so this books is my hashmap so books. values directly extracts the values and iterates through those values so each value is a book type object so this is my element this element in this iterable is iterated in this for each Loop and I'm checking if the
genre matches so books. Valu is again a collection right uh see let's not call it a collection we'll just call it an itable let's just avoid those uh confus no by collection I mean that it's not a single value it is g a collection of all yeah it is you know um let's call it uh let's just call it I mean a collection of book type objects correct don't confuse the collections we have in we were dealing with in Java correct and then on that my book variable is I trating yes this is my element prese
nt in this itable corre each book type object present in this iterable books. values so we have a keys as well right books. Keys we have also that function as yeah yeah yeah you have books. keys as well so if you want want to go through the key just like that yes yeah books do but here we need values that's something like that you know you'll know the methods once you type that the prompts give you mean in vs code the prompts tell me set something that you can explore I'll give the method three
as well so method three is using the iterator interface so for the iterator interface I'll first have to define the iterator it is going to be an iterator of let's just have an iterator for map. entry type objects the string comma book and then use theer is equal to um books. entry set this gives us the set of all the values and this iterator method returns us the iterator now while iter do has next um what I intend to do is um store the map. entry first map. entry of uh string comma book let's
call it entry equal to iter do next so this moves the pointer to the next element and now let me extract the book value uh separately is equal to entry. get value now I've extracted the value if my uh book. getet Jer uh dot equals my input Jer then I add my filtered books so filtered books. add of this particular uh entry which is I mean what should I add the book object book yeah so is this idea clear now so this is my method three see you also have another method four okay it's again the inter
face without the map. entry so directly with the map. values you can have an iterator so it erator of book type objects let's call it iter 2 is equal to books. values so this is another collection as you had told me so this gives me a prompt to use the iterator so which gives me an idea that books. values is also a collection okay so yeah that doubt is clear somebody was asking if that was a colle it is a collection that is why this iterator correct yes so now we can use the similar while iter d
o has next and then you know um use the book value so iter to. next this basically gives me the next value if book. get Jer do equals my input Jer add it to my filtered books so I've discussed four different approaches so this should give you a proper idea of how you iterate is there anyone having doubts so that is it for this particular question you have completed all the methods any doubts no sir okay then shall I stop the share if there are no doubts I'll stop we getting some error message si
r like two errors are no no no that is because the file name does not match here I book question. Java this is the solution file that's why okay okay okay okay sir are you sharing the link link of this uh I already shared it uh in the chat box you can see there is a folder called Java uncore questions is it accessible by everyone is it yes it is accessible is it week six yes week six yeah week six the folder week question two right question two uh yeah it should be question to I'm not sure about
the numbers yeah Q2 Q2 solution yeah yeah see there is the solution as well as the code so both are present I'm sharing the link again uh let me just check so inside week six you have q2. txt which is basically this books question Q2 solution. Java which is the solution file and then you have q1 txt which was the previous question music player. Java is the solution file and then if you go outside week six you have other questions I think there is one for week three then for week four and then t
he uh questions based on week two as well I mean the there are week three questions only so you have for week three week four and week six those are extra questions so any other questions okay then give me a minute so hi everyone so let's see some more questions on maps Okay so PR has left or what sir uh PR yeah PR has completed his portion so he left it Prav are you there yes ma'am I'm here um also like GP you have to ask something yeah yeah hello sir yes yeah you know I was just laid by 5 minu
tes today and I think you had covered that you know reset concept yeah so just quickly I will take one minute see a map map is a interface right like collection is a root interface and then under that it is list and set and you know uh three three verticals are there right so I don't get you I think at the beginning we were discussing the concept of Entry no entry set something like that right yeah let me share the code give me a minute not not the code I just need some know small doubt I'm havi
ng okay yeah so M map is map is there map is a interface right uh okay it's I mean it is storing key value pads let's yeah so yeah it's interface and then map is implemented through certain concrete data class like hashmap and then link hashmap and hash table right so what is this entry you know entry set what is this entry set see uh you have a linked hashmap which has integer key and song value entry set basically uh you know gives you a set of map. Entry type objects so uh give me a minute I'
ll this entry set is interface or concrete class what is it it is a set so the name itself suggests it's a set right so I'll go back to that particular SL yeah so just assume this is your uh map so your map has a key one and a value song So for this when you create an entry set it looks somewhat like you know over here you have a set and every element is a map. entry so this is one map. entry this map. entry is nothing but a a key value pair stored together so you have the key and you have the v
alue again this is another map. entry it has a key and it has a value in this particular example my key is an integer so this is an integer and my value is a song so for every integer come song value pair that is this key value pair I'm iterating and Performing some function so this by itself is a set collection so that is why I'm able to use an iterator on this set so this is a set I'm able to use the iterator on this set because this is a collection okay so entry set itself is a concrete class
huh uh let's not call it concrete it is basically returning you a set entry set is a method so entry set is called on a map so this entry set is a method which is called on a map so map do entry set this returns you a set of map. entries so this is a set containing map. entry as objects is it clear okay let me go through it again and then if require I will okay thank you okay M I'm done uh I'll leave the meeting now you can yeah thank you [Music] so this is uh again a week six related question
Java program below takes as input the names of cricket players in a team and the Run scod by each of them in three consecutive matches the program is supposed to print the names of those players who have scored at least 80 runs in all the matches complete the code to obtain the specified output uh let's go through the code class team has the following members first one is instance variable a map so key is string and the values are an array list of integers and the name of the instance variable i
s player map maps the player name to the list of R scored by him or her in each match a Constructor to initialize the instance variable and then an accessive method to access the instance variable player map in class F class we have a method get final list that accepts an object of class team as input and Returns the list of player names who has or have scor at least 80 runs in all the matches so this is the main thing that we have to do main method accepts input to instantiate an object of Team
the input is accepted in the order player name followed by the list of his or her runs then invokes method get final list by passing an object of team as input to return the list of player names who has scored at least 80 runs in all the matches prints the list returned by the method get finalist what you have to do is to define the method get final list and the rest of them the rest of the things will be already done so suppose let's go through the test case if these are the player names so fi
rst the input will be like player name yeah' can I just say what what the question was again like I lost the connection so I was not there actually yeah yeah I will explain again okay so what basically this question does is it will take player names along with three three uh runs I mean along with the rents in all the matches so if there is like three matches done so what score a particular player has scored in a particular match so list of runs scored in all the matches three matches has alread
y done so in each of the match what trends have these player has scored so revi has scored 76 in first match and this is for second match and this is for third match and we have a player map that player map will have three different players along with the runs that they scored in three matches and the output that I expect is only the players who has scored at least 80 in all the matches got it okay yes man yeah so if the in this case see here REI has scored 76 76 and 76 Sonu 80 80 89 and viral 9
8 47 and 99 if you see the second person Sonu has scored 80 80 and 89 which is greater than 80 all three Rs are greater than 80 so this is the condition to be satisfied so the output is only son uh why V is not included in the list because in the second one it does yeah correct revi anyway has not scored 18 any of the matches so if the question clear let's go through H see this empty thing also here P1 has scored 79 80 and 45 disqualified here P2 88 46 and 90 here and P3 89 56 and 21 again here
and here so got an M list so the template code has as we have seen in the question in the class team we have to define the instance variable and the Constructor and the accs method so class team and this is a map map of string value is string string here is for names and array list of integer this array list will be used to store the runs scored by each of the player and the object name uh the reference variable is player map and here uh the Constructor is taking m a map M and initializing the p
layer map here uh this is a accessor method get player map which will return this particular player map and in F class we have to Define get finalist uh method okay that's our goal and what second line can you repeat please uh second line this class team first line I understood the second line what it is is a Constructor this one right yeah yeah this is Constructor okay so the team is the class name so Constructor name name is also a team taking this particular map object okay okay so here uh he
re this is the thing that we have to do Define the method get final list what else is given in the main method we have scanner class object and here creating a linked hash map so you know that map is the interface right so map with the interface we can create the reference variable and uh the right implementation link hashmap and other hash map other maps are actually the classes from the interface map so here you basically we are creating a link hashmap so the object is of type link hashmap and
key is string and the values are array list of integer now I want to add each of the player name along with the Run scored by the players into this particular map so using a full loop I want to add three uh players so I equal to 0 to I Less Than 3 012 so first what I have to take is array list of runs so array list of uh no no first I have to create actually an array list for the rents so this runs will be integer type so creating an array list of integer and then this particular string name is
to get the name of the player so first input is name and looping again we need a for Loop so that we can take three runs and add all those into this array list of player runs so p is an array list of integer so here uh I will get an array list of okay and I am taking uh here parents do add SC do next in here I will read each of the Run scored by the player then I will add that into this particular array list anybody has doubt here actually ma'am here like I less than three means three input I b
e putting and three input it is on the runs only right so the first name let us say I'm putting the first player and then corresponding three runs how it is getting linked I did not understand that yeah see here so here just I will read the name of the player so suppose it is P1 mhm so I will P1 I'm so sorry sorry I'm so slow so this is P1 and next I will read the Run code by P1 uh actually there is no connection now connection will happen here only so next I will read three input which I will p
ut into this particular runs list suppose I'm reading 90 and then 90 and again 90 okay so now what I have name as P1 and PS as this values 909090 the this array list then yeah then here we have this map right pmap so into that pmap I will add the name and the list so our pmap is a type of string and array list of integer so the name will go for the string and the perents is actually a list of integer that is the value for that particular map so adding adding is here put pm. put name comma par th
is addition is happening in the Inner Loop or in the outer loop like this is happening in the in in this particular Loop not in this Loop this is happening in the outer loop right outer loop to this yeah okay so once the inner loop is done the cursor will come to P map. putut right yes yes after after it reads the run for a particular player got it then it will be added into the map yes yes and then again it will start for next value of yeah that's for P1 done then go to next uh player next for
P2 and again uh we'll read the run for P2 add it to the map again one array list will be created right for next yes yes yeah so the the inner loop is for all the runs which is the value right the Outer Loop is actually for the players ah yeah to add into the map yeah so that means once I finish one player which is just outside the inner loop then we need to add because all the three rounds will be available at that correct correct yeah yeah okay makes sense yeah so then uh one more thing is we w
hat we did we created the map now now we have to create an object of team so team has instance with variable player map which is also string array list so for that we are doing all this now we have to create an object of Team passing this Bap okay ma'am why it is then requ because I already I have the player and their list of numbers and I'm adding into the pmap now again pmap I'm converting to teams like separate teams creating the uh in team actually we have to uh actually this is just for thi
s purpose we this get final list method is actually taking team object not this not the yeah so that means P1 becomes team a or team two Team B TMC like that player player to player no no no no then see here uh see here this player map now player map will have have all the players right yes how will it look let's see M uh just one thing uh I have a session currently in MLP right now so will you share this PDF after this session uh yes yesterday I shared PDF right in the same link I will upload y
eah we got yesterday link yes can I share that link once more um for shade okay okay thank you so so this is our player map and suppose consider that uh here I'm storing the key so this is for player one and then I have player two and I have player three okay and the values are array list array list of what integer array yeah so this is my integer array and is it so inside I can't so this is each of the each of the runs runs correct so P1 along with the RS in the same way we will have P2 along w
ith the RS and P3 so now in class t team in class team we again have the same map object so map reference variable so we will pass this particular map to this particular player map now in class team object each of these object will have this player map right so here we are passing only one team it's basically like passing the same player map understood yeah so basically the team class encapsulate the entire map as a private element yeah we can say like that yeah that is exactly ma'am if you see
the output as for the question that okay like if the player has not score like the P1 P2 P3 and if each each player wi we are comparing whether he has scored 80 marks or not right it's not a wise right now in the first public first public test to uh the one which we SH now weing P1 it is not scoring you know 80 so even P2 are not scoring 80 in each of the things and that is the reason it is coming the list is empty right so now that particular method how we can separate the or we can say we have
to take this particular map okay and we have to iterate through each of the players each of these players and check the values and see whether uh three of them are greater than 80 if three of them are greater than 80 then we have to add that particular player into the new list new list of string right we can create a new array list of string ma'am that is already there Team app play can you please go up uh here before that pmap is there no pmap includes the list of players in addition to their
uh three types of RS right yeah now we have to filter that so we can iterate through pmap right yes yes uh but here we are following the structure of this particular question so so basically get final list method so if the input is clear then here see we have we will call this particular method get final list passing this team object T okay so in get final list method if you see uh the what we say the headed of this method the return type is an array list of string so array list of string is exp
ecting only the player names which has code greater than 80 okay is the question and the template clear then I will go to the coding part yes ma'am yeah then so here uh the only thing we have to do is Define this particular method I think uh like PR discuss two different questions right you will have an idea on what we have to do here first let me create the a list to store the names here this method is static in nature sorry this method we are defining as a static in nature yes yes the question
tells that or actually in template code itself it will be template signature is already given yes now I will be taking this [Music] and this is like extracting from the object that we got from t T do get player will get player always uh Returns the player map right so I will call get player now we got this player map this particular player map here in this B map okay okay now I want to Loop through this particular P map I'll go through the key set using one flat variable let's Let It Be True no
w what I will do is I will take each of the runs ma'am you're assuming the batsman is good that means here she has scored 380s in a row right so that is the default assumption so if I find any evidence against it then I will set the value of flag as false okay got it yeah first I assume that it is above 80 so get method will help me to get the values at particular key so then what I will do is I gives a values that values this particular yeah oh for that okay then I have to check it ising key an
d we get value right in line number 19 we are passing key and we'll get value right yes yes so this key set will give set of keys correct I will iterate through the keys so one first one will be the first key so get off key will give the first value now if it is less than 80 then I will set it as false so ma'am this when you do the get value the value will be asically basically be array of yeah integers right so that will be an array list of integers and here looping through the array list of in
teger we are using this for each Loop for I in the array list of integer perfect yeah and checking whether I is greater than 80 or not so if I less than 80 I will set the flag value as false and then we don't have to check further right so ma'am yeah uh ma'am that I value it is integer and the p is the player list correct now in the for Loop when we begin like uh on the list of key set that is the values and we are checking the players yeah right in the for line number 17 yeah here we got P1 P2
and P3 set of P1 P2 P3 just for assumption I'm using this this uh yeah so in the first line of this 17 what will be the output like we are getting the P1 or P1 corresponding values like 70 80 90 like that no no so see this is string variable key string so this will be P1 for string p means it will be the key P1 yes now the values also will I will get the values also from the P P1 then you have to use get value or get here we have to use get actually okay so get p means I believe getting the P1 v
alues like if I is zero I will get P1 values corresponding P1 values like 70 80 90 that particular that is an array list right so we have to loop again through that array list yes doubt is correct he's asking in the line number 19 what will we get yes values of P1 yes you're right so get of P get of P will be uh example if it is 80 90 90 so P1 corresponds to one particular array list of integer which is the scores sorry run scored by the player so this is get of P Min now for each integer in thi
s particular P map. get of P which is this particular list so first it will take 80 now checking if 80 is less than 80 in that way okay got it thank you yeah now now if one is false then we just have to uh exit from the loop we don't need to check further so that's why we use this break and uh then what we have to do we have to check the flag value if flag is true sorry if flag is still true then we have to add the value p p in the sense it will be the string name so player one if player one sat
isfies this condition and if flag is true then we will add that flag value to this particular P list now we have to finally return P list I something wrong here full loop is completing if condition the outer loop the return has to go outside I mean after the outer loop gets completed yeah after so this will flow is the full perfect let's I will explain this again after let's see whether this is fine 18 90 180 B2 there is space with yeah correct so here P1 uh 80 90 80 others have values less than
80 so P2 and P3 are not included in the output list any doubt what is the output in this sorry P okay P1 yeah okay I hope it's clear the logic is easy ma'am only thing is this uh the hashmap we have to make sure that uh depending on the entry here the example is key string and values are ARR list in the op similar concept but we have to notice the key value pair whatever is the data type we have to use that map then um whatever the data type for the key then whatever the data type for the value
s then that's basically the key right then correct right after that line 17 will always remain the same except here whatever is the first data type because first is a string that's why you do string in line 17 everything else will remain the same and then uh in line 19 because it is a integer array and all that that's why you're doing it otherwise if something else then we may have to change the data yeah if it is a double array then I would right so if we kind of get the construct then I think
you know Sol it's kind of making sense now right so it's a very easy thing in Python we can do it in you know two minutes but jav I slightly harder yeah so so uh in the line number 16 that t do GL player map so we are sending t to the get the map right yeah so this particular method is taking an object of T So using for that particular T we have get the map it's it's like that right correct correct so in the first case this will be our te so ma basically the map itself is a private object inside
the team class so because it's a private object we are using the accessor method to get the values of that private variable inside the team class yes yeah okay and uh do we have to get player method map there yes yes see here okay okay yeah this is private instance variable and using the accessor method get player map we will be able to access this player map object I mean instance variable yeah uh okay everything okay so I can move to next question you can also try with entry set okay for the
same question you can try later as now you know entry set I think you will be able to do this right then should we use iterator for the entry set yeah then you have to use iterator okay or just you can go use the for Loop for Loop is fine don't even have to use iterator yeah just you have to get uh using the map. entry you'll get both this then you have to use git value and the uh git key separately okay so that it's about uh so first we will see uh some other questions in our class are these bo
nus questions or part of those four questions only sorry sorry are these bonus questions or part of those four questions four four questions because this seem to be a bit challenging basically we are doing of collections like map is there and then within the map itself the value part is itself other collection like AR list or something like that right yeah but if you understand that particular question it's fine to go without okay yes which one just now which you sol yeah the diagam the diagram
you did actually is very important the very outside is the team within the team we have a hashmap and then then hash map has entries called keys and values the keys are basically the player names the values are essentially uh integer array of runs in this case three three Innings basically storing three Innings that's it so if we get that structure of objects because objects are basically a hierarchy right so if we the tree structure right so if we remember that then then I think it is very clea
r all the code and everything so to us a while to understand hashmap is actually an in entry in inside the team class so that part yeah only that we added there others are normal things I guess uh but even the keywords are different no here you have used get uh get P that itself giving the value but iterator I think you have to use get value right something like that so yeah wordss are very much different syntax we are completely at least I'm not aware of all we should stick to one I believe rig
ht yeah recomend for beginners like us should we go through the iterative method we'll just freeze our brain on one right I don't think you know which one would you recommend for beginners people who are new to Java I think if you learn iterator then you don't have to learn the other method correct and it will work for any kind of uh you know whether it is uh because the key and value data types may change hmap concept is constant but depending on the problem and context if it is here is a crick
et match right so player are the keys and the runs are the values but if you change it to library books or whatever author books there so many things right yeah so we should learn a method where irrespective of the question and the problem domain we can apply that knowledge mindlessly right yeah yeah okay so iterator is what you are recommending so that you will be able to do anything but for this question uh this this is fine this is simple actually for this particular question but but I think
if we learn the iterator method that's it so then we don't even worry about what problem is there we like a warrier will go and Sol that yeah because in the session he taught iterator but this again the syntax of changing of get and get skis it suddenly everything is changing so it is really confusing that's why I asked whether we can use iterator here you can try you can try Okay anyway we have the flexibility use many methods and you know right the you can use Java documentation you can go thr
ough the methods and see each of this methods how this we can access documentation during the op yes it'll be given in the portal for for us to access nice I didn't know that but we don't even know how to use we have to refer correctly right we not used to that that's the problem another problem so suppose I'm dealing with a linked hash map this is like using Google uh like getting into trouble I think it's better to have limited knowledge and you know we we have created our own hashmap ma'am fo
r the op right so we'll only use that smaller hashmap oh this is like ocean and it again will confuse us more that's right suppose you forget the method name whether it should be small or capital but if you know the that much help only small s or Capital abolutely right it is bit challenging even in MLP also generally we get you know documentation but we hardly use no MLB helps a lot because we'll forget the method things the param Google also helps you know that auto prompt comes sometimes that
's true true yeah yeah let us go to next question today seem to be a challenging day no problem now next is in class question so here uh write a Java program that accepts a list I think uh this will take time let me take this tomorrow anyway yeah just give us a simple thing so that at least he'll pass Rel no tomorrow what you have class uh 8 to 10 if you want it earlier also you can tell like like at the same time 5 to 7 right it'll be easy I think there are few people who are attending MLP op s
o that guy B Basu was complaining not complain not complaining sharing about the morning yeah that is also comfortable like can we give for 10 10 to 12 yes okay but where will you will you update in the calendar I will update in the calendar as well as uh send an announcement yeah sure because last minute also we'll be mugging up some syntaxes yeah 8 to 10 is too late so too late and uh and 8:00 is the op next day next morning that me you to log in at 7:30 and do the true yeah it'll be helpful i
f you teach in the morning I think the timings are got updated so I have also got the timing 8 to 10 9:30 something but then they updated me with the new timings from 2 to 230 oh that might have changed for you not for everyone right okay you can check I mean maybe it's the confusion yeah there are two slots there are a slot 8 to 9:30 then there are four slots in fact so they're trying to that's a hashmap you got assigned to a different I mean you can just please check it one no for me it is sti
ll 8 to 9:30 that is not for everyone yeah I think we have system command system command system command from 10 to 12 so it is it is still it to 9:30 for me yeah 8 to 9:30 and people who have two Ops it's morning slot every okay so you say that inner class question we will discuss tomorrow right one more question last one Fair yeah we are open to discuss question any questions and then please update the timeing calendar yeah yes yeah we can continue today I will stop by 7 that's why I want to do
it quickly so write a Java program to print customize messages on the time needed to read books depending on whether each book is a novel or a short story so this is a pure abstract class question class book is an abstract class with the following members instance variable book title of type string Constructor accessor and mutators as needed and an abstract method compute reading time then class novel it is a child class of class book here it implements the abstract method compute reading time
to return the string book title takes a week to read with book title is the title of the novel uh class short story is a child class of class book it implements method compute reading time to return the string book title takes it day to read here again book title is a title of the short story so basically we have class book which is an abstract class with the abstract method then class novel and class short story is actually uh the subass of class book which uh and both of them will will impleme
nt the method compute reading time now class test has method main that is the following takes as input the number of books for each book it takes the type of the book n for novels and S for short stories and the title of the book as inputs number of book then a n or S along with the title of the book now inv works the right version of method compute reading time to store The Returned messages in array so here compute reading time we will invoke the compute reading time according to the object to
return uh and then we'll store the messages in a list and then we print that particular array print the stored message for each book when you what you have to do is Define classes book novel and short story now in template uh so this is a template for this you have to Define abstract class book then Define class novel and Define class short story and in the main method what we have is we have two uh we have an array list messages list and this array list will be storing the uh messages so here
number of books will take the number of books and looping through zero to the number of books if it is given three then you have to read three books first you have to take the type of the book whether it is n which is denoting novel or yes which is denoting short story so if this type is uh equal to n then here creating reference variable of abstract class book and initialize or creating an object of Novel along with what will be this title of the book so if it is in directly going into this if
block creating novel object along with the title that is taken from the user and According to which book Noel or short story we have to invoke the method compute reading time and compute reading time will return a string like this what was that book title takes a week to read so uh take that message and add that to the messages list that we created here so this array list is used to store the messages so next if the type equals s then it is for short story then we have to create an object of sho
rt story taking title of the short story call s. compute reading time so this compute reading time will invoke the method that is defined in the short story class and that way this computer reading time will return a message that we will add to this messages list so messages list is array list of string the first one will be for that if it is novel it will be like book particular book reads will take this much time like that and then um looping through that particular array list of messages whic
h is of type string so we have to mention for each string yes in the messages list print that particular message is the main method clear what is the objective of trim function trim function here like we are when we are taking know next line we using trim function do trim oh this okay this is this will remove the triing space and they will remove and the space only it will take so it is mentioned in question to no no this is actually given in the template code in order this is already given okay
fa so we have to check with s right so if it is like some as it is a string if it is like space yes then then it will give it will not evaluate to this particular uh equals that's why to avoid that kind of AR so it's a part of main method yeah yes yes ma'am this is using Dynamic dispatch obviously so in the op would we be asked to do the main or typically the questions will be to define a class or a method this is actually the most complex part of the code the main method uh we can tell exactly
that because sometimes if you have seen the previous question there we were doing in main class right that static method right so yeah you can expect any way okay so fine so part of the so a template would be you know if there are two or three classes the question will complete any of the class or methods okay yeah yeah okay so so book has instance variable book title and this abstract method comput reading time this is the Constructor that you have to Define which will take string team and ini
tialize that's about abstract class book next thing is you have to do uh this Constructor what is in the Constructor initialize book title ah initializing yes so that is an abstract class now this is the regular class novel novel extends book now novel uh Constructor will invoke The Constructor of the book class super of T by passing this title here and uh here implementing the method compute reading time so book tile plus takes a week to read so as this is a I mean not a private variable he dir
ectly using book title and in the case of short story again same The Constructor will invoke the super Constructor which is of book and then compute reading time again same thing book title plus takes a day to read so in the case of short story takes a day to read and in the case of novel takes a week to read now uh rest I explained already uh is that fine yes ma'am typically the variables in the interface has to be public there's no point in keeping a private variable interface because you have
to implement concrete implementation yeah so defs the purpose it's generic so that it can be copied and other CL can be created out of that yes [Music] yes and yesterday we had some thing right uh in copy construct question that I will explain now it's very simple actually so I will be taking two books so first if you see here the first one is number of books so uh this is a [Music] novel short story some some short story name so Prejudice takes a week to read for novel and grandfather takes a
day to read so according to the object it invokes the method compute reading time and uh is that fine only one question ma'am like whenever I'm extending a class right like I'm doing the inheritance part so it is mandatory to Define my construct by using super keyword right like yesterday also we solved one question there also we were using the super keyword and then passing the name right so is it mandatory like whenever I'm extending a class for inheritance purpose so the Constructor of that c
hild class has to be defined using super keyword is it mandatory kind of thing so if you okay there are many different cases so I have discussed this in I think uh very quick response I'm not saying that you know we need to revise the concept of Constructor that's okay we can galiz that so I'll show you this there are many different things so explicit call to parent class Constructor using super super so if you call Super like this so it will invoke the super Constructor with these parameters Th
e parameterized Constructor that is present in the parent class and suppose in the second Cas see here uh so here this case is Def constru class by jav so so in this case uh we have uh this particular Constructor in the child class okay and the stes only department and bonus at the same time here here we have a default Constructor so in this case I have given the default Constructor but basically compiler will give a default Constructor if it do not provide any if it do not provide any Construct
ors then compiler will provide that default Constructor the default Constructor uses the keyword super the constru The Constructor given by compiler always uses the word super uh no uh actually if you do not specify any Constructors here if you do not call using the super keyword here then also it will invoke the non par amzed Constructor default Constructor in the parent class that is another case and here you don't use super word it will give the default values null and Known Unknown right yea
h only if you use super it will inherent inherit from the parent and it will give that particular name and whatever it is salary or something otherwise it'll give the null pointing if you don't it's mentioned super correct suppose uh if that is the case when we do not provide anything in the while we're creating the object if you're not providing anything then that is like calling the default Constructor right so in that case that if that case will happen only if there is no Constructors are def
ined for a particular class so you will get error if you uh the fairing class no Constructor Define and in the car class also no Constructor Define in this case see see what exactly happens in this case is we have not provided any Constructors for class card so compiler will give Constructor here which will be default Constructor okay and again this that default Constructor will actually call the default Constructor of this particular parent class so if default Constructor is called then the val
ue for model will be as it is string it will be null n and for yeah here it will be zero zero yeah and then uh it will continue to do its own Constructor here so basically it's like Constructor chaining anywh the parent Constructor will be called and then its own Constructor will initialize the value for in and I mean number of do and F Tye so that's a implicit call to the super yeah so using super it's explicit call and without using super in this particular case it's implicit call and one more
case I can explain here itself is if we had a Constructor here suppose we had a Constructor here and that Constructor okay we have a Constructor here and in vehicle we do not have any Constructors in that case again this Constructor will invoke the which Constructor even if it do not specify super keyword it will invoke default fine uh you just go through this you can exp not defined not defined I'm telling if we Define then it will invoke the default Constructor if no other Constructors are pr
esent in this particular uh class okay this docent this document is there in supplementary content not in supplementary but everything is in disc this is in which we week two week three week three week three um man' you sh the discourse link no in that only one question set is there so all these questions related to 2 3 4 can we get in the discourse link can you share that link please which questions are not there uh this op questions and also the the questions related to week 2 three and all wh
ich are the practice live shared before itself you just go through extra resources you will see codes discussed in particular session okay yeah and see here yesterday somebody had doubt how this is actually not this here right so I I don't know whether that person is here today so when we create an object of uh when we create an object actually what exactly happens is normal this particular student class has name and courses right so so that will have that will have this name variable okay for n
ame the reference variable for name will be created and then again the courses will be here so what exactly happens in copy Constructor is from S2 uh the from uh I'm talking about the previous example of student S1 and S2 so suppose I created S2 here and this is S1 already created with some name and values then using copy Constructor if I create S2 from S1 normal creation happens like the name and the causes and the values will be copied from this particular S2 the name will be copied from S2 to
uh here s sorry S1 to S2 and for the courses we did create a new array and then we pointed to that new array that's what happened happened so and one more yes I think this we discussed yesterday also so it's simple like the name is a atomic value it is getting copied directly and then the courses is basically a collection kind of thing array right yeah object so we object so we are using for Loop and we are extracting value by value right yeah okay and one more thing is you had confusion in thi
s keyword right that I think that was also clarified this you know we can use when there's a name conflict then that becomes mandatory otherwise it's optional kind of thing right yeah so just see what happens actually so in this case using this keyword and using the argument name as the same register number but it doesn't uh uh arise any error so yeah that's what exactly we do normally now if we do not specify this keyword and trying to assign uh register number to register number just predict t
he value for register number now so it is getting confused right see uh yeah so it won't raise any compilation error actually here what basically happens is this Reg number value will be reassigned again to register number and here for this register number it will give the null value and suppose you line number 15 within the parenthesis if it could have been simple simply R right string R then it won't create any problem then registration number equal to R right without using the this we can wri
te right yes yes correct yeah yeah this was clarified yesterday I one of participant explain it and you also explain it yes yes so fine then ma'am did you get some time you as that please mention few Concepts like I had men that you know equals method and then tomorrow we will do compare to Etc okay okay okay the remaining questions which are important for op are still there are questions remaining already start 9 10 question yeah we have to deal with inner class question uh and uh compare compa
rable using how to override the compar to method and all that is op1 comparable and comparative isn't it so yes I don't have asking yes let us ma'am please let us know what are the type of questions like Constructor and compar like please list it out so that we can refres so uh all this for the op yeah I will go yeah yeah I'm dealing with op1 only okay and comparable comparator we have discussed in some interface week I guess so it's important so Dynamic dispatch inheritance uh Dynamic dispatch
so method overriding is done there um what else you remember from week three face uh you know these abstract methods and all that all the collection week by we collection week six uh' only this okay wait wait wait then six we have many array list and you can expect a mix of these things mixup question only becomes difficult understood so so hashmap map in the sense map is an interface so basically the is hashmap yeah and uh yeah uh you learn how to override these methods like compare to equals e
quals I have discussed in Q one uh detailed manner I guess string also places they asking over two string overrate two string method will be common in most of the questions and and simple things you have to remember is one thing is you can return an object in a method right yes okay and uh and always remember that you can create a reference variable for an interface or an abstract class and you can assign an object of class which Implement that particular interface that you have to remember uh a
nd simple things yeah always consider about private variables uh using a super yeah okay so uh in the method or if uh we have to inherit you can write super name while we printing printing stat sorry uh while no uh the super keyword we can also use in the return statement when we want the name when the instance variables are private like name or salary yeah yeah there also we have to use key uh yeah either you you can use that also and also you you will be using sometimes the public accessor met
hods in mock two mock 2 we have to override certain methods and there we needed that name the mock 2 question I have stuck there so we have to use super to get the method itself we have to use okay yeah super dot that yesterday we discussed something similar to this actually okay yeah you should do that so remember this calling the methods or getting the value of the parent class how we can get that using the public access methods you can use that in the return statement also so okay yeah thank
you everyone ma' I think you was trying to hello ma'am hello yes yes I think he was trying to ask that one

Comments