# Please Help!!Daughter needs help with java code

Discussion in 'Java' started by Miggy23@gmail.com, Dec 1, 2005.

1. ### Guest

Hello everyone,

I apologize for taking up anyone's time with such a newbie question.
However, my daughter is tearing her hair out over this code. I myself
don't know anything about programming however, being the father that I
am, I am hoping that someone can respond to my plea for help and
provide her with a solution. Below is her code. Thank you all and God
Bless!!

Problem: Print out all the 4 number combinations using only the
following numbers: 6, 9,0,3,1

The code is giving her only 5 combinations at the moment. This is
where she needs help.

//Source code//

import java.math.BigInteger;

public class NumGen {
public static void main(String[] args) {
char[] elements = {'6', '9', '0', '3', '1'};
int[] indices;
CombinationGenerator x = new CombinationGenerator (elements.length,
4);
StringBuffer combination;

while (x.hasMore ()) {
combination = new StringBuffer ();
indices = x.getNext ();
for (int i = 0; i < indices.length; i++) {
combination.append (elements[indices]);
}
System.out.println (combination.toString ());
}

}
}

, Dec 1, 2005

2. ### Oliver WongGuest

<> wrote in message
news:...
>
> Problem: Print out all the 4 number combinations using only the
> following numbers: 6, 9,0,3,1

The problem statement is not clear to me. Is it asking for all strings
of length 4 whose alphabet is {6, 9, 0, 3, 1}? Or is it asking to print all
permutations of the set {6,9,0,3,1}, except don't print the last character?
Or something else?

Here's a legal string from the first interpretation which is not legal
in the second interpretation, in case the difference isn't clear: 0000.

- Oliver

Oliver Wong, Dec 1, 2005

3. ### Guest

You also did not paste an additional class file CombinationGenerator.
Also what's the purpose this class??

http://www.binaryfrost.com

, Dec 1, 2005
4. ### Daniel DyerGuest

On Thu, 01 Dec 2005 22:25:26 -0000, <> wrote:

> You also did not paste an additional class file CombinationGenerator.
> Also what's the purpose this class??
>

It's almost certainly this class (or something very similar)

http://www.merriampark.com/comb.htm

I stumbled across it myself a few weeks back when looking for some code to
generate combinations. The algorithm is sound but I modified it to work
with longs rather than BigIntegers since I didn't need to work with big
sets and I used generics to get it actually generate the combinations as
arrays of the appropriate type rather than just an int array of indices.

Dan.

--
Daniel Dyer
http://www.dandyer.co.uk

Daniel Dyer, Dec 1, 2005
5. ### Guest

Sorry for the confusion everyone. I am trying my best to be clear.
She is trying to print out all the possible number combinations of
length 4 using only the following numbers: 6,9,0,3, and 1.

, Dec 1, 2005
6. ### Guest

Daniel,

Thanks for the link, however, how would she modify the code to only do
9,6,3,0,1 instead of alphabets of length 4?

, Dec 1, 2005
7. ### Guest

I guess someone is trying to get his homework done by using the usenet
)

, Dec 1, 2005
8. ### Guest

Thanks for the reply Oliver. Sorry for the confusion. She is trying
to find all strings of length 4 whose alphabet is {6,9,0,3,1). In
other words, find all the possible number combinations for {6,9,0,3,1)
of lenght 4. Sorry if I am still not clear. I'm not a programmer.

Thank you all for your help!!

, Dec 1, 2005
9. ### Oliver WongGuest

<> wrote in message
news:...
> Sorry for the confusion everyone. I am trying my best to be clear.
> She is trying to print out all the possible number combinations of
> length 4 using only the following numbers: 6,9,0,3, and 1.
>

To me, there is no new information in this post with respect to your
original one.

Here's what I understand (and have understood since the first post):

The strings are of length 4.
Using characters other than 6, 9, 0, 3, 1 is not permitted.
You want ALL such strings.

