0

Thank you everyone for your answers ! One more question: how do i print out the boolean value? System.out.println(goodBase) does not seem to work

public class Dna {
      public static void main(String[] args){
      aBase('A');
     }

      public static boolean aBase (char c) {                 
        char [] charArray = { 'A', 'G', 'C', 'T' };
        boolean goodBase;

        if (c == 'A' || c == 'G' || c == 'C' || c == 'T') 
        {
          return true;     
        } 
        else 
        {
          return false;
        }
      }  
    }

Thanks !

3
  • How is the value of char defined? Commented Oct 2, 2015 at 22:19
  • Do i have to add something like "char nucleotide;" even though i have "char [] charArray = { 'A', 'G', 'C', 'T' };" ? And even if I add it it still does not work. The errors found highlight the "if" line saying ".class expected" Commented Oct 2, 2015 at 22:26
  • What is this this method trying to do? I could imagine a few different things. Are you trying to see if all of the strings in arg[] consist only of AGCT? Or do you want to pass in a single string, and have the method return whether it only contains AGCTs? Or something else? Commented Oct 2, 2015 at 22:34

3 Answers 3

2

The code is working for me if you use it in the right environment. I have attached a complete functioning sample below:

public class Main {

    // create a Test Method
    public static boolean test(char c) {
        if (c == 'A' || c == 'G' || c == 'C' || c == 'T') {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {

        // create some sample data
        String sample = "AGCTEDHI";

        // test
        for (int i = 0; i < sample.length(); i++) {
            char current = sample.charAt(i);
            System.out.println(current + " is " + test(current));
        }

    }
}

Output:

A is true
G is true
C is true
T is true
E is false
D is false
H is false
I is false
Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean by "the right environment"? Also, note that any time you have if (a) return true; else return false you can just do return a. So in this case: return c == 'A' || c == 'G' || c == 'C' || c == 'T'.
In the right environment, as in if you build a method which takes a charas an argument. I think the OPs actual problem is not the logic behind his method but rather how he can implement and actually use it - thus the sample I built above.
Fwiw, that's not what I think people typically mean by "environment." The environment usually refers to things like the OS, hardware, JVM version -- the "environment" in which a piece of code is executed, not the code itself. I'm not quite sure what the OP is trying to accomplish, so it's hard for me to judge whether your code accomplishes it. :)
0

I think that is so:

char [] charArray = { 'A', 'G', 'C', 'T' };
if (base == charArray[0] || base == charArray[1] || 
        base == charArray[2] || base == charArray[3])
  return true; // a good base
return false;

Comments

0

First of all, there is no declaration of a char named variable in the aGoodBase method you are checking in the if statement.

And secondly, and this is the real deal, such variable could never exist since char is a reserved keyword in Java (like byte, int, long, public, strictfp etc.) , and it can be used only to declare a variable as a primitive char type.

This answers why DnaTest is not compiling.

Now, let's consider a possible solution for your answer:

    public class DnaTest {

        private final static char [] baseArray = {'A', 'C', 'G', 'T'};

        // checks whether String s only contains the right base elements or not
        public static boolean aGoodBase(String s){
                String s1 = s.toUpperCase(); // just ensuring case insensitive checks
                for (int i=0; i<s1.length(); i++) {
                        char check = s1.charAt(i);
                        if (baseArray[0] != check &&
                              baseArray[1] != check &&
                              baseArray[2] != check &&
                              baseArray[3] != check) {
                                   // at least one char of s is not in baseArray
                                   return false;
                                }
                }
                return true;
        }

        public static void main(String[] args) {
                // should be true
                System.out.println("Does GATTACA have aGoodBase? " + aGoodBase("GATTACA"));
                // should be false
                System.out.println("Does MORROW have aGoodBase? " + aGoodBase("MORROW"));
                // should be true, the check on the base is case insensitive
                System.out.println("Does GaTTaca have aGoodBase? " + aGoodBase("GaTTaca"));
                // should be false
                System.out.println("Does GATMOR have aGoodBase? " + aGoodBase("GATMOR"));

        }
    }

Output:

Does GATTACA have aGoodBase? true
Does MORROW have aGoodBase? false
Does GaTTaca have aGoodBase? true
Does GATMOR have aGoodBase? false

Now aGoodBase method takes a String and returns true if s contains only a combination of the four basic elements declared in the baseArray array. I've taken the liberty to return true even when the base elements are not in capital letters (as in the third example of the main: GaTTaca).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.