I need some clarification with my program. THANKS!

Program is supposed to Sort an array of objects into ascending order.

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1523 Views - Last Post: 09 December 2008 - 10:45 AM Rate Topic: -----

#1 Jogan101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 16-November 08

I need some clarification with my program. THANKS!

Posted 06 December 2008 - 11:59 AM

//------------------------------------------------------------------------------
   //  Essential Mutator: Increments the count associated with the given word.  If
   //		the word is not in the collection then it is added prior to the count being
   //		incremented.
   //------------------------------------------------------------------------------
	   public boolean Increment(String Word) {
		 boolean result = false;
		 int index = indexOf(Word);
		 if(index < 0) {
			if(next < limit) {
			
			   WC[next] = new WordCount(Word);
			  
			   next = WC[previous];
			   index = next;
			   next = next + 1;
			} 
			else {
			   index = -1;
			}
		 }
		 if(index >= 0) {
			result = (WC[index].Increment() > 0);
		 }
		 return result;
	  }
   	
   //------------------------------------------------------------------------------
   // Sorts the words in ascending order
   //------------------------------------------------------------------------------	



Alright so I need some clarification/help with this program. I need to take a word and insert into an array then take the next and insert that into the array and so on. The words need to be in ascending order. My question is should I make some sort of new method that sorts them or could I just somehow compare the words band then add them into there correct ascended position? using:

WC[next] = new WordCount(Word);

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: I need some clarification with my program. THANKS!

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: I need some clarification with my program. THANKS!

Posted 06 December 2008 - 02:05 PM

You can do whichever works best for you. If you are going to be using your sorting algorithm more than once then I would recommend creating a separate method. This is just for optimization sake. You're on the right track. now do you need to put them in alphabetical order or something like that or just in space 0, 1, 2...?
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2643
  • View blog
  • Posts: 11,159
  • Joined: 20-September 08

Re: I need some clarification with my program. THANKS!

Posted 06 December 2008 - 03:01 PM

Make WordCount implement Comparable and then use Arrays.sort, or use a custom Comparator
Was This Post Helpful? 0
  • +
  • -

#4 Jogan101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 16-November 08

Re: I need some clarification with my program. THANKS!

Posted 06 December 2008 - 04:39 PM

View Postmarkhazlett9, on 6 Dec, 2008 - 01:05 PM, said:

You can do whichever works best for you. If you are going to be using your sorting algorithm more than once then I would recommend creating a separate method. This is just for optimization sake. You're on the right track. now do you need to put them in alphabetical order or something like that or just in space 0, 1, 2...?



They are going to be alphabetically

View Postg00se, on 6 Dec, 2008 - 02:01 PM, said:

Make WordCount implement Comparable and then use Arrays.sort, or use a custom Comparator


Wordcount already implements Iterator is there a way to also implement Comparable
Was This Post Helpful? 0
  • +
  • -

#5 Jogan101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 16-November 08

Re: I need some clarification with my program. THANKS!

Posted 06 December 2008 - 04:44 PM

Heres my full code just for reference:

