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

Hello, I am currently programming this exercise (The Art and Science of Java by

ID: 3877563 • Letter: H

Question

Hello, I am currently programming this exercise (The Art and Science of Java by Eric Robers, Chapter 8, Excercise No 20) for my programming course, but the test, from my course where i upload it, sents me back this error code:

There were 1 failures: 1) Test howAboutWrongInputHoney returned: java.lang.AssertionError: Stop throwing random stuff/exceptions at me :O All I want is an IllegalArgumentException for wrong input, not NullPointerException. More or less ;)

This is my code:

package programming.set10.dna;



public class DNAMatcher {

   // 'A' (adenosine), 'C' (cytosine), 'G' (guanine), or 'T' (thymine).

   String input = "";

   /**
   * Checks if its a dna string.
   *
   * @param input
   *            is it a dna string?
   * @throws illegalArgumentException
   *             if {@code input} is {@code null}, empty, or contains characters
   *             other than A, C, G, and T.
   */
   public DNAMatcher(String input) {
       this.input = input;
      
       if (input == null) {
           throw new IllegalArgumentException();
       }
       if (input.equals("")) {
           throw new IllegalArgumentException();
       }
       if (!isValid(input)) {
           throw new IllegalArgumentException();
       }

   }

   /**
   * Returns the index of the first position in the base DNA string where
   * candidateDNA can bind, if any.
   *
   * @param candidateDNA
   *            the DNA string to try to bind to the base DNA.
   * @return index of the first binding position or {@code -1} if the candidate
   *         DNA string cannot bind to the base string.
   * @throws IllegalArgumentException
   *             if {@code candidateDNA} is {@code null}, empty, or contains
   *             characters other than A, C, G, and T.
   */

   public int findFirstBindingPosition(String candidateDNA) {

       if (candidateDNA.equals("") || candidateDNA == null || isValid(candidateDNA) == false) {
           throw new IllegalArgumentException();
       }

       int i = 0;
       int index = -1;

       for (int j = 0; j < (input.length()); j++) {

           char inputDNA = input.charAt(j);
           char shortDNA = candidateDNA.charAt(i);

           if (!match(inputDNA, shortDNA)) {
               if (i > 0) {
                   j = j - i;
                   i = i - i;
               }

           }
           if (match(inputDNA, shortDNA)) {

               i = i + 1;
           }

           if (i == (candidateDNA.length() - 1)) {
               index = (j - i) + 1;
               break;
           }
       }

       return index;
   }

   /**
   * This method will check, if the characters will match with each other.
   *
   * @param inputDNA
   *            the char in the input string
   * @param shortDNA
   *            the char in the candidateDNA string
   * @return true if match or false if not match.
   */
   private boolean match(char inputDNA, char shortDNA) {

       Character.toUpperCase(inputDNA);
       Character.toUpperCase(shortDNA);

       boolean matchi = false;

       switch (inputDNA) {

       case 'A':
           if (shortDNA == 'T')
               matchi = true;
           break;
       case 'T':
           if (shortDNA == 'A')
               matchi = true;
           break;
       case 'G':
           if (shortDNA == 'C')
               matchi = true;
           break;
       case 'C':
           if (shortDNA == 'G')
               matchi = true;
           break;
       default:
           matchi = false;
           break;
       }
       return matchi;
   }

   /**
   * Checks, if the entered string is a valid dna string.
   *
   * @param inputbums
   *            the char to check
   * @return true, if valid string and false if not.
   */
   private boolean isValid(String inputbums) {

       boolean valid = true;

       for (int i = 0; i < inputbums.length(); i++) {
           char inputding = inputbums.charAt(i);
           if (inputding != 'A' && inputding != 'T' && inputding != 'G' && inputding != 'C') {
               valid = false;
           }
       }

       return valid;
   }
}

Explanation / Answer

//In single thread environment your code is working fine.You need to synchronize in multi thread environment.

public class DNAMatcher {

   // 'A' (adenosine), 'C' (cytosine), 'G' (guanine), or 'T' (thymine).

   String input = "";

   /**

   * Checks if its a dna string.

   *

   * @param input

   * is it a dna string?

   * @throws illegalArgumentException

   * if {@code input} is {@code null}, empty, or contains characters

   * other than A, C, G, and T.

   */

   public DNAMatcher(String input) {

   this.input = input;

  

   if (input == null) {

   throw new IllegalArgumentException();

   }

   if (input.equals("")) {

   throw new IllegalArgumentException();

   }

   if (!isValid(input)) {

   throw new IllegalArgumentException();

   }

   }

   /**

   * Returns the index of the first position in the base DNA string where

   * candidateDNA can bind, if any.

   *

   * @param candidateDNA

   * the DNA string to try to bind to the base DNA.

   * @return index of the first binding position or {@code -1} if the candidate

   * DNA string cannot bind to the base string.

   * @throws IllegalArgumentException

   * if {@code candidateDNA} is {@code null}, empty, or contains

   * characters other than A, C, G, and T.

   */

   public int findFirstBindingPosition(String candidateDNA) {

   if (candidateDNA.equals("") || candidateDNA == null || isValid(candidateDNA) == false) {

   throw new IllegalArgumentException();

   }

   int i = 0;

   int index = -1;

   synchronized (input) {

      

  

   for (int j = 0; j < (input.length()); j++) {

   char inputDNA = input.charAt(j);

   char shortDNA = candidateDNA.charAt(i);

   if (!match(inputDNA, shortDNA)) {

   if (i > 0) {

   j = j - i;

   i = i - i;

   }

   }

   if (match(inputDNA, shortDNA)) {

   i = i + 1;

   }

   if (i == (candidateDNA.length() - 1)) {

   index = (j - i) + 1;

   break;

   }

   }

   }

   return index;

   }

   /**

   * This method will check, if the characters will match with each other.

   *

   * @param inputDNA

   * the char in the input string

   * @param shortDNA

   * the char in the candidateDNA string

   * @return true if match or false if not match.

   */

   private boolean match(char inputDNA, char shortDNA) {

   Character.toUpperCase(inputDNA);

   Character.toUpperCase(shortDNA);

   boolean matchi = false;

   switch (inputDNA) {

   case 'A':

   if (shortDNA == 'T')

   matchi = true;

   break;

   case 'T':

   if (shortDNA == 'A')

   matchi = true;

   break;

   case 'G':

   if (shortDNA == 'C')

   matchi = true;

   break;

   case 'C':

   if (shortDNA == 'G')

   matchi = true;

   break;

   default:

   matchi = false;

   break;

   }

   return matchi;

   }

   /**

   * Checks, if the entered string is a valid dna string.

   *

   * @param inputbums

   * the char to check

   * @return true, if valid string and false if not.

   */

   private boolean isValid(String inputbums) {

   boolean valid = true;

   synchronized (inputbums) {

   for (int i = 0; i < inputbums.length(); i++) {

   char inputding = inputbums.charAt(i);

   if (inputding != 'A' && inputding != 'T' && inputding != 'G' && inputding != 'C') {

   valid = false;

   }

   }

   }

   return valid;

   }

}

//Check with this code if it also throw unacceptable error. please plovide me test cases

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