Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Sentence
- {
- private String text;
- /**
- Constructs a sentence.
- @param words the words in the sentence
- */
- public Sentence(String words)
- {
- text = words;
- }
- /**
- Count the number of spaces in the sentence.
- @return number of spaces
- */
- public int numSpaces()
- {
- int count = 0;
- for(int i = 0; i < text.length(); i++)
- {
- if(text.charAt(i) == ' '){count++;}
- }
- return count;
- }
- /**
- Count the number of vowels in the sentence.
- @return number of vowels
- */
- public int numVowels()
- {
- int count = 0;
- for(int i = 0; i < text.length(); i++)
- {
- if(Character.toString(text.charAt(i)).matches("[AEIOUaeiou]")){count++;}
- }
- return count;
- }
- /**
- Get the number of consonants in the sentence.
- @return the number of consonants
- */
- public int numCons()
- {
- return text.length() - this.numSpaces() - this.numVowels();
- }
- /**
- Get the ratio of vowels to consonants for words in the sentence.
- @return the ratio of vowels to consonants
- */
- public double ratioVowelsToCons()
- {
- return (double)numVowels() / (double)numCons();
- }
- // This method is used for checking your work. Do not modify it
- public static String check(String aSent)
- {
- Sentence aSentence = new Sentence(aSent);
- return String.format("%4.2f",aSentence.ratioVowelsToCons());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement