The Student Room Group

Computer Science help

I'm having an issue while programming in java

I have to make a calculator that operates on two values and each values has to be in a different method
Original post by Carrying a torch
I'm having an issue while programming in java

I have to make a calculator that operates on two values and each values has to be in a different method

And what is the issue?
Original post by AcseI
And what is the issue?

I have two methods and each one separately gives input values stored in separate variables. So when I make a third method and try to do simple arithmetics using the input values it doesn't work because the variables aren't local.

I was told that I had to somehow pass an argument and use parameters. The thing is, I'm not able to link each input value variables to variables or values in any other method.
(edited 3 years ago)
Original post by Carrying a torch
I have two methods and each one separately gives input values stored in separate variables. So when I make a third method and try to do simple arithmetics using the input values it doesn't work because the variables aren't local.

I was told that I had to somehow pass an argument and use parameters. The thing is, I'm not able to link each input value variables to variables or values in any other method.

So I'm not a Java person, and troubleshooting code is basically impossible without seeing the code.

In general terms, each number would be stored in it's own method as a variable and you'd return that number to the calling method. So:
method_1 generates a number and returns it
method_2 generates a number and returns it

Then method_3, the one where you actually do the calculation, would call each of those methods. You could store those numbers in variables, and then perform an operation (in this example addition):
number_1 = method_1()
number_2 = method_2()
total = number_1 + number_2

It's a slightly odd task though, as by itself this isn't something that actually requires 3 different methods. It's difficult to offer more help without knowing the exact brief you've been given.

Quick Reply

Latest

Trending

Trending