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,
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; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
