Help with program

M

mbuenik5

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?
 
T

Tsukino Usagi

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

Daniel Pitts

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

markspace

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

markspace

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

glen herrmannsfeldt

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
 
S

Stefan Ram

Patricia Shanahan said:
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])?
 
A

Arne Vajhøj

Patricia Shanahan said:
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
 
D

Daniel Pitts

Patricia Shanahan said:
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);
}
 
R

Roedy Green

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

Daniel Pitts

On 5/1/2012 5:06 PM, Daniel Pitts wrote:
...

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[]".
 
A

Arne Vajhøj

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
 
D

Daniel Pitts

On 5/1/2012 4:21 PM, Stefan Ram wrote:
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.
 
Joined
May 3, 2012
Messages
3
Reaction score
0
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
    }
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top