In: Computer Science
In Java
in previous question #1, you created a method called isDNAvalid() in the DNA class. You probably looped through the entire sequence one character at a time (using charAt() method of the String class), and check if the character is 'A', 'T', 'G', or 'C'. If it is not one of these four characters, the sequence contains an invalid base. Please re-create the isDNAvalid() method using a character pattern and the regular expression to validate the DNA sequence.
previous question 1 was:
In object-oriented programming design, such as in Java, your program often consists of two or more classes (often written in different .java source files). One class contains the main() method to generate the console output, while the other classes may just contain properties or methods to process the data. Using this approach to design a DNA sequence class called DNA with a validation method called IsDNAvalid() to validate if the DNA sequence contains any invalid letter (not A, C, G, T). You can use the charAt() method of the String class to loop through a sequence string to check if each letter is valid. Design another class which contains the main() method, and create a DNA object inside the main method, then call the IsDNAvalid()method to validate the DNA sequence input.
this is the code used for previous question #1
import java.util.Scanner;
import java.lang.*;
public class hwk2_1 {
public static void main(String[] args)
{
System.out.println("Enter the DNA Sequence");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
System.out.println(input.length());
DNA d = new DNA(input);
if(d.IsDNAvalid())
{
System.out.println("This is a valid DNA Sequence");
char base1 = 'G';
char base2 = 'C';
System.out.println( "Length of DNA sequence is = " + d.getSize()
);
System.out.println( "Basecount for " + base1 + " = " +
d.baseCount(base1) );
System.out.println( "Basecount for " + base2 + " = " +
d.baseCount(base2) );
float gc_content = ((float)(d.baseCount(base1) + (float)
d.baseCount(base2))*100)/(d.getSize());
System.out.println( "GC content = " +
Math.round(gc_content*100.0)/100.0 );
}
else
{
System.out.println("This is an invalid DNA Sequence");
}
}
}
public boolean IsDNAvalid() { return base.matches("^[ATCG]+$"); }
************************************************** You have not given your DNA class.. You should just replace the method i gave on the DNA class. Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.