Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Using LRStruct to pass a test

  1. Feb 16, 2010 #1
    1. Define this method so that it adds the Strings in inputto an initially empty LRStruct<String> so that they appearin the same order. Example: if input contains "a", "b" and "c", in that order, the returned list must contain "a", "b" and "c", in that order.

    LRStruct Class: http://www.cs.rice.edu/~mgricken/re...ce_MBS/RiceMBS.student/docs/lrs/LRStruct.html

    Test:
    Code (Text):
    @Test
        public void testABC() {
            _input.add("a");
            _input.add("b");
            _input.add("c");
            LRStruct<String> answer = _fwl.addInOrder(_input);
            String expected = "(a b c)";
            String actual = answer.toString();
            assertTrue("I thought answer would be "+expected+" but it was "+actual, expected.equals(actual));
        }


    2. Here's my attempt at the method addInOrder. All I'm getting is the string c to add to the LRStruct. Not sure why my if statement isn't working correctly.

    Code (Text):
    public LRStruct<String> addInOrder(List<String> input) {
       
            LRStruct<String> lrstruct = new LRStruct<String>();
            int counter = 2;
           
            if(counter>=0){
                lrstruct.insertFront(input.get(counter));
                counter--;
            }
           
            return lrstruct;
        }
     
  2. jcsd
  3. Feb 16, 2010 #2
    I think what you're looking for is a loop. I.e.:
    Code (Text):

    int counter = 2;

    if(counter>=0){
                lrstruct.insertFront(input.get(counter));
                counter--;
            }      
     
    will do the exact same thing as
    Code (Text):

    lrstruct.insertFront(input.get(2));
     
     
  4. Feb 16, 2010 #3
    Okay I understand that but thats the exact same thing I wrote. That's not helping me.
     
  5. Feb 17, 2010 #4
    You need a loop

    Doesn't Java have while loops, or for loops? Or for-each loops?
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook