Need Help Developing Simple Program

I need to:

Develop a program that uses recursion to create all permutations of a string argument and prints the permutations, one permutation per line. The string argument must be supplied as the first argument of the program execution call.

If an argument is not supplied whenever the program is to be executed, output a message stating that a string argument is required instead of letting the program abort.

Your help in this would be greatly appreciated.

[483 byte] By [justaregulara] at [2007-10-2 17:19:09]
# 1
> Your help in this would be greatly appreciated.You bet. You have to tell us what part you are stuck on and need help with, though.Drake
Drake_Duna at 2007-7-13 18:35:02 > top of Java-index,Other Topics,Algorithms...
# 2

You are in luck! Code that does exactly what you want exists on this forum. This appears to be a very common assignment. All you have to do is look at every entry in this forum or use the search function until you find it.

Granted that search can take more time than writing the function from scratch but its there if you look for it.

Enjoy

marlin314a at 2007-7-13 18:35:02 > top of Java-index,Other Topics,Algorithms...
# 3

<quote>

public class PermutationGenerator

{

private void perm(String s, String prefix) {

int length = s.length();

if (length== 0) System.out.println(prefix);

else {

for (int i = 0; i < length; i++){

perm(s.substring(0, i) + s.substring(i+1, length), prefix + s.charAt(i));

}

}

}

public void printPermutations(String s){

perm(s,"");

}

public static void main(String[] args) {

new PermutationGenerator().printPermutations("ABC") ;

}

}

</quote>

killerCodingNinjaMonkeya at 2007-7-13 18:35:02 > top of Java-index,Other Topics,Algorithms...
# 4
this is close but if an argument is not supplied whenever the program is to be executed, i need to output a message stating that a string argument is required instead of letting the program abort.
justaregulara at 2007-7-13 18:35:02 > top of Java-index,Other Topics,Algorithms...
# 5

> this is close but if an argument is not supplied

> whenever the program is to be executed, i need to

> output a message stating that a string argument is

> required instead of letting the program abort.

So basically your saying something like:

if (args.length > 0) {

// execute the program

} else {

// print out your message about the manadatory argument

}

Is that what your looking for?

J

Java_Jaya at 2007-7-13 18:35:02 > top of Java-index,Other Topics,Algorithms...
# 6
Yes, but where do i insert that into the program?
justaregulara at 2007-7-13 18:35:02 > top of Java-index,Other Topics,Algorithms...
# 7

> Yes, but where do i insert that into the program?

This is like asking someone for "help" rotating the tires of your car, then having the person come over and do the entire tire rotation themselves (which is what you meant when you said "help") while you eat a sandwich in the kitchen, and then complaining because they got bored and quit before putting on the last hubcap.

They have already helped you cheat on your assignment enough. If you cannot put the cherry on top, drop the class.

Drake

Drake_Duna at 2007-7-13 18:35:02 > top of Java-index,Other Topics,Algorithms...
# 8
go back to your hole...40 year old virgin
justaregulara at 2007-7-13 18:35:02 > top of Java-index,Other Topics,Algorithms...
# 9
> go back to your hole...40 year old virginDefinitely professional programmer material.Drake
Drake_Duna at 2007-7-13 18:35:02 > top of Java-index,Other Topics,Algorithms...
# 10

private void perm(String s, String prefix) {

int length = s.length();

if (length== 0) System.out.println(prefix);

else {

for (int i = 0; i < length; i++){

perm(s.substring(0, i) + s.substring(i+1, length), prefix + s.charAt(i));

}

}

}

public void printPermutations(String s){

perm(s,"");

}

I am confused about the purpose of the prefix....and perm(s, " ")

justaregulara at 2007-7-13 18:35:02 > top of Java-index,Other Topics,Algorithms...
# 11

> I am confused about the purpose of the prefix....and

> perm(s, " ")

Translation: I don't know jack about programming, can someone do my homework?

The answers were given to you. If you can't figure it out now, it's tiime to read some of those textbooks or do some tutorials.

Maybe this is a bit of a harsh reply, but you provoked it with your reaction to Drake (reply #8).

Good luck.

prometheuzza at 2007-7-13 18:35:02 > top of Java-index,Other Topics,Algorithms...
# 12
It goes in the beginning of the main method.
killerCodingNinjaMonkeya at 2007-7-13 18:35:02 > top of Java-index,Other Topics,Algorithms...