Results 1 to 3 of 3
  1. #1
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Question PROOF READ: Sort text file 3 different ways and compare

    If you wouldn't mind could I have someone please proof read my code and give me pointers as to how I can complete my program, I am stuck as of right now. The purpose of my program is to basically utilize three sorting algorithms, bubblesort, insertion sort and selection sort. I am to sort a text file in ascending order. For example, apple should come before banana and banana before orange. Here is what I have so far.

    Java Code:
    public interface InterfaceStringArray
    {
    	public void initialize(int arrSize);
    	//initializes an array of size=arrSize
    	
    	public int getSize();
    	//returns the current array size
    	
    	public void insert(String newItem); 
    	//inserts newItem in the next available spot
    	
    	public boolean delete(String deleteItem); 
    	//returns true if all occurrences of deleteItem are deleted, 						  
    	//false if no matching items found
    	
    	public int search(String searchItem); 
    	//returns the index of the first matching item, -1 if no match
    	
    	public boolean setItem(int index, String itemValue); 
    	//returns true if successful, false otherwise
    	
    	public String getItem(int index); 
    	//returns the item at the requested index
    	
    	public boolean resize(int newSize); 
    	//returns true if successful (widening), false if narrowing
    	
    	public void swap(int one, int two);
    	//swaps strings
    	
    	public void bubbleSort();
    	//sorts the array strings using Bubble Sort Algorithm (ascending)
    	
    	public void selectionSort();
    	//sorts the array strings using Selection Sort Algorithm (ascending)
    	
    	public void insertionSort();
    	//sorts the array strings using Insertion Sort Algorithm (ascending)
    	
    	public String getStringArray(); 
    	//returns a String of all the array strings separated by a white space
    }


    The 4 methods that are currently blank I am unsure of, any pointers or peer edits that would help me see what statements would be used here would be greatly appreciated.

    Java Code:
    public class StringArray implements InterfaceStringArray
    	{
    
    	private String a[];			 //reference to array a
    	private int nElems;			// number of data items
    
    //--------------------------------------------------------------
    	
    public void initialize(int arrSize)
    	{	
    	a = new String[arrSize]; 	//initializes an array of size=arrSize
    	nElems = 0;			   		//sets the number of data items to 0
    	
    	}
    //--------------------------------------------------------------
    	
    public int getSize()
    	{
    		{ return nElems; }		//returns the current array size
    	}
    
    //--------------------------------------------------------------
    
    public void insert(String newItem)
    	{
    	a[nElems] = newItem;
    	nElems++; 					//inserts newItem in the next available spot
    	}
    	
    //--------------------------------------------------------------
    
    public boolean delete(String deleteItem)
    	{
    	int j;
    	for (j=0; j<nElems; j++)
    		if( deleteItem == a[j] )
    			break;
    	if(j==nElems)
    		return false;			//false if no matching items found
    	else
    		{
    		for(int k=j; k<nElems; k++)
    			a[k] = a[k+1];
    		nElems--;
    		return true;			//returns true if all occurrences of deleteItem are deleted
    		}
    	}//end delete()
    
    //--------------------------------------------------------------						  
    	
    public int search(String searchItem)
    	{
    	int j;
    	for(j=0; j<nElems; j++)
    		if(a[j] == searchItem)
    			break;
    	if(j == nElems)
    		return -1;
    	else
    		return j;
    	}
    //--------------------------------------------------------------
    
    public boolean setItem(int index, String itemValue)
    	{
    	//returns true if successful, false otherwise
    	}
    
    //--------------------------------------------------------------
    
    	public String getItem(int index)
    	{
    	//returns the item at the requested index
    	}
    
    //--------------------------------------------------------------	
    	
    public boolean resize(int newSize) 
    	{
    	
    
    	}
    
    //--------------------------------------------------------------	
    
    public void swap(int one, int two) 
    	{
    	String temp = a[one];
    	a[one] = a[two];
    	a[two] = temp;
    	}
    
    //--------------------------------------------------------------
    
    public void bubbleSort()
    {
    int out, in;
    
    for(out=nElems-1; out>1; out--)   			// outer loop (backward)
       for(in=0; in<out; in++)        			// inner loop (forward)
          if(a[in].compareTo(a[in+1]) < 0)      // out of order?
             swap(in, in+1);         		    // swap them
    }  // end bubbleSort()
    
    //--------------------------------------------------------------	
    public void selectionSort()
    {
        int out, in, min;
    
        for(out=0; out<nElems-1; out++)   			// outer loop
           {
           min = out;                     			// minimum
           for(in=out+1; in<nElems; in++) 			// inner loop
              if(a[in].compareTo(a[in+1]) < 0 )     // if min greater,
                  min = in;              		    // we have a new min
           swap(out, min);               		    // swap them
           }  // end for(out)
        }  // end selectionSort()
    
    //--------------------------------------------------------------	
    public void insertionSort()
    {
        int in, out;
    
        for(out=1; out<nElems; out++)     			// out is dividing line
           {
           String temp = a[out];           			// remove marked item
           in = out;                      			// start shifts at out
           while(a[in].compareTo(a[in+1]) < 0) 		// until one is smaller,
              {
              a[in] = a[in-1];           			// shift item to right
              --in;                      			// go left one position
              }
           a[in] = temp;                  			// insert marked item
           }  // end for
        }  // end insertionSort()
    
    //--------------------------------------------------------------
    public String getStringArray()
    	{
    	//returns a String of all the array strings separated by a white space
    	}
    	
    }



    This is my driver .java file. Which will include the main method. Not sure of the entire structure of this .java file. I'm thinking I will need to use a bufferreader to read the .txt file. Then I should I create 3 tokenizers or is only one neccessary? I think I need to create 3 objects, and then sort each object using a different sorting algorithm. Once each has been sorted, I must compare all three then write a final method which says "gj they all match" which I will have no problem with.

    Java Code:
    
    public class StringArrayApp {
    
    	
    }

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    One thing...

    deleteItem == a[j]
    Use deleteItem.equals(a[j]). a == b, for objects, checks that they are the same instance, which is very restrictive.

  3. #3
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Question

    That you for that correction. Do you beleieve my StringArray looks correct so far as to the task I am trying to accomplish?

    Could anyone give me any pointers as to how I can tackle the last 4 remaining methods?

    Also, I will now begin to write my StringArrayApp to drive the program and use the methods I have written.

    Since I am sorting the same text file 3 different ways do I need to use 3 string tokenizers? Also when using a string tokenizer its neccessary to use a file buffer to read the text file correct? I am unfirmilar with these statements could someone provide some direction?

Similar Threads

  1. Read and modify text file
    By heartysnowy in forum New To Java
    Replies: 27
    Last Post: 11-19-2008, 10:03 AM
  2. [SOLVED] How do I read from a text file
    By matzahboy in forum New To Java
    Replies: 5
    Last Post: 11-17-2008, 04:47 AM
  3. Replies: 40
    Last Post: 07-04-2008, 08:41 AM
  4. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 02:22 PM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •