4 hours 19 minutes
Hi and welcome back to the course. I hope you guys doing well. And if you remember in the last video, we mentioned that in this lab we will be going over the concept of method overloading, and we will be making a program which will demonstrate how method overloading works.
Now, as always, I've listed out the main learning objective of this lab, which is to perform the implementation of method overloading by creating a program with some methods that perform certain calculations such as multiplication and addition.
Now, at this point, I'd like you to go ahead and switch over to intelligence.
And in intelligence, I made a class called Math. Now you can do the same, but going thio file new
and class, in case you have forgotten how to do that and then type in whatever you want and go ahead and click. Okay.
All right. So since I already have my class, all that you take a few seconds and do that
once you have your class set up,
I've added three comments which will essentially guide us through the creation of our program.
Now, the first thing that we will do is making method that performed. It's the multiplication
of two integers.
Now that should be as simple as it is said. We will go ahead and make a public method that is static,
And what is it going to return? It is going to return an integer
and let's give it a Nate. Let's name it calculate
Now, since we are performing at the multiplication of two integers, we need to pass in two integers into this method. And I'm going to do that by having the first end as X and the second teacher as into why
Then we're gonna have our two curly places which represent the body off our method. Now you can see this red sort of,
uh, warning over here, which says missing return statement. So once we put in our return statement, what exactly are we returning?
As mentioned a few seconds ago, we're returning the multiplication of two integers. Let's go ahead and do that. In this case are two integers are ex
and then the asterisks signed for multiplication. And then why were they say Michael
Perfect. So that is our first method?
what we want to do now is use the same method name as this one here, but create a new method that performs the addition of three doubles.
Now, let's go ahead and also do that. So I'm gonna make the method public static,
and it's going to return a double
and let's call it calculate.
And then I'm gonna pass in three doubles. So double
with our body of our method
Now, what are returning were returning the addition of three doubles, so that would be X plus y plus z
perfect. So now that we have our two methods,
we need to be able to display the results
so that, uh, I'm gonna do by using our main method or standard main method that we have the option to implement in our classes. So we will do that by saying
Air gs for arguments to curly braces for a body of remain method,
an insider main method. What do we want to do? You want to print something to this terminal or console over here?
So then I'm gonna have a print statement for that. And inside the print statement, let's say something like the result of
plus, and then we're gonna
call our first method by
writing its name. So as you can see here, it's giving us two options. Two options for the calculate method.
The 1st 1 is three Method that returns it into in the 2nd 1 is a method that returns a double. So I'm gonna go ahead with the 1st 1 and let's pass into random numbers for into
And then let's go ahead and copy this print statement.
All right, Perfect. Copied it. And no,
paste it into here. But instead of using our first calculate method,
let's use our 2nd 1
And in here.
Um, Actually, before we do that, let's make sure we changes to the result of the second method is and then in here. Let's pass in 4.5
Now, in order to see the result of our program, let's go ahead and run our program.
All right? So let's give it a few seconds to
build and execute everything
perfect. So as you can see here, it says the result of the first method is eight. The result of the second method is 8.5. Now, that demonstrates how method overloading works because we have
a different method signature for both methods. But we do have the same name. One returns an intruder and the other returns a double.
All right, So if we go back to our video lecture to summarize what we did in this lab, we went over the implementation of method overloading. And I hope you guys understood how that works at a high level and that it's clear.
So coming up next, we're gonna be starting module five, which is just gonna be a quick, short introduction to what object oriented programming is, or also known as old people.
Now, as always as always, if you have any questions at all, feel free to connect with me via Lincoln. And I would be more than happy to answer any of your questions. Thank you very much for watching. And I look forward to seeing you in the next module
Sun Certified Java Programmer
Sun Certified Java Programmer practice exam will help to prepare for the Oracle Certified Associate ...