Programming Assignment 3

Due February 24, before 9:30am

Program Description

In this assignment, you will learn to use the Character wrapper class, and also methods in the String class.   You will also need to use for and while loops. You will develop a method that will count the number of words in a given string, and another method that will count the number of occurances of a particular word within a given string.

What You Do

It is assumed that you are using NetBeans to develop your code for this assignment.  Create a new NetBeans project that is a "General" "Java Application". Name your NetBeans project program3.

Download the source for the Main.java  and
MyString.java class, and store these in the program3/src/program3 directory.

Using NetBeans,
you are to implement two methods for the class MyString:
  1. public int wordCount(String line) 
This method counts the number of words in the string line Words are separated by characters that are not letters or digits.  To determine if a character is a letter or a digit, you must use the methods isDigit and isLetter in the wrapper class Character
  1. public int wordFrequency(String word, String line)
This method counts the number of times word occurs in the string line.  Note that it is looking for the exact occurence of the word. For instance count  = 1 in the following:

int count = wordFrequency("is", "This is a test");

The method  is to be case insensitive.  For instance count = 2 in the following:

int count = wordFrequency("mary", "MARY, marry Oh Mary!");

The method
ignores any characters that are not digits or letters in matching word in the string line.  Note that it does not ignore digits. For instance count = 0 in the following:

int count = wordFrequency("number", "1number = number1");

Error Checking

Your implementation of the method wordFrequency should check for the following error cases, and return -1 if either is true:
  • The string word contains more than one word
  • The string word contains no word

Testing

You are responsible for testing your methods.  To help you in testing, a few example tests are provided in the file Main.java.  Other tests will be conducted in grading -- so test your methods carefully before submitting your code.  Be sure to test that your methods work in all the error cases identified above.
     

What You Submit

Submit your  file called MyString.java in a directory named 03 via gsubmit on csa2.bu.edu.

Under no circumstances will late assignments be accepted.

Grading Criteria



20% Submitted code compiles without errors
20% wordCount
returns the correct value for the cases given in Main.java
20% wordCount returns the correct value for other cases given at testing time.
20% wordFrequency returns the correct value for the cases given in Main.java
20% wordFrequency returns the correct value for other cases given at testing time.