2. The Words class represents a list of words. You have been provided a skeleton for this class.
Complete the Words class using the block comments, Javadocs, and the following instructions.
Step has been done for you.
1. Add a List instance variable named words which will contain the list of words. Be sure to
specify the type of objects that will be stored in the List. Be sure that the instance variable is
private.
2. Complete the one parameter constructor to initialize the words instance variable. You will
need to create the ArrayList and add each word to it. Be sure to create a new Word object with
each String in the parameter array wordList and then add it to the ArrayList words. This would
be a great place to use a for-each loop. Your code should not generate any warnings.
3. Complete the countWordsWithNumChars method to return the number of words with lengths
of num characters.
4. Complete the removeWordsWithNumChars method to remove all words with lengths of num
characters.
5. Complete the countWordsWithNumVowels method to return the number of words that have
num vowels.
6. Complete the getReversedWords method to return an array of reversed words.
7. Complete the toString method to return the words as a String. You should utilize ArrayLists’s
toString method.
8. Uncomment the Words test code in WordTester and make sure that Words works correctly.
STARTER CODES AND SETUP
Word
/**
* Word.java 06/08/08
*
* @author - Jane Doe
* @author - Period n
* @author - Id nnnnnnn
*
* @author - I received help from ...
*
*
* Based on the Lab16c assignment
* Used with permission from Stacey Armstrong
* ? A+ Computer Science - www.apluscompsci.com
*/
3. /**
* A Word object represents a word.
*/
public class Word
{
/** The 5 vowels */
private static final String VOWELS = "AEIOUaeiou";
/** The word */
private String word;
/**
* Constructs a Word object.
* @param w the string used to initialize the word.
*/
public Word(String w)
{
word = w;
}
/**
* Gets the length of the word.
* @return the length (number of characters) of the word.
*/
public int getLength()
{
return word.length();
}
/**
* Gets the number of vowels in the word.
* @return the number of vowels (Characters in VOWELS in
* the word.
*/
public int getNumVowels()
{
int count = 0;
for (int i = 0; i < word.length(); i++)
{
String letter = word.substring(i, i + 1);
4. if (VOWELS.indexOf(letter) >= 0) //if letter is found in VOWELS, its a vowel
{
count++;
}
}
return count;
}
/**
* Gets the reverse word.
* @return a Word with this's
* letters reversed.
*/
public Word getReverse()
{
String reverse = ""; //Start the string as an empty string
for (int i = word.length() - 1; i >= 0; i--)
{
reverse += word.substring(i, i + 1); //build the string backward
}
return new Word(reverse); //must convert the string to a Word object to match
//the return type
}
/**
* Gives the string representing the word.
* @return the string representing the word.
*/
public String toString()
{
return word; // Replace
}
}
WORDS
/**
* Words.java 06/08/08
*
* @author - Jane Doe
5. * @author - Period n
* @author - Id nnnnnnn
*
* @author - I received help from ...
*
*
* Based on the Lab16c assignment
* Used with permission from Stacey Armstrong
* A+ Computer Science - www.apluscompsci.com
*/
import java.util.List; //You must import this to use the List datatype for the reference
import java.util.ArrayList;//Needed to instantiate an ArrayList
/**
* Words represents a list of Word objects.
*/
public class Words
{
/** The Words */
private List words;
/**
* Constructs a Words object.
* @param wordList the words for the Words object.
*/
public Words(String[] wordList)
{
words = new ArrayList();
//complete the constructor by using a loop to access each String in wordList,
//creating a Word object with each String and adding the Word object to the
//ArrayList.
}
/**
* Counts the number of words with num characters
* @param num the number of characters
* @return the number of words with num characters
*/
public int countWordsWithNumChars(int num)
6. {
return -999; // Replace
}
/**
* Removes the words with num characters
* from this
* @param num the number of characters of words to be removed
*/
public void removeWordsWithNumChars(int num)
{
}
/**
* Counts the number of words with num vowels
* @param num the number of vowels
* @return the number of words with num vowels
*/
public int countWordsWithNumVowels(int num)
{
return -999; // Replace
}
/**
* Makes an array containing reversed Word objects.
* Each of the items in the returned array is the reverse of an
* item in words. The items in the returned array
* are in the same order as in words.
* @return an array of the reverse objects of words.
*/
public Word[] getReversedWords()
{
return null; // Replace
}
/**
* Gives the string representing the words.
* @return the string representing the words.
*/
public String toString()
7. {
return null; // Replace
}
}
WORDTESTER
/**
* WordTester.java 06/08/08
*
* @author - Robert Glen Martin
* @author - School for the Talented and Gifted
* @author - Dallas ISD
*
* Based on the Lab16c assignment
* Used with permission from Stacey Armstrong
* A+ Computer Science - www.apluscompsci.com
*/
import java.util.Arrays;
/**
* Application to test the Word and
* Words classes.
*/
public class WordTester
{
public static void main(String[] args)
{
// Test Word
Word one = new Word("chicken");
System.out.println("Word one: " + one);
System.out.println("Number of chars: " + one.getLength());
System.out.println("Number of vowels: " + one.getNumVowels());
System.out.println("Reverse: " + one.getReverse());
System.out.println(" ");
Word two = new Word("alligator");
System.out.println("Word two: " + two);
System.out.println("Number of chars: " + two.getLength());
8. System.out.println("Number of vowels: " + two.getNumVowels());
System.out.println("Reverse: " + two.getReverse());
System.out.println(" ");
Word three = new Word("elephant");
System.out.println("Word three: " + three);
System.out.println("Number of chars: " + three.getLength());
System.out.println("Number of vowels: " + three.getNumVowels());
System.out.println("Reverse: " + three.getReverse());
System.out.println(" ");
// Test Words - Uncomment these lines of code to test the Words class
/*
Words test1 = new Words(new String[] {"one", "two", "three",
"four", "five", "six", "seven",
"alligator"});
System.out.println(" Words test1: " + test1);
System.out.println(" Number of words with 2 vowels: " +
test1.countWordsWithNumVowels(2));
System.out.println(" Number of words with 3 vowels: " +
test1.countWordsWithNumVowels(3));
System.out.println(" Number of words with 4 vowels: " +
test1.countWordsWithNumVowels(4));
System.out.println(" Number of words with 2 chars: " +
test1.countWordsWithNumChars(2));
System.out.println(" Number of words with 4 chars: " +
test1.countWordsWithNumChars(4));
System.out.println(" Number of words with 5 chars: " +
test1.countWordsWithNumChars(5));
test1.removeWordsWithNumChars(3);
9. System.out.println(" Words test1 after removing words " +
"with 3 chars: " + test1);
System.out.println(" Reversed words: " +
Arrays.toString(test1.getReversedWords()));
System.out.println(" ");
Words test2 = new Words(new String[] {"fun", "fly", "four", "six",
"times", "ten", "plus","eight"});
System.out.println("Words test2: " + test2);
System.out.println(" Number of words with 2 vowels: " +
test2.countWordsWithNumVowels(2));
System.out.println(" Number of words with 3 vowels: " +
test2.countWordsWithNumVowels(3));
System.out.println(" Number of words with 4 vowels: " +
test2.countWordsWithNumVowels(4));
System.out.println(" Number of words with 2 chars: " +
test2.countWordsWithNumChars(2));
System.out.println(" Number of words with 4 chars: " +
test2.countWordsWithNumChars(4));
System.out.println(" Number of words with 5 chars: " +
test2.countWordsWithNumChars(5));
test2.removeWordsWithNumChars(3);
System.out.println(" Words test2 after removing words " +
"with 3 chars: " + test2);
System.out.println(" Reversed words: " +
Arrays.toString(test2.getReversedWords()));
System.out.println(" ");
Words test3 = new Words(new String[] {"alligator", "chicken", "dog",
10. "cat", "pig", "buffalo"});
System.out.println("Words test3: " + test3);
System.out.println(" Number of words with 2 vowels: " +
test3.countWordsWithNumVowels(2));
System.out.println(" Number of words with 3 vowels: " +
test3.countWordsWithNumVowels(3));
System.out.println(" Number of words with 4 vowels: " +
test3.countWordsWithNumVowels(4));
System.out.println(" Number of words with 2 chars: " +
test3.countWordsWithNumChars(2));
System.out.println(" Number of words with 4 chars: " +
test3.countWordsWithNumChars(4));
System.out.println(" Number of words with 9 chars: " +
test3.countWordsWithNumChars(9));
test3.removeWordsWithNumChars(3);
System.out.println(" Words test3 after removing words " +
"with 3 chars: " + test3);
System.out.println(" Reversed words: " +
Arrays.toString(test3.getReversedWords()));
*/
}
} BlueJ: Words Studen Project Edit Tools View Help New Class... Word Words Compile
WordTester 8:23 AM 11/7/2016
Solution
words class
O/P
11. Word one: chicken
Number of chars: 7
Number of vowels: 2
Reverse: nekcihc
Word two: alligator
Number of chars: 9
Number of vowels: 4
Reverse: rotagilla
Word three: elephant
Number of chars: 8
Number of vowels: 3
Reverse: tnahpele
Words test1: [one, two, three, four, five, six, seven, alligator]
Number of words with 2 vowels: 5
Number of words with 3 vowels: 0
Number of words with 4 vowels: 1
Number of words with 2 chars: 0
Number of words with 4 chars: 2
Number of words with 5 chars: 2
Words test1 after removing words with 3 chars:
[three, four, five, seven, alligator]
Reversed words:
[eerht, ruof, evif, neves, rotagilla]
Words test2: [fun, fly, four, six, times, ten, plus, eight]
Number of words with 2 vowels: 3
Number of words with 3 vowels: 0
Number of words with 4 vowels: 0
Number of words with 2 chars: 0
Number of words with 4 chars: 2
Number of words with 5 chars: 2
Words test2 after removing words with 3 chars:
[four, times, plus, eight]
Reversed words:
[ruof, semit, sulp, thgie]
Words test3: [alligator, chicken, dog, cat, pig, buffalo]
12. Number of words with 2 vowels: 1
Number of words with 3 vowels: 1
Number of words with 4 vowels: 1
Number of words with 2 chars: 0
Number of words with 4 chars: 0
Number of words with 9 chars: 1
Words test3 after removing words with 3 chars:
[alligator, chicken, buffalo]
Reversed words:
[rotagilla, nekcihc, olaffub]
If you have any doubt you can ask me without any hesitation.