Solved

Java in Eclipse

Posted on 2013-11-19
2
207 Views
Last Modified: 2013-11-20
Please help...I am trying to find the highest frequency in a collection of words.
I put a bunch of stars by the one I am having troubles with...

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

/**
 * WordCollection manages a collection of WordFrequency objects.
 * 
 * @author Lori Robison
 * @version 11/2013
 */
public class WordCollection implements Iterable<WordFrequency> {

	
	private ArrayList<WordFrequency> wordFrequencies;
	private int frequency;
	
	/**
	 * Creates a new, empty WordCollection object.
	 * 
	 * @requires nothing
	 * 
	 * @ensures isEmpty()
	 */
	public WordCollection() {
		this.wordFrequencies = new ArrayList<WordFrequency>();		
		// TODO for part 1	
	}
	
	/**
	 * Updates this collection to keep track of the number of times
	 * the specified text has appeared in the file being analyzed.
	 * <p>
	 * If the specified text is not already in the collection, a
	 * new WordFrequency object is created and added to the collection.
	 * If the text is in the collection, its frequency is incremented.
	 * 
	 * @requires text != null
	 * 
	 * @ensures contains a WordFrequency object for the specified text
	 * 			with the correct frequency
	 * 			
	 * 
	 * @param text	the word whose WordFrequency will be created or updated
	 */
	public void updateWord(String text) {
		WordFrequency newWord = this.findWordWith(text);
		if (this.contains(newWord)) {
			newWord.incrementFrequency();
		} else {
			this.wordFrequencies.add(newWord);
		}
		//this.findWordWith(text);
		
		// TODO for part 1
		// Hint: call methods findWordWith and contains.
	}
	
	private WordFrequency findWordWith(String text) {
		for (WordFrequency aWord: this.wordFrequencies) {
			if (aWord.getWord().equals(text)) {
				return aWord;
			}
		}
		return new WordFrequency(text);
	}
	
	/**
	 * Removes all of the elements from this collection. 
	 * The collection will be empty after this call returns.
	 * 
	 * @requires nothing
	 * 
	 * @ensures isEmpty()
	 */
	public void clear() {
		this.wordFrequencies.clear();
		// TODO for part 1
		// Hint: Delegate the task to data member wordFrequencies.
	}
	
	/**
	 * Returns the number of WordFrequency objects in this collection.
	 * 
	 * @requires nothing
	 * 
	 * @return how many objects are in the collection
	 */
	public int size() {
		int i = 0;
		for (WordFrequency aWord: this.wordFrequencies) {
			i++;
		}
		// TODO for part 1
		return i;
	}
	
	/**
	 * Returns true if the collection size is 0, and false otherwise.
	 * 
	 * @requires nothing
	 * 
	 * @return true if there are no objects in the collection
	 */
	public boolean isEmpty() {
		if (this.size() > 0) {
			return true;
		}
		// TODO for part 1
		return false;
	}
	
	
	/**
	 * Returns the maximum frequency of the WordFrequency objects
	 * in this collection.
	 * 
	 * @requires !isEmpty()
	 * 
	 * @return the highest frequency of occurrence of all words in the input
	 */
	public int getHighestFrequency() {
********	// TODO for part 2
********	//if(this.frequency > anotherWord.getFrequency()
			//for (wordFrequency aword : this.wordFrequencies) {
			//return this.frequency
		}
	}
	
	/**
	 * Returns a list of the WordCounters in this collection that is
	 * sorted in ascending order by their frequency.
	 * 
	 * @requires nothing
	 * 
	 * @return a sorted list
	 */
	public ArrayList<WordFrequency> getSortedWords() {
		ArrayList<WordFrequency> copyOfWordCounters = new ArrayList<WordFrequency>();
		
		// TODO for part 2:
		// Implement code to make copyOfWordCounters a copy of 
		// data member wordFrequencies, with all the same elements.		
				
		//Collections.sort(copyOfWordCounters);
		//return copyOfWordCounters;
	//}
	
	
	/**
	 * Returns an iterator over the elements in this collection in
	 * the order in which they were added. This allows client objects
	 * to use a for-each loop to iterate through the collection.
	 * 
	 * @requires nothing
	 * 
	 * @return the iterator
	 */
	public Iterator<WordFrequency> iterator() {
		return this.wordFrequencies.iterator();
		}
		

		
	
	/**
	 * Returns true if the collection contains the WordFrequency object,
	 * and false otherwise.
	 * 
	 * @requires nothing
	 * 
	 * @param aWord		the object to look for
	 * 
	 * @return true if the collection contains aWord
	 */
	public boolean contains(WordFrequency aWord) {
		return this.wordFrequencies.contains(aWord);
	}
	
}

Open in new window

0
Comment
Question by:lorijean26
2 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39661011
I am assuming from the nature of the problem that this is homework/assignment related? If so, we therefore can't give you the code, but we can assist you in developing the right solution yourself.

You are partially on the right track. The "for" loop on line 124 (as referenced in the code snippet above, line 124 might be slightly different in you actual source code) is correct, but it would not go inside an "if" statement like you have it. This is the general idea that you are looking for...

First, you will need a variable to hold the "max" frequency found so far, a local variable is fine for this. Then you need to iterate through your WordFrequency elements (using the for loop that you already have). Now for each WordFrequency object, you need to "compare" if the frequency of it is greater than the max frequency found so far, "if" it is you can update the max found so far variable. Then at the end of the loop, that variable will hold the answer you are looking for.


PS. As a side note, your size() method on line 88 can be simplified quite a bit. You don't actually need to loop through the elements and count them, ArrayList (which is the class of your wordFrequencies object) also has a size() method that you could probably use.

PPS. Double check your logic for your isEmpty() method on line 104. Are you sure that it returns the correct result depending on how many items are in the list?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question