Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #14086 > unrolled thread

Help with program

Started bymbuenik5@yahoo.com
First post2012-05-01 10:31 -0700
Last post2012-05-01 18:15 -0700
Articles 18 — 10 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Help with program mbuenik5@yahoo.com - 2012-05-01 10:31 -0700
    Re: Help with program Patricia Shanahan <pats@acm.org> - 2012-05-01 10:53 -0700
      Re: Help with program glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-05-01 19:12 +0000
        Re: Help with program Patricia Shanahan <pats@acm.org> - 2012-05-01 12:17 -0700
          Re: Help with program Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-05-01 15:27 -0500
            Re: Help with program Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-05-01 15:32 -0500
          Re: Help with program Arne Vajhøj <arne@vajhoej.dk> - 2012-05-01 19:51 -0400
            Re: Help with program Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-05-01 17:06 -0700
              Re: Help with program Patricia Shanahan <pats@acm.org> - 2012-05-01 18:08 -0700
                Re: Help with program Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-05-02 12:53 -0700
              Re: Help with program Arne Vajhøj <arne@vajhoej.dk> - 2012-05-02 16:25 -0400
                Re: Help with program Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-05-02 15:27 -0700
    Re: Help with program Tsukino Usagi <usagi@tsukino.ca> - 2012-05-02 03:15 +0900
      Re: Help with program markspace <-@.> - 2012-05-01 11:25 -0700
    Re: Help with program Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-05-01 11:16 -0700
    Re: Help with program markspace <-@.> - 2012-05-01 11:16 -0700
    Re: Help with program rossum <rossum48@coldmail.com> - 2012-05-01 20:41 +0100
    Re: Help with program Roedy Green <see_website@mindprod.com.invalid> - 2012-05-01 18:15 -0700

#14086 — Help with program

Frommbuenik5@yahoo.com
Date2012-05-01 10:31 -0700
SubjectHelp with program
Message-ID<0d9aaec9-10f1-42b5-af5c-f4d563a934ab@e42g2000yqa.googlegroups.com>
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?

[toc] | [next] | [standalone]


#14088

FromPatricia Shanahan <pats@acm.org>
Date2012-05-01 10:53 -0700
Message-ID<M56dnaRDzNGAvj3SnZ2dnUVZ_qKdnZ2d@earthlink.com>
In reply to#14086
On 5/1/2012 10:31 AM, mbuenik5@yahoo.com 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.

What do you have so far? What are your thoughts on what the assignment
means, and how to approach it? Do you have a test program to see if your
attempted solutions work or not?

Patricia

[toc] | [prev] | [next] | [standalone]


#14095

Fromglen herrmannsfeldt <gah@ugcs.caltech.edu>
Date2012-05-01 19:12 +0000
Message-ID<jnpcj3$i98$1@speranza.aioe.org>
In reply to#14088
Patricia Shanahan <pats@acm.org> wrote:
> On 5/1/2012 10:31 AM, mbuenik5@yahoo.com 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

[toc] | [prev] | [next] | [standalone]


#14096

FromPatricia Shanahan <pats@acm.org>
Date2012-05-01 12:17 -0700
Message-ID<LsadnfXeh_Mmqz3SnZ2dnUVZ_r6dnZ2d@earthlink.com>
In reply to#14095
On 5/1/2012 12:12 PM, glen herrmannsfeldt wrote:
> Patricia Shanahan<pats@acm.org>  wrote:
>> On 5/1/2012 10:31 AM, mbuenik5@yahoo.com 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.

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

There are, very rare, situations in which local variables are expensive,
and there are some clever tricks for doing without them in various
situations. This is not one of those situations.

Patricia

[toc] | [prev] | [next] | [standalone]


#14099

FromLeif Roar Moldskred <leifm@dimnakorr.com>
Date2012-05-01 15:27 -0500
Message-ID<vpSdnYjTtsew2j3SnZ2dnUVZ7vudnZ2d@giganews.com>
In reply to#14096
Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> 
>  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])?

Ow. You just turned a simple O( 1 ) operation into O( N ). You're
solution is also probably wrong, as it leaves a unchanged.

-- 
Leif Roar Moldskred

[toc] | [prev] | [next] | [standalone]


#14100

FromLeif Roar Moldskred <leifm@dimnakorr.com>
Date2012-05-01 15:32 -0500
Message-ID<vpSdnYvTtsfm1T3SnZ2dnUVZ7vudnZ2d@giganews.com>
In reply to#14099
Leif Roar Moldskred <leifm@dimnakorr.com> wrote:
> 
> Ow. You just turned a simple O( 1 ) operation into O( N ). You're
> solution is also probably wrong, as it leaves a unchanged.

Ack. My mistake. I forgot that the List returned by asList() is
backed by the original array. 

-- 
Leif Roar Moldskred 
 

[toc] | [prev] | [next] | [standalone]


#14116

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-05-01 19:51 -0400
Message-ID<4fa07709$0$293$14726298@news.sunsite.dk>
In reply to#14096
On 5/1/2012 4:21 PM, Stefan Ram wrote:
> Patricia Shanahan<pats@acm.org>  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

[toc] | [prev] | [next] | [standalone]


#14117

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-05-01 17:06 -0700
Message-ID<mU_nr.181146$s82.80514@newsfe10.iad>
In reply to#14116
On 5/1/12 4:51 PM, Arne Vajhøj wrote:
> On 5/1/2012 4:21 PM, Stefan Ram wrote:
>> Patricia Shanahan<pats@acm.org> 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);
}

[toc] | [prev] | [next] | [standalone]


#14119

FromPatricia Shanahan <pats@acm.org>
Date2012-05-01 18:08 -0700
Message-ID<v4ydneVdSJRqFT3SnZ2dnUVZ_rGdnZ2d@earthlink.com>
In reply to#14117
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

[toc] | [prev] | [next] | [standalone]


#14143

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-05-02 12:53 -0700
Message-ID<Dggor.21912$em4.18760@newsfe21.iad>
In reply to#14119
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[]".

[toc] | [prev] | [next] | [standalone]


#14146

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-05-02 16:25 -0400
Message-ID<4fa19850$0$292$14726298@news.sunsite.dk>
In reply to#14117
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<pats@acm.org> 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

[toc] | [prev] | [next] | [standalone]


#14161

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-05-02 15:27 -0700
Message-ID<Zwior.21918$em4.14907@newsfe21.iad>
In reply to#14146
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<pats@acm.org> 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.

[toc] | [prev] | [next] | [standalone]


#14090

FromTsukino Usagi <usagi@tsukino.ca>
Date2012-05-02 03:15 +0900
Message-ID<jnp989$jt5$1@dont-email.me>
In reply to#14086
On 5/2/2012 2:31 AM, mbuenik5@yahoo.com 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.

[toc] | [prev] | [next] | [standalone]


#14094

Frommarkspace <-@.>
Date2012-05-01 11:25 -0700
Message-ID<jnp9rh$on1$1@dont-email.me>
In reply to#14090
On 5/1/2012 11:15 AM, Tsukino Usagi wrote:
> On 5/2/2012 2:31 AM, mbuenik5@yahoo.com 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.



[toc] | [prev] | [next] | [standalone]


#14091

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-05-01 11:16 -0700
Message-ID<%LVnr.34951$Ex1.28733@newsfe18.iad>
In reply to#14086
On 5/1/12 10:31 AM, mbuenik5@yahoo.com 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.

[toc] | [prev] | [next] | [standalone]


#14092

Frommarkspace <-@.>
Date2012-05-01 11:16 -0700
Message-ID<jnp9ah$kct$1@dont-email.me>
In reply to#14086
On 5/1/2012 10:31 AM, mbuenik5@yahoo.com 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.

[toc] | [prev] | [next] | [standalone]


#14097

Fromrossum <rossum48@coldmail.com>
Date2012-05-01 20:41 +0100
Message-ID<4ne0q71jgiq4qdpcs8hles6hr564col2i1@4ax.com>
In reply to#14086
On Tue, 1 May 2012 10:31:36 -0700 (PDT), mbuenik5@yahoo.com 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?
This is homework.  We don't do homework.  What we will do is to help
you with your homeworrk.  YOU do the work, we just help and advise.

>I need help writing a Java method named swap that takes an integer
>array and two indexes. ... The return type for swap is void.
Can you do that?  Just write a method, named swap, that takes the
three specified parameters and does nothing.  Do so and get it to
compile.  If you can't get it to compile, then post your code here and
we can help.

Do not proceed until your empty method compiles correctly and without
errors.

>The method then swaps the integers at those two indexes.
Now add the code inside the method to use your three parameters to do
what the question asks.  Test it thoroughly to make sure it works
correctly.  Again, if you get into difficulties, post your code here
and we can help.  No code = no help.

rossum

[toc] | [prev] | [next] | [standalone]


#14120

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-05-01 18:15 -0700
Message-ID<9821q7psaovre84hsa7t241g7hudi5gvk5@4ax.com>
In reply to#14086
On Tue, 1 May 2012 10:31:36 -0700 (PDT), mbuenik5@yahoo.com 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.
.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web