# last element of recursive list

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-02-2012, 02:04 PM
rokko
last element of recursive list
Hey,

i have a problem to find the last element of my recursive list

Code:

``` import java.util.NoSuchElementException; import list.List; public class Lists {                 /**         *         *         *         * Returns a string representation of the list.         *         * @return a comma-separated list of the list values         */         public static String toString(List list)         {                 if (list == null) return "null";                                 StringBuffer buf = new StringBuffer();                 buf.append(list.value).append(", ").append(toString(list.next));                 return buf.toString();         }                 /**         * Adds a value to at the beginning of a list.         *         * @param list the list to which the value is added         * @param value the value which is added to the list         * @return a new list with the new element first followed by the given list         */         public static List add(List list, int value)         {                 List newlist = new List(value, list);                 list = newlist;                 return list;         }                 /**         * Retrieves the size of a list.         *         * @param list the list in question         * @return the number of values in the list.         */         public static int size(List list)         {                 return list == null ? 0 : 1 + size(list.next);                         }         /**         * Aggregates the sum of all list values.         *         * @param list the list in question         * @return the sum of all list values         */         public static int sum(List list)         {                 return list == null ? 0 : list.value + sum(list.next);                         }                /**         * Retrieves the last element of a list.         *         * The last element of the empty list is the empty list.         *         * @param list the list in question.         * @return the last element of the given list.         */         public static List last(List list)     {                                         if (list != null && list.next == null) {                                                         return list.next;                         } else {                                 return list;                         }                                                         }```
it has to pass a jUnit test,

Code:

``` import org.junit.Assert; import u5a1.Lists; public class Tests {         public void check(List list, String expected)         {                 Assert.assertEquals(expected, Lists.toString(list));                }                 public List create(int... values)         {                 List list = null;                 for (int i=0; i<values.length; i++) {                         list = Lists.add(list, values[i]);                 }                 return list;         } } ----- private void checkLast(List list, String expected)         {                 check(Lists.last(list), expected);         }                 @Test         public void last()         {                 checkLast(null, "null");                 checkLast(create(1), "1, null");                 //checkLast(create(1,2), "1, null");                 checkLast(create(1,2,3), "1, null");         }```

but i receive the whole list, not only the last element and i dont find the failure
if its list.next == null, it means i am at the end of the list, so i can return list.next, or not?

i hope somebody can help me

gruss
• 04-02-2012, 02:28 PM
Norm
Re: last element of recursive list
Can you post the program's output and explain what is wrong with the output and show what the output should be?

Do you have a testing program? I do not see a main() method in the code you posted.
• 04-02-2012, 03:16 PM
Tolls
Re: last element of recursive list
It's JUnit, which executes in its own framework.
The entry point (as far as this is concerned) is the method marked as @Test.
• 04-02-2012, 03:23 PM
rokko
Re: last element of recursive list
Quote:

Originally Posted by Norm
Can you post the program's output and explain what is wrong with the output and show what the output should be?

Do you have a testing program? I do not see a main() method in the code you posted.

the first code block is my code, which i have to test with junit (second code block), i have to return a list with the last element, the test-program makes a string of this result and checks it with junit

the output of junit is:

org.junit.ComparisonFailure: expected:<[1, ]null> but was:<[]null>
• 04-02-2012, 03:25 PM
Norm
Re: last element of recursive list
So this code can't be executed as a java program for testing?
• 04-02-2012, 03:28 PM
JosAH
Re: last element of recursive list
I'd say your last( ... ) method is goofy; the (recursive) logic has to be:

Code:

```List last(List list) {   if (list == null) return null;   if (list.next == null) return list;   return last(list.next); }```
kind regards,

Jos
• 04-02-2012, 03:33 PM
rokko
Re: last element of recursive list
thank you, it works! :(happy):
• 04-02-2012, 04:31 PM
rokko
Re: last element of recursive list
And to find a sublist

Code:

``` /**         * Retrieves a sublist of a list.         *         * @param list the list in question         * @param index a position in the list starting at zero for the head value         * @return the sublist with the element indexed by index as head element         * @throws IndexOutOfBoundsException if the list is smaller than index         */         public static List sublist(List list, int index) throws IndexOutOfBoundsException         {                 index = size(list)-x;                 return sublist(list, index);                         }```
i have to use size(list), right?
• 04-02-2012, 04:32 PM
Norm
Re: last element of recursive list
What happens when you execute the code? Does it do what you want it to do?
• 04-02-2012, 04:36 PM
rokko
Re: last element of recursive list
no no, it cant, because f.e. x is undefined... it was just a question to see if im on the right way or if this idea is completley wrong
• 04-02-2012, 04:37 PM
Norm
Re: last element of recursive list
If it does not compile, then you must be doing something wrong.
• 04-02-2012, 04:51 PM
rokko
Re: last element of recursive list
org.junit.ComparisonFailure: expected:<[1, ]null> but was:<[]null>
• 04-02-2012, 04:52 PM
Norm
Re: last element of recursive list
How can the posted code be executed for testing?
• 04-02-2012, 04:56 PM
rokko
Re: last element of recursive list
In eclipse you can run it as Junit-Test, the code and the test are in the same package
• 04-02-2012, 04:57 PM
Norm
Re: last element of recursive list
I don't use the IDE so I'd need a class with a main() method to test the code.
• 04-02-2012, 04:59 PM
rokko
Re: last element of recursive list
of course you need the whole test
• 04-02-2012, 05:02 PM
Norm
Re: last element of recursive list
Where is the starting class? The one with the main() method.
• 04-02-2012, 05:06 PM
rokko
Re: last element of recursive list
i doubt there is a main method^^
• 04-02-2012, 05:08 PM
Norm
Re: last element of recursive list
The code can not be executed for testing as an application without a class with a main() method.
• 04-02-2012, 05:10 PM
Tolls
Re: last element of recursive list
Quote:

Originally Posted by rokko
Code:

```         /**         * Retrieves a sublist of a list.         *         * @param list the list in question         * @param index a position in the list starting at zero for the head value         * @return the sublist with the element indexed by index as head element         * @throws IndexOutOfBoundsException if the list is smaller than index         */         public static List sublist(List list, int index) throws IndexOutOfBoundsException         {                 if (list == null)                         return list;                 if (list.next == null){                         index = size(list);                         return list.next;                 }                 return list;                         }```
it does run, but dont pass the junit test:

Code:

``` @Test(expected = IndexOutOfBoundsException.class)         public void sublistFail1()         {                 Lists.sublist(null, 0);         }```

org.junit.ComparisonFailure: expected:<[1, ]null> but was:<[]null>

You should be able to say what that failure comes from.
JUnit tells you which test has failed.

I suspect it's the one I've posted here anyway.
If 'list' is null then all you do is return null. You do not throw an IndexOutOfBoundsException, so that test will fail.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last