Help with program

Discussion in 'Java' started by mbuenik5@yahoo.com, May 1, 2012.

  1. Guest

    I need help writing a Java method named swap that takes an integer
    array and two indexes. The method then swaps the integers at those two
    indexes. The return type for swap is void. Can anybody write this out?
    , May 1, 2012
    #1
    1. Advertising

  2. On 5/2/2012 2:31 AM, wrote:
    > I need help writing a Java method named swap that takes an integer
    > array and two indexes. The method then swaps the integers at those two
    > indexes. The return type for swap is void. Can anybody write this out?


    I tutor students in Java for $20 per homework problem.
    Tsukino Usagi, May 1, 2012
    #2
    1. Advertising

  3. Daniel Pitts Guest

    On 5/1/12 10:31 AM, wrote:
    > I need help writing a Java method named swap that takes an integer
    > array and two indexes. The method then swaps the integers at those two
    > indexes. The return type for swap is void. Can anybody write this out?


    Yes, I can, but you should, since it will help you understand Java better.

    We in this forum will gladly help you understand problems that you
    encounter along the way. However, we know that we are not actually
    helping you by doing your homework.

    Try it on your own, if/when you encounter difficulty, show us what
    you've done *and* any error message you received we'll help you
    understand why those error messages occur, and what to do about them.

    Best of luck in your endeavor,
    Daniel.
    Daniel Pitts, May 1, 2012
    #3
  4. markspace Guest

    On 5/1/2012 10:31 AM, wrote:
    > I need help writing a Java method named swap that takes an integer
    > array and two indexes. The method then swaps the integers at those two
    > indexes. The return type for swap is void. Can anybody write this out?



    Do your own homework kid. You might learn something.

    Seriously, if you post the code you wrote here and want some help
    debugging, that's fine. But I'm not going to do this for you.

    Also, what's with all the obvious homework questions lately? It's
    almost like it's the end of the semester and all the procrastinator
    suddenly realized their assignments were due.
    markspace, May 1, 2012
    #4
  5. markspace Guest

    On 5/1/2012 11:15 AM, Tsukino Usagi wrote:
    > On 5/2/2012 2:31 AM, wrote:
    >> I need help writing a Java method named swap that takes an integer
    >> array and two indexes. The method then swaps the integers at those two
    >> indexes. The return type for swap is void. Can anybody write this out?

    >
    > I tutor students in Java for $20 per homework problem.



    Your rate is too cheap. A family friend is a teacher (professional,
    licensed) for elementary school tutors students on the side for I think
    $125 per hour (usually as once per week for one hour). That's for ages
    8 to 11 or so math and English. For college level course work, I think
    it should be more.
    markspace, May 1, 2012
    #5
  6. Patricia Shanahan <> wrote:
    > On 5/1/2012 10:31 AM, wrote:
    >> I need help writing a Java method named swap that takes an integer
    >> array and two indexes. The method then swaps the integers at those two
    >> indexes. The return type for swap is void. Can anybody write this out?


    > I'm sure any who knows Java beyond the extreme beginner level can write
    > it out. Have you considered the possibility that the objective is to get
    > you to work out how to do it yourself? That seems likely to me.


    It is usual that you can get help if you have actually tried,
    and show what you have and haven't got working.

    For extra challenge, try doing it without any temporary/local variables.

    -- glen
    glen herrmannsfeldt, May 1, 2012
    #6
  7. Stefan Ram Guest

    Patricia Shanahan <> writes:
    >>For extra challenge, try doing it without any temporary/local variables.

    >I strongly recommend against this, in favor of doing it as simply,
    >cleanly and directly as possible.


    What could be more clean and direct than

    java.util.Collections.swap( java.util.Arrays.asList( a ), i, j )

    (assuming »a« has base type Integer[ as given in the OP])?
    Stefan Ram, May 1, 2012
    #7
  8. Arne Vajhøj Guest

    On 5/1/2012 4:21 PM, Stefan Ram wrote:
    > Patricia Shanahan<> writes:
    >>> For extra challenge, try doing it without any temporary/local variables.

    >> I strongly recommend against this, in favor of doing it as simply,
    >> cleanly and directly as possible.

    >
    > What could be more clean and direct than
    >
    > java.util.Collections.swap( java.util.Arrays.asList( a ), i, j )
    >
    > (assuming »a« has base type Integer[ as given in the OP])?


    It would be cleaner with an import!

    :)

    Arne
    Arne Vajhøj, May 2, 2012
    #8
  9. Daniel Pitts Guest

    On 5/1/12 4:51 PM, Arne Vajhøj wrote:
    > On 5/1/2012 4:21 PM, Stefan Ram wrote:
    >> Patricia Shanahan<> writes:
    >>>> For extra challenge, try doing it without any temporary/local
    >>>> variables.
    >>> I strongly recommend against this, in favor of doing it as simply,
    >>> cleanly and directly as possible.

    >>
    >> What could be more clean and direct than
    >>
    >> java.util.Collections.swap( java.util.Arrays.asList( a ), i, j )
    >>
    >> (assuming »a« has base type Integer[ as given in the OP])?

    >
    > It would be cleaner with an import!
    >
    > :)
    >
    > Arne
    >


    static import java.util.Collections.swap;
    static import java.util.Arrays.asList;

    (boilerplate code here, that OP must attempt himself)
    {
    swap(asList(a), i, j);
    }
    Daniel Pitts, May 2, 2012
    #9
  10. Roedy Green Guest

    On Tue, 1 May 2012 10:31:36 -0700 (PDT), wrote,
    quoted or indirectly quoted someone who said :

    >I need help writing a Java method named swap that takes an integer
    >array and two indexes. The method then swaps the integers at those two
    >indexes. The return type for swap is void. Can anybody write this out?


    This is clearly an artificial problem. The idea is if you solve it,
    your skill will be enhanced. If somebody else does it, their skill
    might be enhanced, if it is not already beyond this level. It won't do
    anything for your skill. It has no practical use.

    We want you to become a skilled programmer. It is not that we are
    being mean.

    Some generic notes on swapping may be useful though. Think of them an
    hints or encouragement to get you started.
    see http://mindprod.com/jgloss/swap.html

    It you are stuck, see these tricks for getting unstuck.
    http://mindprod.com/jgloss/tackling.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Programmers love to create simplified replacements for HTML.
    They forget that the simplest language is the one you
    already know. They also forget that their simple little
    markup language will bit by bit become even more convoluted
    and complicated than HTML because of the unplanned way it grows.
    ..
    Roedy Green, May 2, 2012
    #10
  11. Daniel Pitts Guest

    On 5/1/12 6:08 PM, Patricia Shanahan wrote:
    > On 5/1/2012 5:06 PM, Daniel Pitts wrote:
    > ...
    >> static import java.util.Collections.swap;
    >> static import java.util.Arrays.asList;
    >>
    >> (boilerplate code here, that OP must attempt himself)
    >> {
    >> swap(asList(a), i, j);
    >> }
    >>

    >
    > Hmmm. I'm looking forward to the O(n**3) bubble sort.
    >
    > Patricia


    Arrays.asList is actually a O(1) operation, as is Collections.swap, so
    it will still only be O(n**2)

    Although, none of this works if by "integer array", the OP meant "int[]".
    Daniel Pitts, May 2, 2012
    #11
  12. Arne Vajhøj Guest

    On 5/1/2012 8:06 PM, Daniel Pitts wrote:
    > On 5/1/12 4:51 PM, Arne Vajhøj wrote:
    >> On 5/1/2012 4:21 PM, Stefan Ram wrote:
    >>> Patricia Shanahan<> writes:
    >>>>> For extra challenge, try doing it without any temporary/local
    >>>>> variables.
    >>>> I strongly recommend against this, in favor of doing it as simply,
    >>>> cleanly and directly as possible.
    >>>
    >>> What could be more clean and direct than
    >>>
    >>> java.util.Collections.swap( java.util.Arrays.asList( a ), i, j )
    >>>
    >>> (assuming »a« has base type Integer[ as given in the OP])?

    >>
    >> It would be cleaner with an import!
    >>
    >> :)


    > static import java.util.Collections.swap;
    > static import java.util.Arrays.asList;
    >
    > (boilerplate code here, that OP must attempt himself)
    > {
    > swap(asList(a), i, j);
    > }


    I may just have used the traditional import, but ...

    Arne
    Arne Vajhøj, May 2, 2012
    #12
  13. Daniel Pitts Guest

    On 5/2/12 1:25 PM, Arne Vajhøj wrote:
    > On 5/1/2012 8:06 PM, Daniel Pitts wrote:
    >> On 5/1/12 4:51 PM, Arne Vajhøj wrote:
    >>> On 5/1/2012 4:21 PM, Stefan Ram wrote:
    >>>> Patricia Shanahan<> writes:
    >>>>>> For extra challenge, try doing it without any temporary/local
    >>>>>> variables.
    >>>>> I strongly recommend against this, in favor of doing it as simply,
    >>>>> cleanly and directly as possible.
    >>>>
    >>>> What could be more clean and direct than
    >>>>
    >>>> java.util.Collections.swap( java.util.Arrays.asList( a ), i, j )
    >>>>
    >>>> (assuming »a« has base type Integer[ as given in the OP])?
    >>>
    >>> It would be cleaner with an import!
    >>>
    >>> :)

    >
    >> static import java.util.Collections.swap;
    >> static import java.util.Arrays.asList;
    >>
    >> (boilerplate code here, that OP must attempt himself)
    >> {
    >> swap(asList(a), i, j);
    >> }

    >
    > I may just have used the traditional import, but ...
    >
    > Arne
    >
    >

    Indeed, I was intentionally obfuscating it so that any decent instructor
    would know this was plagiarized.
    Daniel Pitts, May 2, 2012
    #13
  14. vishnudhoodhan

    Joined:
    May 3, 2012
    Messages:
    3
    Hai hope this will help you


    Code:
    public class swap {
        
        public static void main(String args[])
        {
            Integer a[] = new Integer[2];
            a[0] = 5;
            a[1] = 10;
            System.out.println("a[0] = " + a[0] + "\t a[1] = " + a[1] );
            swap(a);
            System.out.println("a[0] = " + a[0] + "\t a[1] = " + a[1] );
        }
        
        public static void swap(Integer a[])
        {
            int x;
            x = a[0];
            a[0]= a[1];
            a[1] = x ; //swapped
        }
    }
    vishnudhoodhan, May 3, 2012
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pipi
    Replies:
    1
    Views:
    613
    Joseph Millar
    Jul 21, 2003
  2. Rey
    Replies:
    4
    Views:
    10,914
    Roedy Green
    Dec 12, 2003
  3. ola
    Replies:
    3
    Views:
    702
    Marco Schmidt
    Feb 16, 2004
  4. Replies:
    1
    Views:
    462
    Walter Roberson
    Aug 12, 2005
  5. Kris Stark
    Replies:
    1
    Views:
    89
    John Bokma
    Sep 8, 2005
Loading...

Share This Page