00:02
All right, in this question, we are going to use functions to compute the sum and difference of two numbers entered by the user.
00:08
So i've already created a java program, and i have the main class and the main method.
00:14
And so we'll start out here by creating a scanner object, which is what we need to get user input.
00:19
So we'll say scanner input equals new, scanner system .n, which allows the user to type in input.
00:29
Note that to use the scanner objects, you do need to import java .util .scanner at the top.
00:35
Okay.
00:36
Now that we've created the scanner, we can use it to ask the user to enter numbers.
00:41
So we'll system dot out dot print line and we'll tell the user to please enter two numbers.
00:55
And then we will have to create some variables to get those numbers.
00:59
So it doesn't matter what you call the numbers.
01:00
We'll call them a and b.
01:03
Now, if you want the numbers to hold decimal points, we'll use a double, but you could use an int if you don't want a decimal point.
01:11
And so we'll say double a equals input.
01:14
That's our scanner.
01:15
Dot next double.
01:18
And that will basically pause and wait for the user to type in a number and press enter.
01:23
And then we'll do the same thing with our second number.
01:25
Double b equals input.
01:28
Again, that's our scanner.
01:30
Dot next double, and now it will wait for the user to type a second number in.
01:36
At this point, we want to compute the sum and the difference of a and b, and we're going to use a function to do that.
01:42
So we'll start out by calling the sum function.
01:45
And the way we call a function, now keep in mind, we have not created this function yet, so it will show an error, but we are going to use the name of the function, and then in parentheses, we are going to send it the values to compute with.
01:58
In this case, we want it to take the numbers a and b and add them together and display the results.
02:04
But again, because we haven't created this function yet, we see an error.
02:08
So down below the main method, so down here, we'll create our function.
02:14
So the way we're going to do this is we are going to make this a void.
02:19
And void basically means that the function is not going to return a specific answer.
02:25
And the reason it's not going to return a specific answer is that the problem specifies that it should display or print the answer.
02:33
So we'll say void sum, and then in parentheses, two parameters.
02:39
Now it doesn't matter what we call these parameters...