# [SOLVED] Simple Calculations in Java

• 05-19-2009, 01:03 AM
fullmetaljacket
[SOLVED] Simple Calculations in Java
Hey All,

I'm after some simple advice with a java problem.

I need to be able to add two int arguments together in order to produce a result using a method. Say I need to add 9 + 9 = 18, I also need to be able to take the result 18 and use the 1 for something and also the 8 for something. How do I spilt 18 into the number 1 and number 8 so I can set something using these numbers.

The method should take the two arguments and return no result.

Code so far:

public void add(int number 1, int number2)

Yep.. I know this is nothing really!

Any help would be greatly appreciated! :-)
• 05-19-2009, 01:45 AM
CJSLMAN
What if...
Question: will you always have just two digits as a result? What happens if:

number1 = 58
number2 = 43
Result = 101

As for separating them:

result/10 = tens
result%10 = units

Luck,
CJSL
• 05-19-2009, 01:51 AM
fullmetaljacket
Hey CJSLMAN,

To answer your question - no. Its only ever 9 + 9 or 9 x 9 so only two digits as a result.

for example:

public void add(int number1, int number2)

or

{
int number1;
int number2;

After this I am stumped! How do I actually add the int variables together and then split the result. I see that result/10 = tens but what would I need to write in the method to do this.

Please, any help would be greatly appreciated.

Cheers - FMJ.
• 05-19-2009, 01:51 AM
fullmetaljacket
PS - Do I need to add another int variable as the result?
• 05-19-2009, 01:54 AM
OrangeDog
You need to go here Lesson: Language Basics (The Java Tutorials)

As a quick reference, the binary integer operators are:
Code:

`+ - * / %`
• 05-19-2009, 02:07 AM
fullmetaljacket
OK - I'm using a version of BlueJ and if I type into the workspace.

int number 1;
int number 2;
int result;
number1 = 1;
number2 = 8;
result = number1 + number2;
result / 10;

The answers are correct. The issue is how do I get this into the method body so it works?

I have tried:

public void add(int number1, int number2);
{
int result;
result = number1 + number2;

I'm sure the part above is incorrect.

then how do I take result/10 from this?

Cheers - FMJ
• 05-19-2009, 02:11 AM
OrangeDog
Seriously, you need to take the time to read though those tutorials in order to learn the most basic points of Java.

And here's a bonus example

Code:

```public int maths(int arg1, int arg2) {     int intermediate = arg1 * arg2;     intermediate += (int)Math.PI - 1;     return (intermediate % 7) + (arg1 / -arg2); }```
• 05-19-2009, 02:49 AM
fullmetaljacket
Hey Guys,

Thanks for the help. I've got the code: This seems to work, but possibly not the most efficient method in the world.

public void add(int number1, int number2)
{
int result;
result = number1 + number2;
}

Final question - If I was to make this method private e.g

private void add(int number1, int number2)
{
int result;
result = number1 + number2;
}

Why does it no longer work? What else would I need to do?

Cheers - FMJ.
• 05-19-2009, 03:03 AM
OrangeDog
There's no more computationally efficient way of doing it.

Your method doesn't actually "work" in any meaningful way, as the answers go "out of scope" and cannot be accessed after the method is finished. The method should either return something, or (as it seems in this case) update some instance variables or fields.

More reading is required. Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects) in particular, but also all of the "Learning the Java Language" section.
• 05-19-2009, 03:19 AM
fullmetaljacket
Thanks OrangeDog - I will read up tomorrow!

All the best, and once again thank you for your time and help! :-)