Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- /**
- This class models a sentence.
- */
- public class Sentence
- {
- private ArrayList<String> words;
- /**
- Construct a sentence with a given text.
- @param text the sentence. The text ends in a
- punctuation mark which is not stored.
- */
- public Sentence(String text)
- {
- words = new ArrayList<String>();
- String word = "";
- for(int i = 0; i < text.length(); i++)
- {
- if(text.charAt(i) == ' ' || !(Character.isLetter(text.charAt(i))))
- {
- words.add(word);
- word = "";
- }
- else {word += text.charAt(i);}
- }
- }
- /**
- Get the ith word in the sentence.
- @return the ith word
- */
- public String getWord(int i)
- {
- return words.get(i);
- }
- /**
- Get the number of words in the sentence.
- @return the number of words
- */
- public int getCount()
- {
- return words.size();
- }
- // This method is used for checking your work. Do not modify it
- public static String check(String sent)
- {
- Sentence aSentence = new Sentence(sent);
- return aSentence.getWord(aSentence.getCount() - 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement