I have a java project that I need help on. The program is for word occurrences and I have a basic add() and frequency() methods I need help creating, not sure how to begin. here is the skeleton I am working with:

@Override
	public void add(final String word, final int line) {
		// TODO your job
		word.add(line);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.luc.cs.laufer.index.Index#frequency(java.lang.String)
	 */
	@Override
	public int frequency(final String word) {
		// TODO your job
		return -1;
	}

The problem is I have used .add() in other code before but I never created one. Can anyone help a beginner java coder or at least start me off in the right direction.

in your prior case where you used .add() that would usually use the .add() method of some other object. If you are in the class with the add method, then it should be more like add(word, line). and in your add method it should contain something like:

String wordVar = word;
String linVar = line;

It looks to me like you may need to look more into how methods and parameter passing works.

I have a java project that I need help on. The program is for word occurrences and I have a basic add() and frequency() methods I need help creating, not sure how to begin. here is the skeleton I am working with:

@Override
	public void add(final String word, final int line) {
		// TODO your job
		word.add(line);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.luc.cs.laufer.index.Index#frequency(java.lang.String)
	 */
	@Override
	public int frequency(final String word) {
		// TODO your job
		return -1;
	}

The problem is I have used .add() in other code before but I never created one. Can anyone help a beginner java coder or at least start me off in the right direction.

in your prior case where you used .add() that would usually use the .add() method of some other object. If you are in the class with the add method, then it should be more like add(word, line). and in your add method it should contain something like:

String wordVar = word;
String linVar = line;

It looks to me like you may need to look more into how methods and parameter passing works.

LReynolds, thanks for assisting me so I put that in and we have a pre-written test that we are suppose to run after we finish a method to make sure it works but when I run my test it still fails, here is my test:

public void testAdd() {
		int s = index.containsKey("xyz") ? index.get("xyz").size() : 0;
		index.add("xyz", 5);
		assertTrue(index.get("xyz").contains(5));
		assertEquals(s + 1, index.get("xyz").size());
	}

Can you tell me by the test where else I need to add code, I think I have to do something with containsKey, can you assist me on how I would implement such a method.

So I tried written a containsKey but its still failing, can anyone tell me what I am doing wrong:

public boolean containsKey (String wordvar, int linevar){
		if(wordvar == null){
			return false;
		}
		return true;
	}

Going to need you to post more/all of your code, I can't understand it with what you have.


So I tried written a containsKey but its still failing, can anyone tell me what I am doing wrong:

public boolean containsKey (String wordvar, int linevar){
		if(wordvar == null){
			return false;
		}
		return true;
	}

Going to need you to post more/all of your code, I can't understand it with what you have.

Ok Here is all of the class code:

package occurrences;

import java.util.List;
import java.util.TreeMap;

/**
 * An efficient implementation of an index that keeps the index in alphabetical
 * order.
 * 
 * @author laufer
 * 
 */
public class DefaultIndexImpl extends TreeMap<String, List<Integer>> implements	Index {

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.luc.cs.laufer.index.Index#add(java.lang.String, int)
	 */
	@Override
	public void add(final String word, final int line) {
		// TODO your job
		
		
		add(word,line);
	}
	
	public boolean containsKey (String word, int line){
		if(word == null){
			return false;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.luc.cs.laufer.index.Index#frequency(java.lang.String)
	 */
	@Override
	public int frequency(final String word) {
		// TODO your job
		return -1;
	}

	private static final long serialVersionUID = 8119984256641351649L;
}
package occurrences;

/**
 * A simple immutable token.
 * 
 * @author laufer
 */
public class DefaultToken implements Token {

	/**
	 * Constructs a token from a word and line number.
	 * 
	 * @param word
	 *            the word
	 * @param line
	 *            the line number
	 */
	public DefaultToken(final String word, final int line) {
		if (word == null)
			throw new IllegalArgumentException("word == null");
		if (line <= 0)
			throw new IllegalArgumentException("line <= 0");
		// TODO your job; also add any necessary fields
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.luc.cs.laufer.index.Token#getLine()
	 */
	@Override
	public int getLine() {
		// TODO your job
		return getLine();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.luc.cs.laufer.index.Token#getWord()
	 */
	@Override
	public String getWord() {
		// TODO your job
		return getWord();
	}

	private static final long serialVersionUID = 5320093356413218994L;
}
package occurrences;

import java.util.List;
import java.util.Map;

/**
 * An index for a text document. For each word that occurs in the document, the
 * index holds a list of line numbers where the word occurs.
 * 
 * @author laufer
 */
public interface Index extends Map<String, List<Integer>> {

	/**
	 * Adds an index entry for a word at the given line number.
	 * 
	 * @param word
	 *            the word
	 * @param line
	 *            the line number
	 */
	void add(String word, int line);

	/**
	 * Returns the frequency of a word in a document.
	 * 
	 * @param word
	 *            the word
	 * @return the frequency
	 */
	int frequency(String word);
}
package occurrences;

import java.io.IOException;
import java.io.StreamTokenizer;

/**
 * An index builder that populates an index from a source represented by a
 * StreamTokenizer.
 * 
 * @author laufer
 * 
 */
public class StreamTokenizerIndexBuilder {

	/**
	 * Constructs a builder for the given index.
	 * 
	 * @param index
	 *            the Index to be built
	 */
	public StreamTokenizerIndexBuilder(final Index index) {
		if (index == null)
			throw new IllegalArgumentException("index == null");
		// TODO your job; also add any necessary fields
	}

	/**
	 * Builds the index from the given source.
	 * 
	 * @param source
	 *            the StreamTokenizer from which to build the index
	 */
	public void buildFrom(StreamTokenizer source) {
		if (source == null)
			throw new IllegalArgumentException("source == null");
		try {
			// TODO your job
			if (false)
				throw new IOException(); // TODO get rid of this when done
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

}
package occurrences;

import java.io.IOException;
import java.io.StreamTokenizer;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * An adapter that makes a StreamTokenizer look like an Iterator.
 * 
 * @author laufer
 */
public class StreamTokenizerIteratorAdapter implements Iterator<Token> {

	/**
	 * Constructs an adapter for the given StreamTokenizer.
	 * 
	 * @param source
	 *            the StreamTokenizer
	 */
	public StreamTokenizerIteratorAdapter(final StreamTokenizer source) {
		if (source == null)
			throw new IllegalArgumentException("source == null");
		// TODO your job; also add any necessary fields
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Iterator#hasNext()
	 */
	@Override
	public boolean hasNext() {
		// TODO your job
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Iterator#next()
	 */
	@Override
	public Token next() {
		// TODO your job
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Iterator#remove()
	 */
	@Override
	public void remove() {
		throw new UnsupportedOperationException();
	}
}
package occurrences;

import java.io.Serializable;

/**
 * An interface for a simple token consisting of a word and a line number
 * indicating where the word occurs.
 * 
 * @author laufer
 * 
 */
public interface Token extends Serializable {

	/**
	 * Returns the word associated with this token.
	 * 
	 * @return the word
	 */
	String getWord();

	/**
	 * Returns the line number associated with this token.
	 */
	int getLine();
}
package occurrences;

import java.util.Iterator;

/**
 * An index builder that populates an index from a source represented by an
 * iterator over tokens.
 * 
 * @author laufer
 */
public class TokenIteratorIndexBuilder {

	/**
	 * Constructs a builder for the given index.
	 * 
	 * @param index
	 *            the Index to be built
	 */
	public TokenIteratorIndexBuilder(final Index index) {
		if (index == null)
			throw new IllegalArgumentException("index == null");
		// TODO your job; also add any necessary fields
	}

	/**
	 * Builds the index from the given source.
	 * 
	 * @param source
	 *            the Iterator over Tokens from which to build the index
	 */
	public void buildFrom(final Iterator<Token> source) {
		if (source == null)
			throw new IllegalArgumentException("source == null");
		// TODO your job
	}
}

I hope this helps you understand the word occurrence project. I have tests that i have to run and they have to pass and they do not because i need to implement the parts that say TODO, any help would be most appreciated.

So I think I need to develop the correct containsKey() and get() to get the first test working correctly. Here is my code and it still fails:

public boolean containsKey (Object key){
		Object k = key;
		if(key == null){
			return false;
		}
		key.equals(k);
		return true;
	}
	
	public int get (int key){
		int k = key;
		return k;
		
	}

can anyone guide me the right direction. the Test code is here:

@Test
	public void testAdd() {
		int s = index.containsKey("xyz") ? index.get("xyz").size() : 0;
		index.add("xyz", 5);
		assertTrue(index.get("xyz").contains(5));
		assertEquals(s + 1, index.get("xyz").size());
	}

Do you already have the input file tokenized so that you can even check to see if it containsKey ? because the containsKey method seems to be comparing the string you are adding with any word in the document. So unless you have an Index to compare the string being added, your containsKey method is not going to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.