4.3 Addition/Subtraction Program Part 2
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
4 hours 19 minutes
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
and user input in this case is the name of
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
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,
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
enter the first number
and your first
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
which will get the next indigent for me.
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,
colon. And I'd also like to store that number inside user number two
so that I will do by typing user known to equals Cesar input dot next int
which will get me the next integer after we after the user enters the 1st 1
Now, obviously, we want to do something with these two integers and that is why we have these methods.
But in order to store the result of
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
my addition operation in the result variable by
us by right by calling the
ad method and then putting it inside results. So
this is how you call a method you would first type the methods name.
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
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
numb one and then user number two.
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.
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
The result is colon
Now, what that will do is it'll print out our results to the council.
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.
So if the operation that they want to a compute happens to be a subtraction, which is corresponding to the number two,
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
and copy thes lines of code paste them. But instead of having adhere, let's switch that to subtract.
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.
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,
so let's enter our first number. Let's make it 10 and our second number. Let's make that five.
The result is 50. Perfect. Let's try that for subtraction. So let's enter number two.
First number is 15 on dhe. Second number is three
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
methods and make use of them in your program by calling them whenever you need
to use them
and going back to our
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.
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.