Objectives

Students will be able to:

  • gain an appreciation of the String class and String class methods in Java

StringBot.txt

love

despair

love

hate

wrath

gluttony

love

sloth

greed

vanity

lust

love

vanity

love

hate

vanity

hate

String Bot

 

StringBot

Scenario

A StringBot reads text files.

It performs a variaty of operations on the data it finds.

These are the operations:

  1. It counts how many times the word love appears.
  2. It counts how many times the word hate appears.
  3. It counts how many times other words appear.
  4. It counts how many words have a length of 4
  5. It counts how many times the letter e appears in the file

Once the counting is done, it display the results.

Yes, StringBots are weird creatures!

Task

Open the Worksheet and follow the instructions.

You will need the following files:

Fill the Blanks

Now, can you fill the blanks in the following program:

public static void main(String[] args){

String[] fileContents = getFileContents();

int loveCount = getWordCount(fileContents, "love");

int otherCount = getOtherCount(fileContents)

displayResults(loveCount, otherCount);

}

public static int getWordCount( wordList, String word){

int count = 0;

for(int i = 0; i<wordList.; i++){

if(wordList[].equals(word)){

count = count + 1;

}

}

return count;

}

public static getOtherCount(String[] wordList){

int count = 0;

for(int i = 0; i<wordList.length; i++){

if(.equals("love") && .equals("hate")){

count = count + 1;

}

}

return ;

}

SmartStringBot

A SmartStringBot can do everything that a StringBot can do.

However, it also has an additional behaviour.

It can search for a word in the array and return the index of that word if found, or -1 if not found.

Using a Linear Search algorithm, implement this behaviour.

SuperStringBot

A SuperStringBot can do everything that a StringBot can do.

However, it also has another additional behaviour.

It can determine which word is last alphabetically and return it. For example, in the sample file above, the SuperStringBot would return wrath.

Research the String family's compareTo function and implement this additional behaviour.