//*********************************************************************************
//  WordCounts.java	  Author: JTH
//	 An organized collection of WordCount objects.
//*********************************************************************************
   import java.util.*;
   
	public class WordCounts implements Iterator {
	  final public int LIMIT = 1024;	//The default capacity
	  private int limit;					//The object's actual capacity
	  private WordCount[] WC;				//The array of WordCount objects
	  private int next;						//The index of the available element position
   	
   //------------------------------------------------------------------------------
   //  Default Constructor: Uses the default capacity.
   //------------------------------------------------------------------------------
	   public WordCounts() {
		 Initialize(LIMIT);
	  }
		  
   //------------------------------------------------------------------------------
   //  Usual Constructor: Uses the given capacity;  if Limit < 1 then the default
   //		capacity is used instead.
   //------------------------------------------------------------------------------
	   public WordCounts(int Limit) {
		 if(Limit <= 0) {
			Limit = LIMIT;
		 }
		 Initialize(Limit);
	  }
   
   	/////////////////////////////////////////////////////////////////////////////
   	// Used by the constructors to initialize the instance variables
	   private void Initialize(int Limit) {
		 limit = Limit;
		 WC = new WordCount[Limit];
		 next = 0;
	  }
   	/////////////////////////////////////////////////////////////////////////////
   		
   //------------------------------------------------------------------------------
   //  Accessor: Returns the count of the number of elements in the collection.
   //------------------------------------------------------------------------------
	   public int N() {
		 return next;
	  }
   	
   	/////////////////////////////////////////////////////////////////////////////
   	// Used by Contains, Increment and get to determine the index of the given 
   	//	word.  A return value of -1 indicates the word was not found.
	   private int indexOf(String Word) {
		 int result = next - 1;
		 while((result >= 0) && (Word.compareTo(WC[result].Word()) != 0)) {
			result = result - 1;
		 }
		 //System.out.println(Word + "@" + result);
		 return result;
	  }
   	/////////////////////////////////////////////////////////////////////////////
   	
   //------------------------------------------------------------------------------
   //  Predicate: Used to determine whether the given word is currently in the
   //		collection.
   //------------------------------------------------------------------------------
	   public boolean Contains(String Word) {
		 int index = indexOf(Word);
		 return (index >= 0) && (Word.equals(WC[index].Word()));
	  }
   	
   //------------------------------------------------------------------------------
   //  Essential Mutator: Increments the count associated with the given word.  If
   //		the word is not in the collection then it is added prior to the count being
   //		incremented.
   //------------------------------------------------------------------------------
	   public boolean Increment(String Word) {
		 boolean result = false;
		 int index = indexOf(Word);
		 if(index < 0) {
			if(next < limit) {
			
			   WC[next] = new WordCount(Word);
			  
			   next = WC[previous];
			   index = next;
			   next = next + 1;
			} 
			else {
			   index = -1;
			}
		 }
		 if(index >= 0) {
			result = (WC[index].Increment() > 0);
		 }
		 return result;
	  }
   	
   //------------------------------------------------------------------------------
   // Sorts the words in ascending order
   //------------------------------------------------------------------------------	
			
   
   
   
   
   
   
   
   
   //------------------------------------------------------------------------------
   //  Accessor: Returns the WordCount object containing the given Word.  If the
   //		word is not in the collection then null is returned.
   //------------------------------------------------------------------------------
	   public WordCount get(String Word) {
		 WordCount result = null;
		 int index = indexOf(Word);
		 if(index >= 0) {
			result = WC[index];			
		 }
		 return result;
	  }
   	
   ///////////////////////////////////////////////////////////////////////////////////   
   // Iterator Interface instance variables and methods
   ///////////////////////////////////////////////////////////////////////////////////   
	  private int prev = Integer.MAX_VALUE;	//Index of the last element visited
	  private int curr = Integer.MAX_VALUE;	//Index of the next element to be visited
   	
   //------------------------------------------------------------------------------
   //  Resets the iteration so that it starts at the beginning.
   //------------------------------------------------------------------------------
	   public boolean Reset() {
		 curr = 0;	prev = 0;
		 return true;
	  }
   	
   //------------------------------------------------------------------------------
   //  Returns true if more elements remain to be visisted.
   //------------------------------------------------------------------------------
	   public boolean hasNext() {
		 return curr < next;
	  }
   	
   //------------------------------------------------------------------------------
   //  Returns the next WordCount object to be visisted.
   //------------------------------------------------------------------------------
	   public Object next() {
		 Object result = null;
		 if(this.hasNext()) {
			result = WC[curr];
			prev = curr;  curr = curr + 1;
		 }
		 return result;
	  }
   	
   //------------------------------------------------------------------------------
   //  Removes the last element visited from the collection.
   //------------------------------------------------------------------------------
	   public void remove() {
		 if((prev < curr) && (prev < next)) {
			for(int i=prev; i < next; i++) {
			   WC[i] = WC[i+1];
			}
			prev = curr;
			next = next - 1;
		 }
	  }
   	
   }


Was This Post Helpful? 0
  • +
  • -

#6 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: I need some clarification with my program. THANKS!

Posted 06 December 2008 - 04:48 PM

you can implement as many interfaces as you want.
public class WordCounts implements Iterator, Comparable
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: I need some clarification with my program. THANKS!

Posted 06 December 2008 - 04:54 PM

Java's pseudo multiple inheritance.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2643
  • View blog
  • Posts: 11,159
  • Joined: 20-September 08

Re: I need some clarification with my program. THANKS!

Posted 07 December 2008 - 04:23 AM

It's the class WordCount that we need to see, not WordCounts

btw, is it just me or has the usual link to copy syntax-highlighted code to the clipboard disappeared..?
Was This Post Helpful? 0
  • +
  • -

#9 Jogan101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 16-November 08

Re: I need some clarification with my program. THANKS!

Posted 07 December 2008 - 11:20 AM

