60 678
Assignments Done
Successfully Done
In April 2018

Answer to Question #57454 in Java | JSP | JSF for Aine

Question #57454
Write a method in Java called FirstDNAMatch(shortDNA, longDNA) that receives 2 strings with the letters that have to do with the four main nucleobases found in the nucleic acids DNA (A for Adenosine, G for Guanine, C for Cytosine and T for Thymine). The method must return the index of the first spot where the first DNA chain can connect to the second or -1 if there is no spot for the 2 chains to match. A matches with T (and other way round) and C matches with G (and other way round). For instance, Chain1: T A A C G G T A C G and Chain2: G C C A match on the index 3.
Expert's answer
import java.util.Scanner;

public class Main {
public static int FirstDNAMatch(String shortDNA, String longDNA) {
shortDNA = shortDNA.replaceAll("\\s", "").replaceAll("T", "A").replaceAll("G", "C"); //removing whitespaces, replacing "T" with "A" and "G" with "C"
longDNA = longDNA.replaceAll("\\s", "").replaceAll("T", "A").replaceAll("G", "C"); //removing whitespaces, replacing "T" with "A" and "G" with "C"
return longDNA.indexOf(shortDNA); //searching for the match

public static void main(String[] args) {
//manual testing
Scanner sc = new Scanner(System.in);
System.out.println("Please enter longDNA: ");
String longDNA = sc.nextLine();
System.out.println("Please enter shortDNA: ");
String shortDNA = sc.nextLine();
System.out.println("Match: " + FirstDNAMatch(shortDNA, longDNA));
//predefined test
longDNA = "T A A C G G T A C G";
shortDNA = "G C C A ";
System.out.println("Match for predefined strings: " + FirstDNAMatch(shortDNA, longDNA));


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

Privacy policy Terms and Conditions