Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default [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. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Hey CJSLMAN,

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

    Can you please help - I am really confused with how to initialise the variables.

    for example:

    public void add(int number1, int number2)

    or

    public void add()
    {
    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. #4
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    PS - Do I need to add another int variable as the result?

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  6. #6
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    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. #7
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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);
    }
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  8. #8
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    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;
    int answer1;
    int answer2;
    result = number1 + number2;
    answer1 = result / 10;
    answer2 = result % 10;
    }

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

    private void add(int number1, int number2)
    {
    int result;
    int answer1;
    int answer2;
    result = number1 + number2;
    answer1 = result / 10;
    answer2 = result % 10;
    }

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

    Cheers - FMJ.

  9. #9
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  10. #10
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Thanks OrangeDog - I will read up tomorrow!

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

Similar Threads

  1. please help in java code (simple)
    By rahul_swe23 in forum New To Java
    Replies: 4
    Last Post: 01-26-2009, 04:32 PM
  2. decimal calculations?
    By arnab321 in forum CLDC and MIDP
    Replies: 5
    Last Post: 11-19-2008, 04:36 AM
  3. Simple java
    By abhiN in forum New To Java
    Replies: 0
    Last Post: 01-16-2008, 03:44 PM
  4. Simple java
    By abhiN in forum New To Java
    Replies: 1
    Last Post: 01-16-2008, 11:13 AM
  5. help with simple program in java
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 09:03 PM

Posting Permissions

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