mercredi 25 février 2015

How do I create a runner for this program? (To test it)

I am writing a program that counts the amount of vowels in a string and if there are more vowels than consonants, it returns true. If not, false. This is a homework assignment, but the runner is not part of it. I want to test to see if my program works, which it should (hopefully!).


Now, for all our homeworks and labs, a runner is usually given. We were never taught how to write one, which is pretty bad since I'd like to check my code. I tried mimicking past runners, but I kept getting errors in my runner, some which read: "Cannot find symbol" How would I create a runner for this program?


Here is my code:



import static java.lang.System.*;
public class StringAnalyzer {
//String word;
public static boolean hasMoreVowelsThanConsonants(String word) {
// String word = string.toUpperCase();
int vowelCount;
int newLength;
for (vowelCount = 0; word.length() >= 1; vowelCount++) {

if (word.indexOf("A") != 1) {
vowelCount++;
} else if (word.indexOf("E") != 1) {
vowelCount++;
} else if (word.indexOf("I") != 1) {
vowelCount++;
} else if (word.indexOf("O") != 1) {
vowelCount++;
} else if (word.indexOf("U") != 1) {
vowelCount++;
}


newLength = (word.length() - vowelCount);

if (vowelCount > newLength) {
return true;
} else {
return false;
}

}
return true;
}
}


If you catch any problems, I'd always except advice :)


Here is my "runner" (its pretty bad, haha):


import static java.lang.System.*;



import static java.lang.System.*;


public class StringAnalyzerRunnerCDRunner {
public static void main(String[] args) {
hasMoreVowelsThanConsonants("DOG");
}
}


Thank you :)


Aucun commentaire:

Enregistrer un commentaire