4.7 Math Overload

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Difficulty
Beginner
Video Transcription
00:00
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.
00:17
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.
00:35
Now, at this point, I'd like you to go ahead and switch over to intelligence.
00:39
And in intelligence, I made a class called Math. Now you can do the same, but going thio file new
00:49
and class, in case you have forgotten how to do that and then type in whatever you want and go ahead and click. Okay.
00:55
All right. So since I already have my class, all that you take a few seconds and do that
01:00
once you have your class set up,
01:03
I've added three comments which will essentially guide us through the creation of our program.
01:11
Now, the first thing that we will do is making method that performed. It's the multiplication
01:18
of two integers.
01:21
Now that should be as simple as it is said. We will go ahead and make a public method that is static,
01:30
so public
01:33
static.
01:36
And what is it going to return? It is going to return an integer
01:41
and let's give it a Nate. Let's name it calculate
01:45
Kill
01:46
que late.
01:48
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
02:05
Then we're gonna have our two curly places which represent the body off our method. Now you can see this red sort of,
02:14
uh, warning over here, which says missing return statement. So once we put in our return statement, what exactly are we returning?
02:23
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
02:31
and then the asterisks signed for multiplication. And then why were they say Michael
02:38
Perfect. So that is our first method?
02:42
No,
02:43
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.
02:54
Now, let's go ahead and also do that. So I'm gonna make the method public static,
03:01
and it's going to return a double
03:05
and let's call it calculate.
03:07
And then I'm gonna pass in three doubles. So double
03:14
X double
03:15
why?
03:16
And double
03:17
said
03:19
with our body of our method
03:21
Now, what are returning were returning the addition of three doubles, so that would be X plus y plus z
03:30
perfect. So now that we have our two methods,
03:34
we need to be able to display the results
03:38
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
03:50
public static
03:53
avoid
03:57
main
03:59
string.
04:00
Okay,
04:02
Air gs for arguments to curly braces for a body of remain method,
04:06
an insider main method. What do we want to do? You want to print something to this terminal or console over here?
04:15
So then I'm gonna have a print statement for that. And inside the print statement, let's say something like the result of
04:26
the first
04:28
method
04:30
is
04:31
plus, and then we're gonna
04:33
call our first method by
04:35
writing its name. So as you can see here, it's giving us two options. Two options for the calculate method.
04:43
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
04:56
And then let's go ahead and copy this print statement.
05:00
Um, okay,
05:03
so
05:04
one second.
05:06
All right, Perfect. Copied it. And no,
05:10
uh, that's
05:12
paste it into here. But instead of using our first calculate method,
05:17
let's use our 2nd 1
05:19
And in here.
05:20
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
05:30
ah, 2.5,
05:32
1.5.
05:35
Okay.
05:35
Now, in order to see the result of our program, let's go ahead and run our program.
05:45
All right? So let's give it a few seconds to
05:47
build and execute everything
05:50
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
06:03
a different method signature for both methods. But we do have the same name. One returns an intruder and the other returns a double.
06:14
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.
06:29
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.
06:40
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
Up Next
5.1 Intro to OOP
6.1 Intro to Encapsulation
6.2 Student Profile
7.1 Intro Abstraction
7.2 Bank Account Part 1