# Thread: [SOLVED] Simple Calculations in Java

1. Member
Join Date
Mar 2009
Posts
62
Rep Power
0

## [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! :-)

2. ## 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

3. Member
Join Date
Mar 2009
Posts
62
Rep Power
0
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.

4. Member
Join Date
Mar 2009
Posts
62
Rep Power
0
PS - Do I need to add another int variable as the result?

5. You need to go here Lesson: Language Basics (The Java Tutorials)

As a quick reference, the binary integer operators are:
Java Code:
`+ - * / %`
Last edited by OrangeDog; 05-19-2009 at 02:57 AM.

6. Member
Join Date
Mar 2009
Posts
62
Rep Power
0
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

7. 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

Java Code:
```public int maths(int arg1, int arg2) {
int intermediate = arg1 * arg2;
intermediate += (int)Math.PI - 1;
return (intermediate % 7) + (arg1 / -arg2);
}```

8. Member
Join Date
Mar 2009
Posts
62
Rep Power
0
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.

9. 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.

10. Member
Join Date
Mar 2009
Posts
62
Rep Power
0
Thanks OrangeDog - I will read up tomorrow!

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•