Please Help!!Daughter needs help with java code

M

Miggy23

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 ());
}

}
}
 
O

Oliver Wong

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
 
D

Daniel Dyer

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

Miggy23

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

Miggy23

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

Miggy23

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!!
 
O

Oliver Wong

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
 
O

Oliver Wong

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
 
M

Mickey Segal

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

Miggy23

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

Daniel Dyer

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

Chris Smith

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
 
M

Monique Y. Mudama

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

Chris Uppal

Daniel said:
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
 
O

Oliver Wong

Chris Uppal said:
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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top