Exercise 1: An OOP Translation of CountVowels — Possible Solution ==================================================================== THE PSEUDOCODE (IMPLIED BY THE PROBLEM STATEMENT) ------------------------------ ALGORITHM CountVowels(word) count <- 0 FOR EACH letter IN word IF letter IS A VOWEL THEN count <- count + 1 ENDIF ENDFOR RETURN count OOP TRANSLATION, FOLLOWING THIS CHAPTER'S OWN EvenSquareSummer TEMPLATE ------------------------------ class VowelCounter: def __init__(self, word): self.word = word def compute(self): vowels = set('aeiouAEIOU') count = 0 for ch in self.word: if ch in vowels: count += 1 return count VERIFYING THE COUNT FOR "algorithm" ------------------------------ Running VowelCounter("algorithm").compute() gives 3. Confirmed by hand: a-l-g-o-r-i-t-h-m a -> vowel (count=1) l -> not a vowel g -> not a vowel o -> vowel (count=2) r -> not a vowel i -> vowel (count=3) t -> not a vowel h -> not a vowel m -> not a vowel Final count: 3, matching the class's own computed result exactly. WHY THIS FOLLOWS THIS CHAPTER'S OWN OOP PATTERN ------------------------------ Just like EvenSquareSummer, the class's constructor stores the input data (the word) as an instance attribute, and a single compute() method contains the exact same loop-and-condition logic the imperative version would use - the OOP wrapper changes how the data and the operation on it are packaged together, not the underlying algorithm itself, exactly the distinction this chapter's own three-paradigm comparison established. WHY THIS WORKS AS AN ANSWER ------------------------------ The translation follows this chapter's own established OOP template structure precisely (constructor storing input, a compute() method holding the loop), and the resulting count is verified two independent ways - by actually running the class's own method, and by a manual letter-by-letter hand trace - confirming they agree.