DNA Project

Welcome to the Get Help category!

This is where you can ask questions about your code. Some important things to remember when posting in this category :slight_smile:

  • Learn how to ask a good question and get a good answer!
  • Remember to include a link to the exercise you need help with!
  • If someone answers your question, please mark their response as a solution :white_check_mark:
  • Once you understand a new concept, come back and try to help someone else! DNA Project[quote=“tera0256702019, post:1, topic:549095, full:true”]
    Welcome to the Get Help category!

This is where you can ask questions about your code. Some important things to remember when posting in this category :slight_smile:

  • Learn how to ask a good question and get a good answer!
  • Remember to include a link to the exercise you need help with!
  • If someone answers your question, please mark their response as a solution :white_check_mark:
  • Once you understand a new concept, come back and try to help someone else! DNA Project
    [/quote]

In the DNA project, shouldn’t there be another condition that requires stop codon(TGA) to come after start codon(ATG)?For instance, the sequence “TGACGCATG” satisfies conditions 1, 2 and 3 but is not a protein?

@tera0256702019, I think I know what you are talking about, however clarifying it won’t hurt. I am assuming that you misunderstood the conditions. The stop/end codon needs to be at the absolute end of the chain. Of course, I am probably wrong. It would be nice if can be more clear on what you mean.

//checks for protein in DNA strand
public class DNA{
  public static void main(String[] args){
    String dna1="ATGCGATACGCTTGA";
    String dna2="ATGCGATACGTGA";
    String dna3="ATTAATATGTACTGA";
    String dna4="TGACGCATG";
    String dna=dna4;
    int length=dna.length();
    int start=dna.indexOf("ATG");
    int stop=dna.indexOf("TGA");
    

    System.out.println("Length: "+length);
    System.out.println("Start: "+start);
    System.out.println("Stop: "+stop);
    if(start != -1 && stop!=-1 &&  (stop-start)%3==0){
      System.out.println("Condition 1 and 2 and 3 are satisfied");
      String protein=dna.substring(start,stop+3);
      System.out.println("Protein: "+protein);

    }else{System.out.println("No protein.");}

  }
}

The above code throws an ‘StringIndexOutOfBoundsException’ .The code works fine if the conditional were: if(start != -1 && stop>=start+3 && (stop-start)%3==0) This ensures stop codon comes after start codon ,a condition that the original conditional does not cover.

I think I understand now, this is a very interesting alternative to the usual way of completing this exercise. And yes, the stop codon should come after the start codon.

I believe the traditional way would also factor that in.

I used an ArrayList to store the information, and I used this to satisfy the conditions.

if(sample.substring(0, 3).equals("ATG")){
          if(sample.substring(length - 3).equals("TGA"))

because the substring(0, 3) finds the substring from the beginning to the 3rd letter, it is at the absolute front. Then, you have substring(length - 3), which finds the value at the 3rd to the last letter to the last letter to the end, it is at the absolute back. The only exception would be if the string is only 3 letters long, which then it cannot satisfy both situations.

I respect you finding another way of solving this :+1::+1:, but I believe the traditional way solves the problem as well.