Seeking comments on my Java code

Discussion in 'OT Technology' started by Surgie, Jan 22, 2003.

  1. Surgie

    Surgie Mein Führer, I can walk!

    Joined:
    Jun 24, 2001
    Messages:
    69,008
    Likes Received:
    0
    Location:
    Knoxville, TN
    No I'm not asking for help on any homework, I've already turned it in :)

    Just wondering how you guys would have approached this program (Sorted list matching with 4 lists) and if there's a better way to have approached this than I did.

    Code:
    /*
     *
     * Travis Surgnier ([email protected])
     * 1/22/2003
     * COP 3503-02 Assignment #1
     *
     *    Inputs: 4 arrays of listed strings
     *    Work Done: Compare the 4 arrays and find strings present in all 4 (stored in commonStrings)
     *    Outputs: Prints cStrings to the terminal
     *
     */
    
    import java.util.*;
    
    public class SLM
    {
        private static List cStrings = new ArrayList();   // Array list to store strings present in all 4 lists, grows / shrinks as we add&remove entries
    
        /*
         *
         * public static void SortedListMatch( String[] l1, String[] l2, String[] l3, String[] l4 )
         *
         *    inputs: 4 sorted lists of strings
         *    outputs: prints the common strings
         *    actions: takes the 4 lists of strings, finds the common strings among them, and then prints them to console
         *
         */
    
        public static void SortedListMatch( String[] l1, String[] l2, String[] l3, String[] l4 )
        {
            InitializeList( l1 );                         // Initialize the list by adding all the entries of l1 into cStrings
    
            GetCommonStrings( l2 );
            GetCommonStrings( l3 );
            GetCommonStrings( l4 );
    
            PrintCommonStrings();                         // Print the list of common strings
        }
    
        /*
         *
         * private static void GetCommonStrings( String[] tempList )
         *
         *    inputs: a sorted array of Strings
         *    outputs: none
         *    actions: Compares the supplied String array with the existing list, adds the ones in common to a temp list,
         *             clears the cString list and then copies the temporary list over it (overwriting the old common list
         *             with the new list of common strings)
         *
         */
    
        private static void GetCommonStrings( String[] tempList )
        {
            List tString = new ArrayList();
    
            int listPos = 0;                              // location in the temporary list (tempList)
            int listLength = tempList.length;             // size of the temporary list (tempList)
    
            while( listPos < listLength )        
            {
    
                boolean comparison = cStrings.contains( tempList[listPos] );
    
                if( comparison == true )
                {
                    tString.add( tempList[listPos] );
                }
    
                ++listPos;
            }            
    
            cStrings.clear();
            cStrings = tString;
        }
    
        /*
         *
         * private static void InitializeList( String[] listOne )
         *
         *    inputs: The list with which to initialize the common strings list
         *    outputs: none
         *    actions: Adds a list of words to the cStrings list as a basis to compare with
         *
         */
    
        private static void InitializeList( String[] listOne )
        {
            cStrings.clear();
    
            for(int i=0; i < listOne.length; ++i)
            {
                cStrings.add( listOne[i] );
            }
        }
    
        /*
         *
         * public static void PrintCommonStrings()
         *
         *    inputs: None
         *    outputs: Prints the common strings list to terminal
         *    actions: Creates an iteration and goes through the common strings list and prints them all to terminal
         *
         */
    
        public static void PrintCommonStrings()
        {
            // Print out the entire array list
            Iterator iter = cStrings.iterator(); 
    
            while ( iter.hasNext() )
            {
                System.out.println( iter.next() );
            }
        }
    }
    
    Here's the short little application I wrote to use it with

    Code:
    /*
     *
     * Example to show off the SLM class
     *
     * Travis Surgnier
     * 1/21/2003
     *
     */
    
    public class MyApplication
    {
        public static void main(String[] args) 
        {
            String[] listOne = { "Andrew", "Brian", "Matt", "Nick", "Travis", "Travis" };
            String[] listTwo = { "Azrael", "Brian", "Steve", "Travis" };
            String[] listThree = { "Azrael", "Brian", "Dan", "Dwayne", "Travis" };
            String[] listFour = { "Azrael", "Brian", "Dan", "Travis", "Xander" };
    
            SLM.SortedListMatch( listOne, listTwo, listThree, listFour );
        }
    }
     
  2. Surgie

    Surgie Mein Führer, I can walk!

    Joined:
    Jun 24, 2001
    Messages:
    69,008
    Likes Received:
    0
    Location:
    Knoxville, TN
    And yeah I know there's a lot of BS comments in there but I figure it's better to have too much than not enough (at least for homework assignments ;))

    Oh and be easy, its the first time I've used Java :o
     
  3. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    hmmmm no offence (i just out of my java class, not really wanting any more) but why do u have all the arrays in the main void. why not point it to another area (set arrays) and then ur main program starts.

    keeps the public static void main clean and just for OS use.

    thats what we are preached on.
     
  4. Surgie

    Surgie Mein Führer, I can walk!

    Joined:
    Jun 24, 2001
    Messages:
    69,008
    Likes Received:
    0
    Location:
    Knoxville, TN
    The actual assignment was the SLM class, the main was just to test it, if it was an actual program being written and not just testing the class it'd have been cleaner :)

    Was mostly wondering if people would have used something other than an ArrayList there, etc :)
     
  5. choler

    choler New Member

    Joined:
    May 21, 2002
    Messages:
    183
    Likes Received:
    0
  6. Surgie

    Surgie Mein Führer, I can walk!

    Joined:
    Jun 24, 2001
    Messages:
    69,008
    Likes Received:
    0
    Location:
    Knoxville, TN

Share This Page