2013-09-26 44 views
0
/** 
    * Fills the mutations array and sends to printMutations 
    * @param firstString original DNA generation. 
    */ 

    public static void mutation(String firstString) 
    { 
     final int ROWSINDEX = 26; 
     final int SPACEUSED = firstString.length(); 


     char[][] mutations = new char[ROWSINDEX][SPACEUSED]; 
     String dnaChars = "AGTC"; 

     for (int i = 0; i < SPACEUSED; i++) 
     { 
     mutations[0][i] = firstString.charAt(i); 
     } 

     for (int i = 1; i < ROWSINDEX - 1; i++) 
     { 
     for (int j = 0; j < SPACEUSED; j++) 
     { 
      mutations[i][j] = mutations[i - 1][j]; 

      } 
      int randomIndex = (int) (Math.random() * (SPACEUSED)); 
      int randomChar = (int) (Math.random() * (dnaChars.length())); 
      mutations[i][randomIndex] = dnaChars.charAt(randomChar); 
     } 
      printMutations(mutations, ROWSINDEX, SPACEUSED); 
     } 


    /** 
    * Prints the 25 generations of mutations and the astrixes. 
    * @param mutations array that holds the mutated generations 
    * @param ROWSINDEX integer holding the max amount of rows possible 
    * @param SPACEUSED integer that holds the number of columns 
    */ 

    public static void printMutations(char[][] mutations, int ROWSINDEX, int SPACEUSED) 
    { 
     for (int i = 0; i < ROWSINDEX; i++) 
     { 
     for (int j = 0; j < SPACEUSED; j++) 
     { 
      System.out.print(" " + mutations[i][j]); 
     } 

     if (i > 0) 
     { 
      char[] a = mutations[i]; 
      char[] a2 = mutations[i - 1]; 
      if (Arrays.equals(a, a2) == true) 
      { 
       System.out.print("*"); 
      } 
     } 
     System.out.println(""); 
     } 
    } 
} 

在輸出結束時,您應該在模擬過程中沒有改變的任何字母列的下面打印星號。如果該列從未被視覺改變,我需要在每列的末尾打印一個星號。建議嗎?

程序的例子來看應該出現這樣的:

$ java BeckJ0926 
Enter a DNA sequence up to 80 bp: ATTCGGCTA 
ATTCGGCTA 
ATCCGGCTA 
ATCCGTCTA 
ATCCGTCTA * 
... 
ATCCGTCTT 
AACCGTCTT 
AATCGTCTT 
* ** ** 

,我不知道是否會是最好設置一個布爾數組,以確定每個列是否有變化,這是我原本想做的事情。我不能使用arrayLists。

回答

0

你可以行

mutations[i][randomIndex] = dnaChars.charAt(randomChar); 

改變

char currentChar = mutations[i][randomIndex]; 
if (currentChar == randomChar) { 
    System.out.print("*"); 
} else { 
    mutations[i][randomIndex] = dnaChars.charAt(randomChar); 
    printMutation(mutations[i]); 
} 

更改打印功能,需要一個突變並打印。

private void printMutation(char[] mutation) { 
    for (char a : mutation) { 
     System.out.print(a + " "); 
    } 
} 

是這樣有用嗎?

相關問題