Advertisement
advictoriam

Untitled

Feb 1st, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. public class Sentence implements Measurable
  2. {
  3.    /**
  4.       Constructs a sentence.
  5.       @param text the text of the sentence.
  6.    */
  7.    public Sentence(String text)
  8.    {
  9.       int n = text.length();
  10.       String punctuation = text.substring(n - 1, n);
  11.       words = text.substring(0, n - 1).split("\\s+");
  12.    }
  13.    
  14.    /**
  15.       Returns a word in this sentence.
  16.       @param the index of the word
  17.       @return the ith word or an empty string if i is < 0
  18.       or >= the number of words in this sentence
  19.    */
  20.    public String getWord(int i)
  21.    {
  22.       if (i >= 0 && i < words.length)
  23.          return words[i];
  24.       else
  25.          return "";
  26.    }
  27.    
  28.    public String toString()
  29.    {
  30.       String r = "";
  31.       for (String w : words)
  32.       {
  33.          if (r.length() > 0) r += " ";
  34.          r += w;
  35.       }
  36.       return r + punctuation;
  37.    }
  38.    
  39.    public double getMeasure(){return words.length;}
  40.  
  41.    // TODO: Do what it takes to implement the measurable interface
  42.    // so that the measure of a sentence is the number of words.
  43.    
  44.    private String[] words;
  45.    private String punctuation;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement