Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: need help constructing get method

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    42
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default need help constructing get method

    This is one of three trees I am using to parse text and print out every unique word, the line numbers it appears on, and the number of times it appears. I have already used a hash and tree map and have both of them working correctly. My instructor said that we need to use an AVL tree for the third map and gave us code. This is where I am unsure where to go. I will include the method that creates/fills the AVL map as well as the AVL map class. If further code is needed, I will gladly include it.

    AvlMap class
    //AvlMap class. implements map and extends Comparable
    @SuppressWarnings("rawtypes")
    public class AvlMap <AnyType extends Comparable<? super AnyType>> implements Map 
    {
    	    //construct the tree
    	    public AvlMap( ){
    	        root = null;
    	    }
     
    	    //inserts object into the map
    	    public void insert( AnyType x, AnyType y ){
    	        root = insert( x, y, root );
    	    }
     
    	    //removes the key from the map
    	    public void remove( AnyType x ){
    	        root = remove( x, root );
    	    }
     
    	    //internal method to remove from a subtree
    	    private AvlNode<AnyType> remove( AnyType x, AvlNode<AnyType> t ){
    	        if( t == null )
    	            return t;   // Item not found; do nothing
     
    	        int compareResult = x.compareTo( t.key );
     
    	        if( compareResult < 0 )
    	            t.left = remove( x, t.left );
    	        else if( compareResult > 0 )
    	            t.right = remove( x, t.right );
    	        else if( t.left != null && t.right != null ) // Two children
    	        {
    	            t.key = findMin( t.right ).key;
    	            t.right = remove( t.key, t.right );
    	        }
    	        else
    	            t = ( t.left != null ) ? t.left : t.right;
    	        return balance( t );
    	    }
    .
    .
    .
    		@Override
    		public Object get(Object key) {
    		//method in question	
    		}

    WordStats class
    import java.util.SortedSet;
    import java.util.TreeSet;
     
    //WordStats class
    public class WordStats {
     
    	//local variable occurrences and creates new set with lineNumbers
    	private int occurrences;
    	private SortedSet<Integer> lineNumbers = new TreeSet<Integer>();
     
    	//adds line number to the set
    	public void addOccurrence(int lineNumber){
    		occurrences++;
    		lineNumbers.add(lineNumber);
    	}
     
    	//returns the occurrences variable
    	public int getOccurrences(){
    		return occurrences;
    	}
     
    	//returns the line numbers stored in the set
    	public SortedSet<Integer> getLines(){
    		return lineNumbers;
    	}
    }

    This is the method specifically called to run everything for the AVL Map
    public void avlMap(String fileName){
     
    		//try/catch block to check for invalid file name
    		try{
    			//creates new bufferedreader object for input file
    			BufferedReader inFile = new BufferedReader(new FileReader(fileName));
     
    			//creates new treeMap object named avlMap
    			Map avlMap = new AvlMap();
    			String oneLine;
     
    			//Read the words and add them to the avlMap
    			for (int lineNum = 1; (oneLine = inFile.readLine()) != null; lineNum++){
    				String delims = " !@#$%{}[]/^&*,.()-;:\'\"\t";
    				StringTokenizer st = new StringTokenizer(oneLine, delims);
    				while (st.hasMoreTokens()){
    					String word = st.nextToken();
    					//vvvvvvvvvvvvvvv this is the line that is causing problems-----------------------------------------------
                                            WordStats stats = (WordStats)avlMap.(word);
     
    					//if WordStats is empty/doesnt exist,
    					//if exists, adds line numbers into a WordStats in the value
    					//...field for the int's respective key
    					if (stats == null){
    						stats = new WordStats();
    						avlMap.put(word, stats);
    					}
    					//WordStats already exists for the word
    					//calls addOccurrence method and adds the line number
    					stats.addOccurrence(new Integer(lineNum));	
    				}
    			}
     
    			//creates a new iterator object to iterate entries
    			Iterator itr = avlMap.entrySet().iterator();
     
    			//runs printEntry for each key in the treeMap
    			while (itr.hasNext()){
    				printEntry((Map.Entry)itr.next());
    			}
    		}
    			//the file name used wasn't able to be reached
    			catch(IOException e){
    				e.printStackTrace();
    			}
    	}

    I commented the line causing problems. I need to see if the wordstats already exists and return it, or return null if it doesnt.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help constructing get method

    the line causing problems
    Can you explain what the problems are?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    42
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: need help constructing get method

    Quote Originally Posted by Norm View Post
    Can you explain what the problems are?
    I need the get method to receive a string value, check if it exists in the tree, and return either the object/key that the string corresponds to or a null value if it does not exist. I know I need to change the parameter to (String inputString), but the code inside the method is what I need help with.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help constructing get method

    What does the get() method need to do? Traverse the list and find a match to the arg that was passed to the get() method,
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    42
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: need help constructing get method

    yes, to see if it exists. if it doesn't, return null.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help constructing get method

    Look at how the methods in the class work and see if there are any good examples there.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: January 23rd, 2013, 07:29 AM
  2. Only Constructing an Object Once (Details Inside)
    By avalanche72 in forum Object Oriented Programming
    Replies: 0
    Last Post: April 6th, 2012, 04:23 AM
  3. Constructing 48 bit RGB bufferedimage
    By themadmathematician in forum AWT / Java Swing
    Replies: 2
    Last Post: March 20th, 2012, 02:49 PM
  4. Help needed with constructing a while loop please!
    By Bentino in forum Loops & Control Statements
    Replies: 5
    Last Post: March 19th, 2012, 11:10 AM
  5. Constructing Strings
    By Farmer in forum Loops & Control Statements
    Replies: 5
    Last Post: September 3rd, 2011, 10:24 AM