Solved

Java in Eclipse

Posted on 2013-11-19
2
202 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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now