class R16_R22 { // R1.6 revisited -- see also 4/10 lecture sketch // upper-case dna strand complementary to $in$. case is ignored. // example: $System.out.println(complement("taCaGCgATAcgtg"));$ public static String complement(String in) { in = in.toUpperCase(); // added [4/11] String out = ""; int i = 0; while (i < in.length()) { char c = in.charAt(i); if (c == 'A') { out = out + 'T'; // or: out = out + "T"; } else if (c == 'T') { out = out + 'A'; // or: out = out + "A"; } else if (c == 'G') { out = out + 'C'; // or: out = out + "C"; } else { out = out + 'G'; // or: out = out + "G"; } i = i + 1; } return out; // NOTE: $out +=$ can be used instead of $out = out +$. } /* matlab equivalent: function out = complement(in) % out = complement(in): return uppercase dna strand complementary to $in$. % case is ignored. example: $complement('taCaGCgATAcgtg')$ out = ''; i = 1; while i <= length(in) c = in[i]; if c == 'A' out = [out 'T']; elseif c == 'T' out = [out 'A']; elseif c == 'G' out = [out 'C']; else out = [out 'G']; end i = i+1; end */ // R2.2 revisited // "$isbn$ is a valid ISBN string?". ignore case. public static boolean checkisbn(String isbn) { if (isbn.length() != 10) { return false; } int want = -1; // desired value of checksum if (isbn.charAt(9) == 'X' || isbn.charAt(9) == 'x') { want = 10; } else if ('0' <= isbn.charAt(9) & isbn.charAt(9) <= '9') { want = isbn.charAt(9) - '0'; } else { // last character is not a digit, $'x'$, or $'X'$ -- bad! return false; } int i = 0; int star = 0; // inv: star = sum of first $i$ out of all 9 terms of checksum while (i != 9) { if ('0' <= isbn.charAt(i) & isbn.charAt(i) <= '9') { star += (isbn.charAt(i) - '0') * (i+1); } else { // not a digit -- bad! return false; } i = i+1; } return star % 11 == want; } /* matlab equivalent: function ok = checkisbn(isbn) % ok = checkisbn(isbn): return "$isbn$ is a valid ISBN string?". ignore case. if length(isbn) ~= 10 ok = logical(0); return end if isbn(10) == 'X' | isbn(10) == 'x' want = 10; % desired value of checksum elseif ('0' <= isbn(10) & isbn(10) <= '9') want = isbn(10) - '0'; % desired value of checksum else % last character is not a digit, $'x'$, or $'X'$ -- bad! ok = logical(0); return end i = 0; star = 0; % inv: star = sum of first $i$ out of all 9 terms of checksum while i ~= 9 i = i+1; if ('0' <= isbn(i) & isbn(i) <= '9') star = star + (isbn(i) - '0') * i; else % not a digit -- bad! ok = logical(0); return; end end ok = rem(star, 11) == want; */ // sample calls to $complement$ and $checkisbn$ // run $complement$ and $checkisbn$ on some sample test cases public static void main(String[] args) { String[] dna = new String[] { "taCaGCgATAcgtg", "aTGTcgcTATGcAC", "", "a", "G", "AtTaCatGagTagTatAct" }; int i = 0; while (i < dna.length) { System.out.println("complement of `" + dna[i] + "' is `" + complement(dna[i]) + "'"); i += 1; } String[] isbn = new String[] { "0812544501", "156389016X", "812544501", "0712544501" }; i = 0; while (i < isbn.length) { System.out.println("`" + isbn[i] + "' is valid ISBN number: " + checkisbn(isbn[i])); i += 1; } } }