CS174: OOP - Drills - Vowel Counting

Developed by Professor Tralie and Professor Mongan.


Exercise Goals

The goals of this exercise are:
  1. To do proper string indexing
  2. To use conditional statements appropriately
  3. To keep track of auxiliary variables within loops
Fill in the method countVowels to count the number of vowels (both lowercase and uppercase, not including y or Y) in a string. Recall that the method charAt of the String class returns a character at a particular index, and, like arrays, strings are zero-indexed. For instance, if

String s = "I love CS";

, then

s.charAt(3)

returns the character o.

Recall also that the length() method of the String class returns the total number of characters in the string.

Enter your Ursinus netid before clicking run. This is not your ID number or your email. For example, my netid is ctralie (non Ursinus students can simply enter their name to get this to run, but they won't get an e-mail record or any form of credit)

Netid
Clicking Run below will check your work and, if it passes, will submit your work automatically. You must be connected to the VPN for submission to be successful! You will receive a copy of your code via e-mail, so you'll know that it was submitted if you receive that e-mail!

StringUtils.java

public class StringUtils { /** * Count the number of vowels in a string, regardless * of case * * @param s The string to examine * @return The number of vowels in the string */ public static int countVowels(String s) { int numVowels = 0; // TODO: Fill this in return numVowels; } }

Tester.java

public class Tester { public static void main(String[] args) { String s1 = "I love CS"; int vowels1 = StringUtils.countVowels(s1); String s2 = "The quick brown fox jumped over the lAzy dOg"; int vowels2 = StringUtils.countVowels(s2); String s3 = "aEIOu AeioU"; int vowels3 = StringUtils.countVowels(s3); System.out.print(vowels1 + "." + vowels2 + "." + vowels3); } }
Tester.main(null);

Output