Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A DNA strand can be represented as a String of characters, where each character

ID: 3890997 • Letter: A

Question

A DNA strand can be represented as a String of characters, where each character represents one of four nucleotide molecules: Adenosine, Cytosine, Guanine, and Thymine. The DNA strand contains only these four letters, but may have an arbitrary length. The complement of a DNA strand consists of another sequence of nucleotide molecules, except this time, every nucleotide in the original strand is replaced with a specific different nucleotide. Every Adenosine nucleotide is replaced by a Thymine (and vice-

Explanation / Answer

Hi, here is the full code you can use, i have added comments to help you understand.

import java.util.Scanner;
public class HelloWorld{

public static void main(String []args){
String dna;

System.out.println("enter the DNA");
Scanner s= new Scanner(System.in);
dna=s.nextLine();
int[] visited=new int[dna.length()];//array to keep track of visited indexes
for(int i=0;i<dna.length();i++)
{
if(visited[i]!=1 && dna.charAt(i)=='A') //replacing all A with T but only not visited before
{

/* since string is immutable in java, convert ti char array and modify */
char[] myNameChars = dna.toCharArray();
myNameChars[i] = 'T';
dna = String.valueOf(myNameChars);
visited[i]=1;
}
/* all 3 if else before follow same logic as first one */
else if(visited[i]!=1 && dna.charAt(i)=='T')
{
char[] myNameChars = dna.toCharArray();
myNameChars[i] = 'A';
dna = String.valueOf(myNameChars);
visited[i]=1;
}
if(visited[i]!=1 && dna.charAt(i)=='C')
{
char[] myNameChars = dna.toCharArray();
myNameChars[i] = 'G';
dna = String.valueOf(myNameChars);
visited[i]=1;
}
else if(visited[i]!=1 && dna.charAt(i)=='G')
{
char[] myNameChars = dna.toCharArray();
myNameChars[i] = 'C';
dna = String.valueOf(myNameChars);
visited[i]=1;
}
}
  
System.out.println(dna);
}
}

Thumbs up if this was helpful, otherwise let me know in comments

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote