/** Yields: a String that contains each capital Letter (in 'A'..'Z') whose representation is prime */
public static String primeChars() {
String s = "";
// inv: s contains each capital in "A'..c-1 whose representation is prime
for (char c = 'A'; c <= 'Z'; c=(char)(c+1)) {
if (Loops.isPrime((int)c) == true) {
s= s+1;
}
}
// s contains each capital in 'A' ..'Z' whose rep is a prime
return s;
}
isPrime()
will return you boolean
, since you can directly call if(true)
or if(false)
, then no need to double comparison - Adil Soomro 2012-04-04 05:27
== true
. It's good advice, but to benefit human readers - Matthew Flaschen 2012-04-04 05:28
It should be:
s = s + c;
You want to concatenate the character if it's prime.
Maybe you want to write
s = s + c
Then do you want evaluate isPrime on char ASCII code or on char position? That is do you want 'A' value is 65 (the ASCII code) or 1 (because A is the 1 letter)? In the first case fix
s = s + c
it is enough, but in second you need change also
if (Loops.isPrime((int)c)==true) {
and do not pass "c" but
if (Loops.isPrime( (int)c - (int)'A' + 1 )==true) {