: Deoxyribonucleic acid, or DNA, is comprised of four bases: (G)uanine, (C)ytosine, (A)denine and (T)hymine. Ribonucleic acid, or RNA, is different than DNA in that it contains no Thymine; thymine is replaced with something called (U)racil. For this assignment, you will create an array of 255 characters. You must start by filling the array with random characters of G, C, A and T. You must then print out the array. Next, replace all the instances of Thymine with Uracil. Finally, you must print out the array again. In your solution, you must write at least one function that contributes to the solution. You must use the length attribute of the array in your answer. Sample run

Respuesta :

Answer:

The code solution is written in Java as below:

  1. import java.util.Arrays;
  2. import java.util.Random;
  3. public class Main {
  4.    public static void main(String[] args) {
  5.        char my_seq[] = new char[255];
  6.        generateRandomSequence(my_seq);
  7.        System.out.println("Generated DNA Sequence: \n" + Arrays.toString(my_seq));
  8.        System.out.println();
  9.        getRNASeq(my_seq);
  10.        System.out.println("Generated RNA Sequence: \n" + Arrays.toString(my_seq));
  11.    }
  12.    public static void generateRandomSequence(char [] seq)
  13.    {
  14.        char bases[] = {'G', 'C', 'A', 'T'};
  15.        Random rand = new Random();
  16.        for(int i=0; i < seq.length; i++)
  17.        {
  18.            int index = rand.nextInt(4);
  19.            seq[i] = bases[index];
  20.        }
  21.    }
  22.    public static void getRNASeq(char[] seq)
  23.    {
  24.        for(int i = 0; i < seq.length; i++)
  25.        {
  26.            if(seq[i] == 'T')
  27.            {
  28.                seq[i] = 'U';
  29.            }
  30.        }
  31.    }
  32. }

Explanation:

Firstly, create an character array, my_seq, and set its size to 255. (Line 1)

To fill up the my_seq array with random characters, (A, T, G & C), a function  generateRandomSequence() is created (Line 19 - 29). This function will accept one argument which is seq array.  The procedure of generation of random sequences are as follows:

  1. Create a short character array, bases, and initialize it with four elements, {'G', 'C', 'A', 'T'}. (Line 21)
  2. We need to rely on Java Random generator to randomly pick one of the four elements from the bases array to fill up our seq array. Hence, a Random object, rand, is created. (Line 22)
  3. Create a for-loop to iterate through the element of seq array one by one. (Line 24).
  4. In each iteration, we use nextInt() method from the Java Random object to generate a random index. (Line 26). The argument 4 in nextInt(4) will generate an integer between 0 - 3.
  5. The generated random integer is used as an index to extract one of the elements from bases array and assign it to current element of the seq array. Line (27)

After completion of the for-loop the seq array will be filled up with random characters of 'G', 'C', 'A', 'T'.

We call the function generateRandomSequence() by passing my_seq array as the argument (Line 10) and the function will generate the random DNA sequences in the array. We can print out the my_seq array using the Arrays.toString() method. (Line 11)

Next, we create another function getRNASeq() to convert all the character "T" found in the seq array to "U". This function also take one argument, seq array as input (Line 31). Within the function, there is a for-loop that will iterate through all the elements of the seq array and check if the current element is "T", replace it with "U" (Line 35 - 38).

Again, we call the function getRNASeq() by passing the my_seq array as argument (Line 15) and then print out the converted seq (RNA sequence) using the Arrays.toString()  method (Line 16).