Here's what's unclear (and was unclear since the first post):

Can the characters repeat? E.g. is 0000 a legal string?
Is the fact that the numbers not in any discernable order of some
significance? I.e. why didn't you say "0,1,3,6,9"?
Why is BigInteger getting involved for such small integers?
What is CombinationGenerator? Or more importantly, where did it come
from? You daughter? The teacher? Somewhere else?

- Oliver

Oliver Wong, Dec 1, 2005
10. ### Oliver WongGuest

<> wrote in message
news:...
> Thanks for the reply Oliver. Sorry for the confusion. She is trying
> to find all strings of length 4 whose alphabet is {6,9,0,3,1). In
> other words, find all the possible number combinations for {6,9,0,3,1)
> of lenght 4. Sorry if I am still not clear. I'm not a programmer.
>
> Thank you all for your help!!

Does she know what recursion is?

- Oliver

Oliver Wong, Dec 1, 2005
11. ### Mickey SegalGuest

<> wrote in message
news:...
> Thanks for the reply Oliver. Sorry for the confusion. She is trying
> to find all strings of length 4 whose alphabet is {6,9,0,3,1). In
> other words, find all the possible number combinations for {6,9,0,3,1)
> of lenght 4. Sorry if I am still not clear. I'm not a programmer.

The part that is unclear is whether this is sampling with replacement or
sampling without replacement. In other words, is 9933 a possible solution
or are we restricted to solutions such as 9613?

BTW, how old are the kids getting this assignment? Were they given the
CombinationGenerator code to work with? CombinationGenerator looks like a
nice piece of code; I could of used it for the send-up that my 9 year old
and I programmed to protest the difficulty of the Magic Square math homework
given to my 6 year old.

Mickey Segal, Dec 1, 2005
12. ### Guest

I think she copied the code from the link that Daniel provided.
Hopefully this will clarify it,
- The strings are of length 4.
- Number combinations are only permitted to use the following
characters: 6,9,0,3,1

I can't answer the rest of your questions b/c I don't understand the
terminology. I am not a programmer.

Thanks!!

, Dec 1, 2005
13. ### Daniel DyerGuest

On Thu, 01 Dec 2005 23:06:46 -0000, Oliver Wong <>
wrote:

> Here's what's unclear (and was unclear since the first post):
>
> Can the characters repeat? E.g. is 0000 a legal string?
> Is the fact that the numbers not in any discernable order of some
> significance? I.e. why didn't you say "0,1,3,6,9"?

I think he means combinations in the mathematical sense (i.e. ordering is
unimportant and elements cannot be repeated).

> Why is BigInteger getting involved for such small integers?

Because the code to generate combinations calculates factorials. The
largest factorial that can be stored in a Java long is 20!, which means
without BigInteger source sets larger than 20 elements could not be used.
This of course is not a problem for the set he posted. However, if this
is homework and the CombinationGenerator was not provided by the
teacher/lecturer, then the use of BigInteger would be a give-away that
this code was not written specifically for the problem at hand.

> What is CombinationGenerator? Or more importantly, where did it come
> from? You daughter? The teacher? Somewhere else?

Almost certainly from the link I posted. The class name, method names and
general approach are identical and both use BigInteger.

Dan.

--
Daniel Dyer
http://www.dandyer.co.uk

Daniel Dyer, Dec 1, 2005
14. ### Guest

Thanks for clarifying it for me Daniel!

, Dec 1, 2005
15. ### Chris SmithGuest

<> wrote:
> I think she copied the code from the link that Daniel provided.
> Hopefully this will clarify it,
> - The strings are of length 4.
> - Number combinations are only permitted to use the following
> characters: 6,9,0,3,1
>
> I can't answer the rest of your questions b/c I don't understand the
> terminology. I am not a programmer.

This is not a matter of being a programmer. It's a matter of an
ambiguous problem statement. Your daughter would need to ask her
professor for clarification (after, of course, understanding the
ambiguity herself).

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Chris Smith, Dec 2, 2005
16. ### Monique Y. MudamaGuest

On 2005-12-01, penned:
> Hello everyone,
>
> I apologize for taking up anyone's time with such a newbie question.
> However, my daughter is tearing her hair out over this code. I
> myself don't know anything about programming however, being the
> father that I am, I am hoping that someone can respond to my plea
> for help and provide her with a solution. Below is her code. Thank
> you all and God Bless!!
>

After reading all of this, the only thing that comes to mind is that
if your daughter needs help, she should probably be asking these
questions herself. Having a non-programmer trying to translate for a
programming student is ... sub-optimal.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Monique Y. Mudama, Dec 2, 2005
17. ### Chris UppalGuest

Permuatations [was: Please Help!!Daughter...]

Daniel Dyer wrote:

> http://www.merriampark.com/comb.htm
>
> I stumbled across it myself a few weeks back when looking for some code to
> generate combinations.

Nice algorithm. Does anyone know of something comparatively slick for
generating permutations ?

I like the way that the state of the overall iteration is implicit in the state
of the array ('a' in the code on the website). It's straightforward to modify
the idea so that it generates all combinations with repetitions included, and
even more straightforward to make it generate all permutations with repetitions
included, but I can't think of a comparably small change that would make it do
"normal" permutations where repetitions are not included.

It's pretty easy to generate permutations by recursion, but converting a deeply
recursive algorithm into one suitable for external iteration would take, I
think, a Stack of Iterators -- which seems excessive when the other three
closely related algorithms can be handled so elegantly.

BTW, in case it's not clear (I'm not sure if I'm using the standard
terminology) to illustrate what I mean by permutations/combinations
with/without repetitions. Given the source collection {A B C}, and wanting
sub-collections of length 2, I mean
Combinations: AB AC BA
Permutations: AB AC BA BC CA CB
Combinations with repetition: AA AB AC BB BC CC
Permutations with repetition: AA AB AC BA BB BC CA CB CC

-- chris

Chris Uppal, Dec 2, 2005
18. ### Oliver WongGuest

Re: Permuatations [was: Please Help!!Daughter...]

"Chris Uppal" <-THIS.org> wrote in message
news:4390a938\$0\$20536\$...
>
> BTW, in case it's not clear (I'm not sure if I'm using the standard
> terminology) to illustrate what I mean by permutations/combinations
> with/without repetitions. Given the source collection {A B C}, and
> wanting
> sub-collections of length 2, I mean
> Combinations: AB AC BA

Did you mean "AB AC BC"?

> Permutations: AB AC BA BC CA CB
> Combinations with repetition: AA AB AC BB BC CC
> Permutations with repetition: AA AB AC BA BB BC CA CB CC

- Oliver

Oliver Wong, Dec 2, 2005
19. ### Roedy GreenGuest

Re: Permuatations [was: Please Help!!Daughter...]

On Fri, 2 Dec 2005 20:11:21 -0000, "Chris Uppal"
<-THIS.org> wrote, quoted or indirectly
quoted someone who said :

>
>Nice algorithm. Does anyone know of something comparatively slick for
>generating permutations ?

see http://mindprod.com/jgloss/permutation.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green, Dec 2, 2005
20. ### Daniel DyerGuest

Re: Permuatations [was: Please Help!!Daughter...]

On Fri, 02 Dec 2005 20:11:21 -0000, Chris Uppal
<-THIS.org> wrote:

> Daniel Dyer wrote:
>
>> http://www.merriampark.com/comb.htm
>>
>> I stumbled across it myself a few weeks back when looking for some code
>> to
>> generate combinations.

>
> Nice algorithm. Does anyone know of something comparatively slick for
> generating permutations ?

Same site:

http://www.merriampark.com/perm.htm

Dan.

--
Daniel Dyer
http://www.dandyer.co.uk

Daniel Dyer, Dec 2, 2005

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