Jump to content


Photo

I Need Java Help


  • Please log in to reply
1 reply to this topic

#1 arena_anth

arena_anth

    X-S Enthusiast

  • Members
  • 5 posts

Posted 26 February 2005 - 12:42 AM

I need to 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.

public class MyString {

public MyString() {
// Constructor -- do nothing.
}

// THE METHODS YOU WRITE

// This method counts the words in the String line
// words are separated by characters that are not letters or digits
public int wordCount(String line){
int count = 0;

return count;
}

// This method counts the number of times the word appears in the string line
// The method looks for the exact occurence of the word.
// The method ignores any characters that are not digits or letters
// in matching word in the string line.
public int wordFrequency(String word, String line){
int count = 0;

// Be sure to handle the error cases here.

return count;
}
}

This next code is the testing:
public static void main(String[] args) {
// TODO code application logic here

MyString s = new MyString();

int count;

// Tests of method wordCount

System.out.println("Testing method wordCount");

count = s.wordCount("This is a test");
System.out.println(count + " should be 4");

count = s.wordCount("This1 puts it 2 the test!\n 4ever");
System.out.println(count + " should be 7");

count = s.wordCount("\t \n \r");
System.out.println(count + " should be 0");

count = s.wordCount("this-is-a-test.");
System.out.println(count + " should be 4");

count = s.wordCount("");
System.out.println(count + " should be 0");


// Tests of method wordFrequency

System.out.println("\nTesting method wordFrequency");

count = s.wordFrequency("is", "This is a test");
System.out.println(count + " should be 1");

count = s.wordFrequency("number", "1number = number1");
System.out.println(count + " should be 0");

count = s.wordFrequency("fuZZy", "Fuzz-Fuzzy-wuzzy was a bear?\n");
System.out.println(count + " should be 1");

count = s.wordFrequency("\n", "MARY, marry Oh Mary!");
System.out.println(count + " should be -1");

count = s.wordFrequency("", "1number = number1");
System.out.println(count + " should be -1");
}

}

#2 RPS_Man

RPS_Man

    X-S Enthusiast

  • Members
  • 1 posts

Posted 26 February 2005 - 12:49 AM

http://www.ringsurf....fee_Makers.html




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users