Advertisement
advictoriam

Untitled

Jan 28th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. public class Sentence
  2. {
  3.    private String text;
  4.  
  5.    /**
  6.       Constructs a sentence.
  7.       @param words the words in the sentence
  8.    */
  9.    public Sentence(String words)
  10.    {
  11.       text = words;
  12.    }
  13.  
  14.    /**
  15.       Count the number of spaces in the sentence.
  16.       @return number of spaces
  17.    */
  18.    public int numSpaces()
  19.    {
  20.       int count = 0;
  21.       for(int i = 0; i < text.length(); i++)
  22.       {
  23.          if(text.charAt(i) == ' '){count++;}
  24.       }
  25.       return count;
  26.    }
  27.  
  28.    /**
  29.       Count the number of vowels in the sentence.
  30.       @return number of vowels
  31.    */
  32.    public int numVowels()
  33.    {
  34.       int count = 0;
  35.       for(int i = 0; i < text.length(); i++)
  36.       {
  37.          if(Character.toString(text.charAt(i)).matches("[AEIOUaeiou]")){count++;}
  38.       }
  39.       return count;
  40.    }
  41.  
  42.    /**
  43.       Get the number of consonants in the sentence.
  44.       @return the number of consonants
  45.    */
  46.    public int numCons()
  47.    {
  48.       return text.length() - this.numSpaces() - this.numVowels();
  49.    }
  50.  
  51.    /**
  52.       Get the ratio of vowels to consonants for words in the sentence.
  53.       @return the ratio of vowels to consonants
  54.    */
  55.    public double ratioVowelsToCons()
  56.    {
  57.       return (double)numVowels() / (double)numCons();
  58.    }
  59.    
  60.  
  61.    // This method is used for checking your work. Do not modify it
  62.  
  63.    public static String check(String aSent)
  64.    {
  65.       Sentence aSentence = new Sentence(aSent);
  66.       return String.format("%4.2f",aSentence.ratioVowelsToCons());
  67.    }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement