Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name: Sammy Samkough
- // Prog: Word & Words
- // Spec: Complete methods for a Word and Words Class that consists of an ArrayList of Word objects.
- public class Word
- {
- private String word;
- public Word()
- {
- word = "";
- }
- public Word(String wrd)
- {
- word = wrd;
- }
- public void setWord(String wrd)
- {
- word = wrd;
- }
- public String getWord()
- {
- return word;
- }
- public int getNumVowels()
- {
- int count = 0;
- String vowels = "AEIOUaeiou";
- for (int i = 0; i < word.length(); i++)
- {
- if (vowels.indexOf(word.charAt(i)) >= 0)
- {
- count++;
- }
- }
- return count;
- }
- public int getLength()
- {
- int length = word.length();
- return length;
- }
- public String toString()
- {
- return "" + word;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Name: Sammy Samkough
- // Prog: Word & Words
- // Spec: Complete methods for a Word and Words Class that consists of an ArrayList of Word objects.
- public class WordRunner
- {
- public static void main(String[] args)
- {
- Word w = new Word("Word"); // vowels: 1 length: 4
- System.out.println("The word is: " + w);
- System.out.println("Number of vowels: " + w.getNumVowels());
- System.out.println("Length of word: " + w.getLength() + "\n");
- w = new Word("Bananas"); // vowels: 3 length: 7
- System.out.println("The word is: " + w);
- System.out.println("Number of vowels: " + w.getNumVowels());
- System.out.println("Length of word: " + w.getLength() + "\n");
- w = new Word("Alligator"); // vowels: 4 length: 9
- System.out.println("The word is: " + w);
- System.out.println("Number of vowels: " + w.getNumVowels());
- System.out.println("Length of word: " + w.getLength() + "\n");
- }
- }
- /*
- The word is: Word
- Number of vowels: 1
- Length of word: 4
- The word is: Bananas
- Number of vowels: 3
- Length of word: 7
- The word is: Alligator
- Number of vowels: 4
- Length of word: 9
- Press any key to continue . . .
- */
- -------------------------------------------------------------------------------------------------------------------------------
- // Name: Sammy Samkough
- // Prog: Word & Words
- // Spec: Complete methods for a Word and Words Class that consists of an ArrayList of Word objects.
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.Collections;
- public class Words
- {
- private ArrayList<Word> words;
- public Words()
- {
- setWords("");
- }
- public Words(String wordList)
- {
- words = new ArrayList<Word>();
- setWords(wordList);
- }
- public void setWords(String wordList)
- {
- words = new ArrayList<Word>();
- Scanner sc = new Scanner(wordList);
- while(sc.hasNext())
- {
- words.add(new Word(sc.next()));
- }
- }
- /*public String getWords()
- {
- String wd = words.get();
- return wd;
- }*/
- // count how many characters there are in a word
- public int countWordsWithXChars(int size)
- {
- int count = 0;
- for (Word wd : words)
- {
- if(wd.getLength() == size)
- {
- count++;
- }
- }
- return count;
- }
- // this method will remove all words with a specified size / length
- // this method will also return the sum of the vowels in all words removed
- public int removeWordsWithXChars(int size)
- {
- int count = 0;
- for (int i = 0; i < words.size(); i++)
- {
- if (words.get(i).getLength() == size)
- {
- count += words.get(i).getNumVowels();
- words.remove(i);
- }
- }
- return count;
- }
- // count how many vowels there are in a word
- public int countWordsWithXVowels(int numVowels)
- {
- int count = 0;
- for (Word wd : words)
- {
- if (wd.getNumVowels() == numVowels)
- {
- count++;
- }
- }
- return count;
- }
- public String toString()
- {
- return "" + words;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Name: Sammy Samkough
- // Prog: Word & Words
- // Spec: Complete methods for a Word and Words Class that consists of an ArrayList of Word objects.
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.Collections;
- public class WordsRunner
- {
- public static void main(String args[])
- {
- Words test = new Words("one two three four five six seven alligator");
- System.out.println("The words are: " + test);
- System.out.println("word with 2 vowels = " + test.countWordsWithXVowels(2));
- System.out.println("word with 3 vowels = " + test.countWordsWithXVowels(3));
- System.out.println("word with 4 vowels = " + test.countWordsWithXVowels(4));
- System.out.println("word with 2 chars = " + test.countWordsWithXChars(2));
- System.out.println("word with 3 chars = " + test.countWordsWithXChars(3));
- System.out.println("word with 4 chars = " + test.countWordsWithXChars(4));
- System.out.println("word with 5 chars = " + test.countWordsWithXChars(5));
- int vowelsRemoved = test.removeWordsWithXChars(3);
- System.out.println("\nafter removing words with 3 chars: \n" + test);
- System.out.println("\nnumber of vowels in the words removed == " + vowelsRemoved);
- System.out.print("--------------------------------------------------------------------------------");
- /*----------------------------------------------------------------------------------------------------------------------------*/
- test = new Words("fun fly four size times ten plus eight");
- System.out.println("The words are: " + test);
- System.out.println("word with 2 vowels = " + test.countWordsWithXVowels(2));
- System.out.println("word with 3 vowels = " + test.countWordsWithXVowels(3));
- System.out.println("word with 4 vowels = " + test.countWordsWithXVowels(4));
- System.out.println("word with 2 chars = " + test.countWordsWithXChars(2));
- System.out.println("word with 3 chars = " + test.countWordsWithXChars(3));
- System.out.println("word with 4 chars = " + test.countWordsWithXChars(4));
- System.out.println("word with 5 chars = " + test.countWordsWithXChars(5));
- vowelsRemoved = test.removeWordsWithXChars(3);
- System.out.println("\nafter removing words with 3 chars: \n" + test);
- System.out.println("\nnumber of vowels in the words removed == " + vowelsRemoved);
- System.out.print("--------------------------------------------------------------------------------");
- /*----------------------------------------------------------------------------------------------------------------------------*/
- test = new Words("alligator chicken dog cat pig buffalo");
- System.out.println("The words are: " + test);
- System.out.println("word with 2 vowels = " + test.countWordsWithXVowels(2));
- System.out.println("word with 3 vowels = " + test.countWordsWithXVowels(3));
- System.out.println("word with 4 vowels = " + test.countWordsWithXVowels(4));
- System.out.println("word with 2 chars = " + test.countWordsWithXChars(2));
- System.out.println("word with 3 chars = " + test.countWordsWithXChars(3));
- System.out.println("word with 4 chars = " + test.countWordsWithXChars(4));
- System.out.println("word with 5 chars = " + test.countWordsWithXChars(5));
- vowelsRemoved = test.removeWordsWithXChars(3);
- System.out.println("\nafter removing words with 3 chars: \n" + test);
- System.out.println("\nnumber of vowels in the words removed == " + vowelsRemoved);
- System.out.print("--------------------------------------------------------------------------------");
- }
- }
- /*
- The words are: [one, two, three, four, five, six, seven, alligator]
- word with 2 vowels = 5
- word with 3 vowels = 0
- word with 4 vowels = 1
- word with 2 chars = 0
- word with 3 chars = 3
- word with 4 chars = 2
- word with 5 chars = 2
- after removing words with 3 chars:
- [two, three, four, five, seven, alligator]
- number of vowels in the words removed == 3
- --------------------------------------------------------------------------------
- The words are: [fun, fly, four, size, times, ten, plus, eight]
- word with 2 vowels = 4
- word with 3 vowels = 0
- word with 4 vowels = 0
- word with 2 chars = 0
- word with 3 chars = 3
- word with 4 chars = 3
- word with 5 chars = 2
- after removing words with 3 chars:
- [fly, four, size, times, plus, eight]
- number of vowels in the words removed == 2
- --------------------------------------------------------------------------------
- The words are: [alligator, chicken, dog, cat, pig, buffalo]
- word with 2 vowels = 1
- word with 3 vowels = 1
- word with 4 vowels = 1
- word with 2 chars = 0
- word with 3 chars = 3
- word with 4 chars = 0
- word with 5 chars = 0
- after removing words with 3 chars:
- [alligator, chicken, cat, buffalo]
- number of vowels in the words removed == 2
- --------------------------------------------------------------------------------
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement