Results 1 to 6 of 6

Thread: Simple Java Help

  1. #1
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default JAVA FINAL-questions!

    I am going to use this thread to post small questions I have (Its been about 2 days since I last slept so cut me a little slack for the stupidity of some of these questions)

    Public Class problem1
    {
    Public static void main(string[] args)
    {
    System.out.println(mystery(3));
    }
    public static int mystery(int n)
    {
    if (n<=1)
    return 1;
    else
    return (mystery(n-1) + n);
    }
    }

    Ok so the number is 3...
    3-1 + 3 = 5, 5-1 + 5 = 9 ? Overstack error?
    or is it something like
    3 - 1 = 2 (then recursion so.. 2-1 = 1) returns 1 + the orginal 3?

    2, 1, + 3? = 6?

    ----
    The answer they got is 6, I am just confused how what happened in the method, any explanation would be nice.


    More questions will follow.
    Last edited by [XoL]; 05-07-2013 at 04:44 PM.




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  2. #2
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

    Ok I think I figured it out, sorry ignore my double posts (I will delete them all/thread in like two days... just super super cranked on caffeine and other stimulants to stay awake!) I really should of payed attention in class -.-... Learning an entire semester of java in 3 days is going to be greaat!

    Anyway
    The way that last one works is

    It becomes 2 at first, then goes back into recursion ignoring the next step
    So

    2 + 1 (Since it is 1, it returns 1)
    So again
    2 + 1 (Then it adds the original n which is 3)

    So 2 + 1 + 3 = 6
    Right?

    Someone confirm this for me plox




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  3. #3
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Yeah, your thinking is right.

    When your code is going deep into recursion, or you have lots of iterations, it can help just to make a simple table or something to understand it, like this:

    ffs chrome.png

    Quite basic, but I think you get the idea.

  4. #4
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

    Thanks, and that is a pretty solid method of thinking thanks!

    Ok got the motherload of questions coming in:

    1) Give an implementation of the divide-and-conquer sorting pattern that will realize the selection sort algorithm for an array with base type double.

    Have no idea what it is asking of me, the answer they give me:
    Code:
    public class SelectionSort
    {
        public static void sort(double[] a, 
                                        int begin, int end)
        {
            if ((end - begin) >= 1)
            {
                int splitPoint = split(a, begin, end);
                sort(a, begin, splitPoint);
                sort(a, splitPoint + 1, end);
                join(a, begin, splitPoint, end);
            }//else sorting one (or fewer) elements
             //so do nothing.
        }
    
        private static int split(double[] a, 
                                         int begin, int end)
        {
            int index = indexOfSmallest(begin, a, end);
            interchange(begin,index, a);
    
             return begin;
        }
    
        private static void join(double[] a, int begin, 
                                       int splitPoint, int end)
        {
          //Nothing to do.
        }
    
        private static int indexOfSmallest(int startIndex,
                                   double[] a, int endIndex)
        {
            double min = a[startIndex];
            int indexOfMin = startIndex;
            int index;
            for (index = startIndex + 1; 
                                 index < endIndex; index++)
                if (a[index] < min)
                {
                    min = a[index];
                    indexOfMin = index;
                    //min is smallest of a[startIndex] 
                    //through a[index]
                }
            return indexOfMin;
        }
    
        private static void interchange(int i, int j, double[] a)
        {
                double temp;
                temp = a[i];
                a[i] = a[j];
                a[j] = temp; //original value of a[i]
        }
    }
    #2
    This one basically uses the code from the last one, but seeing as I don't understand the last one I don't understand this one.

    Write a class for sorting strings into lexicographic order that follows the outline of the class SelectionSort. Your definition, however, will use an ArrayList of the class ArrayList<string>, rather than an array of elements of type double. For words, lexicographic order reduces to alphabetic order if all the words are in either all lowercase or all uppercase letters. You can compare two strings to see which is lexicographically first by using the String method CompareTo. For strings s1 and s2, s1.compareTo(s2) returns a negative number if s1 is lexicographically before s2, returns 0 if s1 equals s2, and returns a postive number if s1 is lexicographically after s2. Call your class StringSelectionSort. A test program you can test your code with is provided below.

    The test class
    Code:
    import java.util.ArrayList;
    
    public class StringSelectionSortDemo
    {
        public static void main(String[] args)
        {
            ArrayList<String> b = new ArrayList<String>( );
            b.add("time");
            b.add("tide");
            b.add("clouds");
            b.add("rain"); 
    
            System.out.println("ArrayList values before sorting:");
            int i;
            for (String e : b)
                System.out.print(e + " ");
            System.out.println( );
            StringSelectionSort.sort(b);
            System.out.println("ArrayList values after sorting:");
            for (String e : b)
                System.out.print(e + " ");
            System.out.println( );
        }
    }
    The correct code
    Code:
    import java.util.ArrayList;
    
    /**
     Class for sorting an ArrayList of Strings lexicographically
     (approximately alphabetically).
    */
     public class StringSelectionSort
     {
        /**
         Sorts the ArrayList a so that a.get(0), a.get(1), . . ., 
         a.get(a.size( ) - 1) are in lexicographic order.
        */
         public static void sort(ArrayList<String> a)
         {
            int index, indexOfNextSmallest;
            for (index = 0; index < a.size( ) - 1; index++)
            {//Place the correct value in position index:
                indexOfNextSmallest = 
                                indexOfSmallest(index, a);
                interchange(index,indexOfNextSmallest, a);
                //a.get(0), a.get(1),...,a.get(index)
                //are sorted. The rest of the
                //elements are in the remaining positions.
            }
         }
         
        /**
          Precondition: i and j are legal indices for the ArrayList a.
          Postcondition: The values of a.get(i) and
          a.get(j) have been interchanged.
         */
         private static void interchange(
                                    int i, int j, ArrayList<String> a)
         {
            String temp;
            temp = a.get(i);
            a.set(i, a.get(j));
            a.set(j, temp);
         }
         
        /**
         Returns the index of the lexicographically first value among
         a.get(startIndex), a.get(startIndex+1),...,a.get(a.size( ) - 1)
        */
        private static int indexOfSmallest(
                                     int startIndex, ArrayList<String> a)
        {
            String min = a.get(startIndex);
            int indexOfMin = startIndex;
            int index;
            for (index = startIndex + 1; index < a.size( ); index++)
            if ((a.get(index)).compareTo(min) < 0)
            {
                min = a.get(index);
                indexOfMin = index;
            }
            return indexOfMin;
        }
    }
    How, what is happening in both of these questions?




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  5. #5
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

    So from what I gathered the two classes are just sorting patterns? Should I just commit them to memory or is there an actual method to this that I should learn?




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  6. #6
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

    Can anyone explain this one to me? I am still kinda confused I managed to work through most of it but there is no way I could memorize that for a test question is there some simple way of doing this?


    edit: nvm
    Last edited by [XoL]; 05-08-2013 at 09:49 PM.




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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