简体   繁体   中英

How to check for conditions dynamically in java?

I am trying to create a list of all possible two letter words using 4 letters - A, C, T, G. So the output should be like

Two letter word output

AA
CA
TA
GA
AC
CC
TC
GC
AT
CT
TT
GT
AG
CG
TG
GG

I am able to produce the above output using this Java code

Two letter word Java code

import java.io.IOException;

class Variations {

    private static char replace (char bc) {
        switch (bc) {
            case 'A':
                bc = 'C';
                break;

            case 'C':
                bc = 'T';
                break;

            case 'T':
                bc = 'G';
                break;

            case 'G':
                bc = 'A';
                break;
        }

        return bc;
    }

    public static void main(final String[] args) throws IOException {

        String code = "AA";
        char[] bc = new char[code.length()];

        for (int i=0; i<code.length(); i++) {
            bc[i] = code.charAt(i);
        }


        for (int i=0; i < Math.pow(4, bc.length); i++) {

            System.out.println(bc);

            bc[0] = replace(bc[0]);

            if (i%4 == 0) {
                bc[1] = replace(bc[1]);
            }


        }

    }
}

Now I want to create a list of all possible three letter words using the same 4 letters - A, C, T, G. So the output will be like

Three letter word output

AAA
CAA
TAA
GAA
ACA
CCA
TCA
GCA
ATA
CTA
TTA
GTA
AGA
CGA
TGA
GGA
AAC
CAC
TAC
GAC
ACC
CCC
TCC
GCC
ATC
CTC
TTC
GTC
AGC
CGC
TGC
GGC
AAT
CAT
TAT
GAT
ACT
CCT
TCT
GCT
ATT
CTT
TTT
GTT
AGT
CGT
TGT
GGT
AAG
CAG
TAG
GAG
ACG
CCG
TCG
GCG
ATG
CTG
TTG
GTG
AGG
CGG
TGG
GGG

So to generate this above output, I just have to add one more if statement in my main method,

Three letter word Java code

import java.io.IOException;

class Variations {

    private static char replace (char bc) {
        switch (bc) {
            case 'A':
                bc = 'C';
                break;

            case 'C':
                bc = 'T';
                break;

            case 'T':
                bc = 'G';
                break;

            case 'G':
                bc = 'A';
                break;
        }

        return bc;
    }

    public static void main(final String[] args) throws IOException {

        String code = "AAA";
        char[] bc = new char[code.length()];

        for (int i=0; i<code.length(); i++) {
            bc[i] = code.charAt(i);
        }


        for (int i=0; i < Math.pow(4, bc.length); i++) {

            System.out.println(bc);

            bc[0] = replace(bc[0]);

            if (i%4 == 0) {
                bc[1] = replace(bc[1]);
            }

            if (i%16 == 0) {
                bc[2] = replace(bc[2]);
            }


        }

    }
}

So my question is, if the user has a choice of specifying the whether he/she wants a list of all 2-letter words or 3-letter words - how do I combine both the programs? Both programs vary by just one conditional statement.

The user may specify if he/she needs a list of all possible 2-letter words or 3-letter words or even 4-letter words. My program has to dynamically add or remove a conditional statement to generate the list. Is it possible to do this in Java?

Thank you in advance!

You are basically implementing a cartesian product. There is a very nice method in Google's Guava called Sets#cartesianProduct .

With this you can easily generate all possible combinations:

Set<List<String>> set = Sets.cartesianProduct(Collections.nCopies(3, ImmutableSet.of("A", "C", "T", "G")));

You can have the 3 as a user-determined parameter.

Now we just need some Java 8 Stream "magic":

String output = set.stream()
    .map(list -> list.stream().collect(Collectors.joining()))
    .collect(Collectors.joining("\n"));

You can of course adjust the last part, it is just to mimic the output you showed in your question.

Yes. A good way to do this would be to have some sort of code to ask the user which length they would like: 2 or 3. You can do this in the console with a Scanner. Add

import java.util.Scanner;

to the top of your code. Then, at the top of the main method, add

Scanner scannerName = new Scanner(System.in);

Then you can ask the user and save their response.

System.out.print("Enter the length you would like, AA or AAA: ");
String code = scannerName.next();

Then you can change the if statement that you want to add on to be:

if (i%16 == 0 && code.length() == 3) {your code...}

This way it will only run if the user inputs AAA or another token of length 3.

Also, if you wanted to expand this to work for up to four, you can collapse the code like this:

for (int count = 2; count <= code.length(); count++){
   if (i % (Math.pow(2, count)) == 0){
      bc[count] = replace(bc[count]);
   }
}

And this should work for all of 1, 2, 3, 4, and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM