2.1.1 Java String Exercise: Parse This

You will need to use some the Java: Strings method to complete this assignment.

To parse something is to break it down into smaller parts.  For example to parse a sentence into words, you break out each word in the sentence.  That is your assignment.  To start, do not include punctuation in your sentence.

Object #1 Parser

This object will have two methods:
  1. return the first word of a phrase
  2. remove the first word of a phrase
Use this to strat your object:
public class Parser
{
    
    /**
     * Constructor for objects of class Parser
     */
    public Parser()
    {
        
    }

    /**
     * Returns the first word of a string
     * 
     * @param  phrase   a phrase that will used as input
     * @return     the fisrt work in parameter phrase
     */
    public String firstWord(String phrase)
    {
        String firstWord ="";
        // your code to extract the first word of the phrase goes here
        return firstWord;
    }
    
    /**
     * removes the first word of a string
     * 
     * @param  phrase   a phrase that will used as input
     * @return     the phrase less the first word
     */
    public String removeFirstWord(String phrase)
    {
        String newPhrase ="";
        // your code to remove the first word of the phrase goes here
        return newPhrase;
    }
}

Object #2 Sentence

NEW CONCEPT:  parameters are passed by value, not by reference. ie. methods only use the values of parameters. The original variable value does not change (yet).

This object will:
  • Prompt the user for a sentence
  • request the Praser to return the fisrt word
  • display each word on a new line
  
Non critical Input errors to check.  These errors will be caught by your program and produce the correct results.
  • multiple leading and trailing spaces 
  • multiple spaces between words

Modifications 

DO NOT ATTEMPT until you have the first part working
  • display a word count
  • remove any special standard punctuation from the words 
  • determine the average length of the words
  • count the number of characters used for punctuation

Modifications Part 2

ALSO SEE:  Java: 1D Arrays

DO NOT ATTEMPT until you have the above modifications working
  • using an array, count the number of 1 letter words, 2 letters words ... all the way up to words with 10 or more letters in them. 
  • using an array, count the number of unique words in the sentence.  Assume that there are not more than 15 unique words in a sentence.  Make sure  that the case is ignored.
This code will append a new word to an array:

Output the all stats in the Sentence class.

Read text from a file

Add a new class to this project FileReader

This modification will allow a text file to be read from the computer and parsed.

Comments