 Hi, this is Christian. In this video, I'm going to complete or build this assignment for the sandwich so we have three, I think three parts, right? There's three parts. One is to create the bread class. We're going to create the sandwich filling class and then, actually four, right? The sandwich class which is used for a composition as we discussed in this week's lecture and then, finally, we have the main application to generate these sandwiches. Okay, so if you look at what I have down the bottom here, I have a simple run to show you what it would look like. Of course, I should have probably you know, I want to call these not bread one, bread two, but maybe like sandwich one, sandwich two, and so on. And then down here is the UML diagram to show you the different pieces or you know, functions and variables or instance fields in each class. So the sandwich class test sandwich is just the main application and you're going to instantiate three objects in here, three sandwiches, and then the sandwich class, is the one that has the composition, right? So it contains what is composed of the bread and a sandwich filling data field and then those are mapped to these separate classes over here. Okay? So again, this box just tells you what are the fields and then this is one of the methods you need to create for each class. Okay? And okay, what I'm going to do here is just one of the ways to do this. There are, I'm pretty sure there are many ways to do it, but this is a fairly simple program. So the differences may not be that too too many, but you can't be done in a different way. So I'm just going to show you one way how you can accomplish this, right? So using this as a guide as well, and if you are kind of like stuck, if you don't know what to begin, just write it out. Write it out in words and sentences. We call those pseudocode, right? And what have appeared is pretty much kind of pseudocode ready. These instructions are pseudocode, right? Kind of. So we're going to create a class called bread and then under bread class we'll have two fields. Okay, and then for like the calories and then the bread type. Okay, so we'll refer to this document. As we continue on, so let's go to the IDE. I'm going to go and create a new class. This is the bread class. And before I continue, I should put this into the package. We'll call it chapter four sandwich. Yeah, that should be enough. Okay, so use that as the package. And then we have more, right? So let me go in here and the sandwich. Create another class for the sandwich filling. And then one more, two more, this sandwich, then the main program. So this one here is just a test sandwich, but you can call it whatever you want. Okay, so I'm just going to call it test sandwich. Follow my instructions there. Um, is that what I call? Yeah, well, okay. So this is the main main program part of note here. Main application. We'll leave that for now. Let's go to the bread class first. So we need private bread type. This would be a string. I've got to put that there. And then we have one for the calorie. So calories are usually measured in double. If you want to use double, if you want to use just, um, integer, I think that's fine too. We'll just go with integer. We're just assuming it's whole numbers, like, you know, 100 calories and so on. So you could hear calorie per a slice of bread. Okay, because the sandwich is made up of two slices. So this is about one slice. And when we do the calculation, if you go back to the instruction, ask you to multiply this slice, uh, calorie per by two to get the two slices, right? So we get that and then put notes here. These are your data fields. And now we have a constructor. I will constructors here for both of them. The first one is the blank constructor. You need that. And the other one is the one that kind of have both parameters. So the string for the bread type and then the end for the calorie per slice. And then down here we have our getters and setter methods. And just use a shortcut to do that for you. Okay. And then we can use those to set our properties. So this does set bread type bread. This does set Calc per slice Calc per slice. Okay. So this part is pretty much done. I think I asked you to also create a static field called model. I think we can just do that right here. Let me see where to put it though. I don't remember the instructions. Include a public final stack string for model. Initialize it to the staff of life. I think that's the only one, right? Yeah. This is just for practicing using static data. I mean it has very little meaning here but we just put it here. So this is going to be private. Is it private or public? Public. Okay. We'll make this public. Public static final model is going to be assigned to the string the staff of life. Okay. So that's our field. Okay. So this part is done. Let's go over to the sandwich filling. This won't be kind of similar. These are your data fields. Private string for the sandwich. You can call it just make a short filling type. And then private and filling the calorie per filling. Okay. Something like that will be fine. And then our constructors. This is the empty constructor. This is the one with these two parameters. Okay. And then we're going to set them. So again the setter, getter and setter methods use the shortcut and just fill those. Okay. So this is done. And then we are done with this too. And I think let's go to the sandwich. This one here is going to have the data fields here as well. And it just basically has two fields. Okay. These are used for composition. And bread compositions are when you use a data field as a type of another class. So we have the bread here for the bread class. And then we have the sandwich filling for the sandwich filling class object. And then notice I did not instantiate it right away. You can if you want to. You could do like new bread like this. Only thing is that if you do this way then you have to manually add the content to the class. Right. Because you're already pretty clear here. But if you want to wait and then in the constructor you'll see in a minute how you can actually make it a little bit faster. So here are my constructors. So I'll do just put two constructors. This is the empty constructor. And this is the one that has all the parameters. So bread will be bread again and then sandwich filling sandwich filling. And then so again just like before getter and setter methods. Okay. So now remember that these are objects. Okay. These are not primitive type. So when you return a bread you actually return the object of this bread class. So if you want to print later you have to go through the get bread function dot and then go to the bread class to get those information. All right. So let's set these the bread and then this that set sandwich filling with the filling. And then okay notice that when I set the bread and the filling they come in as an object. That means that the fields when you create this bread and the sandwich filling objects they would have already been set to have all this information. They type in the calories in order for this to work. Okay. You have one option here. This is option number two. And I put here this is the default constructor. I'll just put another one here. Okay. So you can see the difference here. The I guess the power of using constructor overloads. So we have another one here sandwich. This time I'm going to I'm going to put something very different. So what do I need for the bread? Well the bread needs two things a strain for the type and then a integer for the calorie. The filling is the same thing right a strain in a calorie preserving. So this option can give the developer or user to provide both information instantly. So I can say something like this. So I need a string for the bread name or bread type. I need one for the maybe calorie per slice of bread. Okay. These two will be passed to the bread class. And then the next two will be passed to the sandwich filling class for strain. You know filling type and then the int cal per filling. So if you think about it these are right the same variables I use over here. Okay. So I'm going to pass them to this to this class when I create the objects. And then here now notice because I did not instantiate the bread object yet. Right. So you need to instantiate the bread object in order to make it work. In this case this part here because I'm passing in as an object already. So the bread object has already been created. So you don't need to create that. Right. But here I have to do that. So what I mean is I have to say bread is new bread. And this is why I mentioned earlier that you don't want to do up here. If you did up here then you can't you don't have this option. Right. Because I haven't created my object yet. I'm just declaring or defining a placeholder to hold the bread object. So it doesn't exist until I either assign it to an existing bread already or I create it right here. And so when I do this way I just pass in the bread type and then the calorie per slice. And then now my bread has been my bread object has been created because this bread object is this bread object here. Right. And then I can also do so in this case you can do bread or you can do this dot bread. It's actually safer to do this dot bread. And this dot sandwich filling is new sandwich filling. And I pass in the filling type and then the count per filling. And there you go. I have my two objects created. Oops. I have too many parentheses in here. Okay. So my two objects are created and then now they are assigned with those two objects. So we have three options to create. Right. All right. So go back to my other question was if I did not do this down here say if I did this I just do the bread part. Okay. If I did that then I cannot re-declare this again. It's already declared up here. So in that case then I have to do something like this. I have to say bread you know set bread type and then pass in the bread type. And then bread dot set calorie per slice and then count per slice. So you have to do it that way. Right. You have to call the functions to set them this way. Whereas if you did not you know create them yet you did not instantiate is the term. Then you can just pass these two properties or arguments directly to the bread class. Right. So that's the difference. So I'm going to go back to what I had earlier and we'll keep it this way. Okay. So this is done and then now we need to go to the main program and do our action here. So we have the main method and inside we're going to create three sandwiches but also ask you to create two functions. This will be two functions. And again since we're not creating objects of this sandwich class you test sandwich class. You want to call this function directly within the static main. Remember static can only call static functions. So this function has to be static static and this is for uses for the calorie calculation. So let me see what my what I have here for the output. Okay. So it says down here a method to calculate total calories in the sandwich. And then it says the sandwich has two slices of bread. Okay. And this method should take two parameters the bread and the sandwich filling. Okay. And they have another method to display the data. So the display here would see what you see here. Okay. So I see that my calories are in double or decimal numbers. If that's the case then you can just easily go back and change it to decimal. Maybe we should do that. Maybe just make more sense. Okay. So we can just change it to double. All things if I do that to change it everywhere else. Here down here. Okay. And not that bad. Double double. And then yeah this is this is fine. It's just in here. I have to have double here then double here. So we're good. Oh yeah. And the constructor too. So in here double and then double here. What I have the first slice double. Yeah that's fine. Why is it red? Oh maybe my sandwich. Yeah I think it's fine. Maybe just out here. So double calculate total calories. And it takes two parameters bread and sandwich filling. It's a long one. So two objects. Okay now I have an error here. Why is that? Cal per slice. What's wrong with this one? Oh one more here. Yep inside the function parentheses. Okay. So now we got that and then we're going to return the total calories. So I'll put here total. Okay so we're going to declare a variable here called a double and then total. And this is going to be assigned with something. Okay so what is the total it's going to be like? For now let's put zero here for now. So we're going to do a calculation of the bread calories. Right times two plus the sandwich calories. So it's pretty pretty simple right. So I can replace this naturally. If we replace it now since I know where it is. Bread dot get calorie per slice times two for this one. And then plus yeah I'm going to times two first right. I don't want to times the other one times two for that. And then plus the sandwich filling dot get calorie. Only one for that one. And then I return the total to wherever it's being called. So that's my calculate total calories. And then I have another one here to show the information. So this will be a void. I'm not you know returning anything here. So what does this say? It just say display data. So you don't don't have a name here. You can call it whatever you want. So we call it you know show or just say print result. Okay so what do we need to pass in here? Well maybe kind of similar thing right. I need to print the bread and then print the filling. Because I need to access those so we put here bread. Oh actually you know what I'm not going to print the bread. I'm going to print this sandwich. So I'll be the sandwich object. Because the sandwich contains both the bread and the filling already right. So you don't do pass those in there. You can obtain this information from this sandwich. Okay so really in here I could just you know pass in the sandwich and you can get these information too. Like I mentioned in the previous couple of minutes ago that you have many ways to do this. Okay and yeah well I'm going to show you what that is. So remember about method overload right. I can overload this with something like this. Okay just the sandwich. But here I would say sandwich dot get bread get calories. Okay and then here will be same thing. So sandwich dot get sandwich filling dot get calories. And I should have a wrap. So you can see the different ways how to do this right. So I have two functions now. And they are both returned the same information. Here I'm passing two objects. Here I'm just passing one object. Because this one here contains both already. Okay so either one of those will work. And I just leave it here because it should work. Now this part I'm here for display. So I'm passing it this justice sandwich. Because I can access all this information here. So here then we're going to print a message. This is the model. So the model will be sandwich dot bread. It's in the bread class dot get model. And I think I didn't have a getter for that one. But it'll be just model. Okay you notice if I do that. I did have a getter. And the reason why is because it's set to public right. If it's set to public then there's no meaning to have another function to you know return this. The reason why we have the getters and setters for the other types is because they are all private. And since they are private you cannot access them directly like I did over here. Now you cannot just say give me the bread type. You can't do that. You have to go through the function to get it. Because we make those hidden. That's deterring encapsulation. Okay so encapsulating means you're hiding information they're private. Only way to get them is through a public function. Okay but this is public. And also it's static right. And so that means when you call it this way this is fine. It's no problem. But you could just go directly through the class. Which is the bread class. So this is okay. This is synonymous to just doing something like this. All right I'm calling the model because it's static. And it's public and the bread class. So you just do that. It's shorter. Whether I call this way or the other the other one I had earlier you're going to get the same information. And that is also why static is being used like that. It's shared by everybody anybody you want to use it. It does not belong to any particular object. Well I mean it's in the bread class but any data can access that. So we've got the model and then we'll have a dash some dashes down here. And the way I had was I put like here I say bread or sandwich. Like number one or something like that right. One something like that right. Now how do you how do I get one two and three. Well you could do it multi lines. Sys out and then here will be what did I have. I have really short memory sorry. So you have the bread type calories the filling per serving and then the total calories. And then everything gets repeated for this for the second bread. Okay second bread and it looks exactly the same. It just that the name is different and these values are different. So that means you have to call this function three times. Each time you call you're going to pass that sandwich. But when you call it's going to print this every time one one one one. You want to say one two three four right. So how do you do that. Okay and again a couple of ways. One way is you can use a variable and you pass a variable to this function. Along with the sandwich you can pass another variable here. And maybe like a counter. And then here you'll print the counter here. So we replace that with the counter. And then plus the rest of that string. So it will say sandwich one sandwich two and so forth. So we got that and then here we have the bread type. And plus the sandwich that get bread that get type. Okay and I'll go through that and I'll duplicate this a couple of times. This is the bread calories. Let me just put it like a calorie after you've put a slice. And then bread just get calories. All right and then here will be the sandwich filling type. You get the get sandwich filling type. And then you get the get filling type function. Here will be kind of same thing. I put here calories per filling. And get this information here. Get sandwich filling that get calories per filling. Okay and then one more down here. I'm going to get the total calories. This is for the sandwich okay. So we're going to add and get the information from where. From one of these functions and either one you want to use. It's fine. So we'll try the first one first. So that says calculate calories you pass in the sandwich. Right so that should return the total calories because it will be turned a total here. And if you call the other function then you just pass in the bread and sandwich. And since we don't have the bread and sandwich how do I do that? So I'm going to print here just to show you two ways okay. This is method one. This is the method two. And so this one here you have to pass two objects to it. So the first one is the bread. So you can say sandwich that get bread. Remember the get bread function returns the object the bread object. Which is the sandwich bread. The second is sandwich get sandwich filling. So you get those two objects passed to this function. And now they are satisfied. And you're going to get the same result down here. Okay so these are done. Now we need to go to the main method and start creating our objects. And so you want to instantiate three sandwiches. So we call it sandwich sandwich one. Get new sandwich. So we have three options right. As you can see actually two options. One is the empty one. And one is I think we have let me see. You have three options. That's what it's not showing but yeah we have no parameter. One has the bread and the other one. I mean you can always go back here and see it. We have we have three of them. The empty one one has the bread and sandwich object. And the one has the value for each of those. So you can choose any one you want. And the less the less of one you choose the more you have to add more data. The the more you choose you have to you can you know quickly create those in there. So if you choose the first one then you have to create each object because they were not created yet. Right so that means I'll do one of each so you can see. So this one here chooses this option. So that means I need to go and satisfy the bread and the filling. Object because it's not filled right nothing is set. So in this case I have to say a sandwich one. Okay I have to create a new sandwich new bread for that. So I can't really do this one yet. I have to say you know but bread and then let's call it bread one. It's new bread and we'll put the information here. So the type would be like rye and say this one has 50 calories. And then I can add that to my sandwich but I also need the filling. So sandwich filling filling one equals new sandwich filling. I can pass in again this is the say eggs and then calories would be 100. And so I have my two objects. Now these two objects can be passed to my sandwich now but I can't because I already created. So I have to use the sandwich one to set the bread. I pass in the bread one. And sandwich one that set filling. Pass in the filling one. Okay so all of these lines are used only to create this single sandwich bread. And for now we'll just leave that for now and if you want to print the result you can just say print result passing this sandwich one. And see what happens. And also the counter because we added a counter. So this is bread number one so you just put here one. Okay so if I save this now and run this program hopefully there are no errors. And see what it gives us. Okay so here we go we have the static data. Then we have the sandwich one. Right we have the bread type and the eggs and so on. So the calories you can see exactly the same whichever function you use. All right so that's the first method to do that. Now what about the second method? The second method is to call the second constructor. So here's sandwich two. Okay so sandwich and then sandwich two is new sandwich. This time I'm going to pass in the sandwich and then the bread filling. Okay oh not sandwich I mean bread. That's what it means. That means I have to create this again because I don't want you to say one. But I'm going to copy this too and I'll just put it right here. Right above here okay because you need to create those first. So this would be the number two. And we'll call this white bread 670 and then this is like salad. And then since salad's healthy we'll put here like 60. Okay so now I passed in the bread two and the filling two. So I created my second sandwich and I can print that out right here. And that's the sandwich number two and so I passed in the parameter two. Argument two. Okay so if I run it now I'm going to get two results. So here we go. The first one is what we had earlier the rye. The second and notice it says number two now right. And then it has this information and surprisingly enough at the same calories. And just to make sure this is true okay because remember we have to double the slice two times two. So you're going to get 40 out of that right. I mean 140. So 140 plus 60 you get 200. Same thing here. If I double that I get 100 and 100 plus 100. Wow interesting. Okay so that's the method number two. Now here's method number three. Sandwich three. So this time you're going to pass in this information directly to the constructor. So I would say sandwich sandwich three three is no sandwich. Pass in the first is for the bread. So let's call this what do we have right. Right so we'll call this the wheat. And this one has maybe like 25 calories per slice. And then the type would be a potato. And then this one has let's say 80 calories. Not 800. So you can see this much shorter I'm done. And then I just print the result. Print the result of sandwich three. And then the counter would be three. So there you go. So run. And as you can see I have my the first sandwich. The second and my third right. So you can see how your code get shorter and shorter. If you have these constructors here. And here because I didn't I have to pass in both objects. That means they have to be created first before you can actually pass them in here. This one is the most tedious one because I use the shortest one. But then I end up creating my new bread object. And then my filling object and they pass those objects into that. You set each of those individually. So you can see there's a lot more to do there. But either way either one of these will work. And of course you can always reflect on these. This is you can always reflect to make it a little bit shorter. Like this part here I can make it so that it will look something similar to this. Okay just to show you how that works. I can go here and use this sandwich same name. All right let's let's put sandwich four. Okay but I'm going to use something similar like this here. So where's the sandwich four is new sandwich. I'm going to pass in two objects. So instead of you know declaring them this way I could put this inside here. Inside here that's the new bread object. The second object is just this part. So there we go I have my fourth bread. And yeah it's a little bit longer inside here. But I need to instantiate a bread object. And then instantiate a new sandwich filling object. Yeah that's the way I can hide this information here and make it make my code really long but actually it's not this long. All right so the reason why you do this is because you don't want to reuse this again. So I'm not creating a variable here. So once I run line 2021 I'm done. Okay no more I can't access this again without going through the sandwich four object. These two will exist because I created using a variable. So once I pass those to this function here this constructor I can still reuse them again. And because they are the same object remember they are just the same memory address. I pass that to the sandwich this still exists. So if I'm making any changes outside here it's going to affect the bread the sandwich in here too. Here I can. Here once I instantiate that I'm done. It exists inside the sandwich only. I can't access anywhere outside unless I go through the sandwich four okay. So by doing this way you can see I have one line of code just kind of like this one here. And this one here yeah has a lot this is just the you can't get any shorter than that until um yeah because once you once you set that then you you can't I mean you can you can omit this right. So here's the same thing so when you set the bread I could do this. I could do the same thing like I did here okay. So instead of creating this bread here I just copy this. You can put it right in here bread one and then this sandwich here going to the sandwich too so in that way I don't need these two lines here. So it's the same thing right it's the same thing I'm just either if I want to reuse it I put in a variable if I don't then you can instantiate right inside the function as an argument it will pass that as an object to the constructor or to the function here. And you'll see that that should be completely okay and I can print here the bread number sandwich number four. So sandwich number four and four and two are the same okay because I'm using the same white here but I can just make some different calories say that this is I don't know chocolate is there a chocolate bread I think so right so it's really high calories okay so here we go and run okay so here we go the first one as you can see even though I changed my code right everything's fine this second one is normal the white bread the third is the wheat and then the fourth one is the chocolate one okay so there you go and you might want to you know separate these with a with another line it's kind of how to read and so when you do the output here the variable above you can put this out and then just leave it a blank line like this that will give you an empty line so you can see that looked a little bit nicer has a little separation here so you can see you know much better this way okay so I showed you like how you can use this in many ways when you create objects this way and so that's that's really important and if you have any questions let me know I know I probably did more than I should have but I do want to show you the you know flexibility of doing all this code because there's really no single way to do it okay I just did everything here in the main method here but of course you can always reflect on your classes to do more than we would just had here but this will do all right well let me know if you have questions