# 4.3 Addition/Subtraction Program Part 2

Video Activity
Join over 3 million cybersecurity professionals advancing their career
or Time
4 hours 19 minutes
Difficulty
Beginner
CEU/CPE
4
Video Transcription
00:00
Hi and welcome back to part two of this lab. Now we left off by storing which operation our user would like to perform by specifying a number one or two inside the Operation Variable, which is of the type integer. And we're doing that by saying user input dot next int
00:19
and user input in this case is the name of
00:22
our scanner. And now we'd like to make some decisions and actually use our methods that we created up here. In order to do that, we will take advantage of
00:34
if and else if statements. So I'm gonna first go ahead and say, Let's say if the user enters in the number one for our operation variable So if operation equals one,
00:47
then we'd like to do a set of certain things first. I'd like to ask them to enter the first number, the first number that we will be using to compute our result or calculation of the ad method. So I could say something like system that out print line and then
01:07
enter
01:07
first and
01:10
enter the first number
01:12
01:15
and then I'd like to store that first number. And for that if you remember, we created user number one, which is also an integer or a variable of the type int. And I'd like to set user name one as user input dot accent
01:32
which will get the next indigent for me.
01:34
Then I'd like to again asked the user for this time the second numbers. So system the outdoor print line. Enter the second number,
01:45
second number
01:47
colon. And I'd also like to store that number inside user number two
01:53
so that I will do by typing user known to equals Cesar input dot next int
02:00
which will get me the next integer after we after the user enters the 1st 1
02:06
Now, obviously, we want to do something with these two integers and that is why we have these methods.
02:12
But in order to store the result of
02:16
thes methods, we need to have a variable which we created over here. And I called it result to resemble what we're trying to do here. So I could store the result of
02:27
my addition operation in the result variable by
02:30
us by right by calling the
02:34
ad method and then putting it inside results. So
02:37
this is how you call a method you would first type the methods name.
02:40
And then here it's asking you to put in an integer and another integer, and it's referring to into accent into Why? To represent the parameters that we have up here
02:52
now, In our case, those X and Y integers are easier numb, which you could think of as X and then use user number two. So using someone as X and using them to as why
03:05
so user
03:07
numb one and then user number two.
03:10
All right, Perfect. So now what this is doing is we're calling our method to take a user name one and user number two and do some sort of calculation, which in this case is addition and store that in our result variable.
03:24
Now, once we have our addition operations results stored, we obviously want to do something with it. So for that, I'm gonna print out the result so systems are out of print line. The result
03:38
The result is colon
03:40
plus results.
03:43
Now, what that will do is it'll print out our results to the council.
03:47
Now that is just for, um, the first operation, which is addition. Let's say our user enters in here she enters to which would resemble this attraction operation. For that, I would do something like else if operation equals equals two.
04:04
So if the operation that they want to a compute happens to be a subtraction, which is corresponding to the number two,
04:15
then I would like to do a set of things, and that is essentially what we did up here with the addition operation so I can take it
04:24
and copy thes lines of code paste them. But instead of having adhere, let's switch that to subtract.
04:33
Perfect.
04:34
Now, in order to test our program, we need to first run it, and we can run it by either going up here or right clicking or right clicking on our file name and selecting a run. So I'm gonna go ahead and first do that.
04:51
Now it takes a few seconds for it to compile. All right, perfect. So here it says, please enter which operation you want to perform. One for addition or two First attraction. Let's first go with addition,
05:04
so let's enter our first number. Let's make it 10 and our second number. Let's make that five.
05:12
The result is 50. Perfect. Let's try that for subtraction. So let's enter number two.
05:18
First number is 15 on dhe. Second number is three
05:24
perfect. The result is 12. Now, as I mentioned before, we're not dealing with negative cases just yet. The entire goal of this program is to give you a basic and general idea, a simple idea of how you could use an implement something like
05:40
methods and make use of them in your program by calling them whenever you need
05:45
to use them
05:46
and going back to our
05:47
video a lecture and back to our slides, we discussed how to implement and create methods by coating and math program in this lab and in the falling video, we will be discussing method overriding.
06:02
And I hope you guys understood what we went over during this lab. And if you have any questions at all, feel free to contact 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 video lecture.
Up Next