View Postg00se, on 7 Dec, 2008 - 03:23 AM, said:

It's the class WordCount that we need to see, not WordCounts

btw, is it just me or has the usual link to copy syntax-highlighted code to the clipboard disappeared..?


//*********************************************************************************
//  WordCount.java	  Author: JTH
//*********************************************************************************

	public class WordCount {
	  private String word;				//The word represented as a string
	  private int count = 0;			//The corresponding count
		  
	   public WordCount(String S) {	//The essential constructor
		 word = S;
	  }
		  
	   public String Word() {			//Accessor for the word
		 return word;
	  }
		  
	   public int Increment() {		//Essential mutator that increases the count
		 count = count + 1;
		 return 1;
	  }
		  
	   public int Count() {			//Accessor for the count
		 return count;
	  }
   }



thanks
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2643
  • View blog
  • Posts: 11,159
  • Joined: 20-September 08

Re: I need some clarification with my program. THANKS!

Posted 07 December 2008 - 11:29 AM

Do i take it that you want to sort by count rather than the word itself?

Method names, incidentally should begin lower case, as should variable names. An example would be

public class WordCount implements Comparable<WordCount> {
    private int count;

    public int compareTo(WordCount other) {
        return count - other.count;
    }
}


Was This Post Helpful? 0
  • +
  • -

#11 Jogan101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 16-November 08

Re: I need some clarification with my program. THANKS!

Posted 07 December 2008 - 11:55 AM

View Postg00se, on 7 Dec, 2008 - 10:29 AM, said:

Do i take it that you want to sort by count rather than the word itself?

Method names, incidentally should begin lower case, as should variable names. An example would be

public class WordCount implements Comparable<WordCount> {
    private int count;

    public int compareTo(WordCount other) {
        return count - other.count;
    }
}



No i am trying to sort it alphabetically. I was thinking along the lines of an insertion sort algorithm I'm just not sure where exactly I would put that in my WordCounts class.
Was This Post Helpful? 0
  • +
  • -

#12 Jogan101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 16-November 08

Re: I need some clarification with my program. THANKS!

Posted 07 December 2008 - 03:15 PM

Is there anyway I can just compare the words in this method and then just add them into their correct alphabetical positions?

 //------------------------------------------------------------------------------
   //  Essential Mutator: Increments the count associated with the given word.  If
   //		the word is not in the collection then it is added prior to the count being
   //		incremented.
   //------------------------------------------------------------------------------
	   public boolean Increment(String Word) {
		 boolean result = false;
		 int index = indexOf(Word);
		 if(index < 0) {
			if(next < limit) {
			   
			   WC[next] = new WordCount(Word);
			  
			   index = next;
			   next = next + 1;
			} 
			else {
			   index = -1;
			}
		 }
		 if(index >= 0) {
			result = (WC[index].Increment() > 0);
		 }
		 return result;
	  }


Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2643
  • View blog
  • Posts: 11,159
  • Joined: 20-September 08

Re: I need some clarification with my program. THANKS!

Posted 07 December 2008 - 03:34 PM

If you store your WordCount obejcts in a TreeSet owned by WordCounts, then, with the change i just made applied to the word instead of the count, they'll be stored in word order automatically
Was This Post Helpful? 0
  • +
  • -

#14 Jogan101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 16-November 08

Re: I need some clarification with my program. THANKS!

Posted 07 December 2008 - 03:45 PM

View Postg00se, on 7 Dec, 2008 - 02:34 PM, said:

If you store your WordCount obejcts in a TreeSet owned by WordCounts, then, with the change i just made applied to the word instead of the count, they'll be stored in word order automatically


What do you mean? Could you please show me?

See I was thinking about putting something along these lines into my WordCounts class.

 //------------------------------------------------------------------------------
	//  Sorts the words using the insertion sort algorithm
	//------------------------------------------------------------------------------
		public static void insertionSort (Comparable[] list)
		{
			for (int index = 1; index < list.length; index++)
			{
				Comparable key = list[index];
				int position = index;
				while (position > 0 && key.compareTo(list[position-1]) < 0)
				{
					list[position] = list[postion-1];
					postion--;
				}
				
				list[position] = key;
			}
		}
	}



I'm just not sure how to fix this code so it applies to the words.
Was This Post Helpful? 0
  • +
  • -

#15 Jogan101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 16-November 08

Re: I need some clarification with my program. THANKS!

Posted 07 December 2008 - 10:47 PM

Anyone have any idea about this?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2