Title: How do I get my program to do random methods?
Tags: java;algorithm;user-interface;encryption;methods
Question: I'm working on a project for the google science fair. I'm making an application which takes two numbers (Barcode and Serial Number) and changes them into two different numbers using various algorithms( i.e, Like Fisher-Yates shuffle).
My code looks something like this :
enter code here public static void main(String[] args) {
reverseNum b = new reverseNum();
addOne   c= new addOne();
plusThree f= new plusThree();
plusNine  i = new plusNine();
plusTwo l = new plusTwo();
minusTwo  p = new minusTwo();
plusOne  r= new plusOne();
plusFour u= new plusFour();
threeTwo x= new threeTwo();
intoTwo  ab= new intoTwo();
plusFive bc = new plusFive();
intoSix cd= new intoSix();
twoOne de = new twoOne() ;
plusSeven ef= new plusSeven();
plusEight fg= new plusEight();
minOne gh = new minOne();
intoSeven hi = new intoSeven();
intoEight ij = new intoEight();
intoNine jk = new intoNine();
intOne kl = new intOne();
intoFour lm = new intoFour();
// TODO code application logic here
Scanner user_input = new Scanner( System.in );
String a ;
System.out.println("Enter the Barcode:");
a = user_input.next();
System.out.println("Encrypted Barcode:"+ blah.randomMethod(a));
//Random method from the above given methods should modify this.
String z;
System.out.println("Enter the Serial Number:");
z= user_input.next();
System.out.println("Encrypted Serial Number:" + blah.randomMethod(z) );
//Random method from the above given methods should modify this
My main objective is to modify the two given numbers with the above mentioned methods(algorithms).I want a random method from the given list to modify the given numbers each time the user runs the program. I also need to put this all into a GUI. Please help.
Comment: I mean, I want f  and g to be the default variables for the barcode and Serial number, and I want them to be modified by random methods each time the user runs the program

Comment: My main objective is to modify the Serial Number and Barcode to stop counterfeits/knock offs. The user will enter the barcode and serial number of a product and it will be modified by  methods which are stored in separate classes. I want a default variable for the barcode and serial number instead of separate ones and then I want these numbers to be modified randomly by a random method each time the user enters the serial number and barcode. @RobertHarvey

Comment: @owlstead I have revealed them now, And I've also modified my code.

Comment: @RobertHarvey any solutions to this?

Comment: What problem are you trying to solve by using random methods?

Comment: @gmcgath: I think he's trying to provide a random implementation.

Comment: The answer provided below didn't help you?

Comment: I don't understand what you mean by coming under a single variable. You can create an embedded class that holds both the barcode and serial number variables and manipulate them together as a single object. Then your methods could take them as a single argument and modify them in place, which might be cleaner. Is that what you're looking for?

Comment: The description and the fact that you want to keep the actual method implementations secret does not give me a good warm feeling for your kristinajones@example.org. Have you heard about [Kerckhoff's principle](http://en.wikipedia.org/wiki/Kerckhoffs%27s_principle)?

Comment: On the other hand some things are probably best left a secret...


Here is another answer: I agree with jgitter about the Strategy and Factory pattern.
I'm posting an example.
public interface Algorithm {
public String execute(String input);
public class SomeAlgorithm implements Algorithm {
public String execute(String input) {
///... Algorithm here
}
public class AnotherAlgorithm implements Algorithm {
public String execute(String input) {
///... Algorithm here
}
public abstract class AlgorithmFactory {
public static Algorithm createRandomAlgorithm() {
//Create a new Random object
Random randomEngine = new Random();
//Retrieve a number from 0 (inclusive) to 2 (exclusive)
int randomNumber = randomEngine.nextInt(2);
//Select which implementation to use according to the random number
switch(randomNumber) {
case 0: return new SomeAlgorithm();
case 1: return new AnotherAlgorithm();
}
//This will most likely never get called, but a return value is mandatory
return null;
}
Then, you would retrieve and execute the algorithm like this:
String encryption = AlgorithmFactory.createRandomAlgorithm().execute(input);
Please note that the factory is abstract and has a static method, which is not how I would approach this, but it will help you out in your problem nice and easily.
Comment for this answer: Thanks for your consideration. But unfortunately,this isn't what I want. Thanks a lot though :) @Mati Cicero
Comment for this answer: Thanks for giving an example implementation.  Looks good to me.
Comment for this answer: Please explain *why* this isn't what you want. What's missing from the given implementation?

Here is another answer: Use the strategy pattern.  Have each algorithm implement a version of that strategy.  Then use a factory to get a random strategy implementation using any mechanism you want.  No need to combine the inputs into a single object.
Comment for this answer: Can you make this answer a bit less abstract, perhaps by providing a brief code sample?  Describing solutions using pattern lingo is fine, if you know the lingo.  All too often, I see patterns being used in cargo cult fashion, rather than trying to understand them first.  Show him a rudimentary factory method with a random number and a case statement.
Comment for this answer: Looks like Mati beat me to the punch.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 10, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:05/05/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><algorithm><user-interface><encryption><methods>', 'Title': 'How do I get my program to do random methods?', 'ViewCount': 85, 'id': 23477062, 'user_AboutMe': None, 'user_AccountId': 4426565.0, 'user_DisplayName': 'user3604889', 'user_Reputation': 29.0, 'user_Views': 8.0, 'user_id': 3604889.0}}