Results 1 to 12 of 12
  1. #1
    yadster101 is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Question New to CompSci and Java

    Hi i just started my CompSci class and for hw my teacher gave us the question:

    "Write an expression that, given a positive integer n, computes a new integer
    in which the units and tens digits have swapped places. For example, if
    n = 123, the result should be 132; if n = 3, the tens digit is zero and the result should be 30."

    Im not really sure how i would do this but my guess is that it is more of a math question than a java question. If you have any ideas that might help please let me know.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    There are two ways to approach this question.
    A) Convert the int into a String and manipulate the String chars using the String method charAt(int i).
    B) Mathematically isolate your tens and ones digits and all the digits from the hundreds on up. This is probably what your teacher wants you to do. To do this, look up "integer division" and also the modulus operator. HTH.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Here is a the solution in the first way that Fubarable explain,

    Java Code:
            Scanner scn = new Scanner(System.in);
            String result = null;
            
            System.out.println("Enter the value: ");
            String str = scn.nextLine();
            
            if(str.length() == 1)
                result = str + "0";
            else 
                result = str.substring(0, (str.length() - 2)) + 
                        str.charAt(str.length() - 1) + str.charAt(str.length() - 2);

  4. #4
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Smile

    And here is the other way:

    Java Code:
        public int swapTensAndUnits(int n) {
            int tens = (n/10)%10;
            int units = n%10;
            int rest = n-n%100;
            return rest+units*10+tens;
        }

  5. #5
    ianjedi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by yadster101 View Post

    Hi i just started my CompSci class and for hw my
    Im not really sure how i would do this but my guess is that it is more of a math question than a java question. If you have any ideas that might help please let me know.
    I would always have a go first, cause its going to get harder you need the basics. Good luck in your studies.

    cheers

    Ian J.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by ianjedi View Post
    I would always have a go first, cause its going to get harder you need the basics. Good luck in your studies.
    AMEN, Brother!

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by ianjedi View Post
    I would always have a go first, cause its going to get harder you need the basics. Good luck in your studies.

    cheers

    Ian J.
    Yep, nice thoughts! Thanks.

  8. #8
    gnarly hogie is offline Member
    Join Date
    Oct 2008
    Posts
    34
    Rep Power
    0

    Default

    Heh, that question totally blew my mind, you guys thought of the answer pretty quickly, do questions like that get easier over time?

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  10. #10
    gnarly hogie is offline Member
    Join Date
    Oct 2008
    Posts
    34
    Rep Power
    0

    Default

    We'll I was pretty lost as that guy about the question because that isn't a normal math question and you have to think differently then normally does it get easier. I don't know if I can explain it any easier, maybe it's a dumb question?

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    If you are looking in programing way, post #3 have the solution. Or else if you are looking on mathematical way, post #4 is the solution.

    I don't know what you mean by normal math question. There are no single pattern in maths. In different applications it takes in different ways. Quite similar thing is done in image processing, when converting an image into black and white, and many more usages are there.

  12. #12
    gnarly hogie is offline Member
    Join Date
    Oct 2008
    Posts
    34
    Rep Power
    0

    Default

    Well what I meant is that when you switch the tens with the ones normally you would think just switch the places but that program for post 4 is not what you would normally think to do just to switch the integer.

Similar Threads

  1. CompSci 1 Novice, need some help
    By annoyingzhang in forum New To Java
    Replies: 9
    Last Post: 10-12-2008, 09:02 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
  •