How to Ensure Order in LRStruct with addInOrder Method?

  • Thread starter Thread starter MFlood7356
  • Start date Start date
  • Tags Tags
    Test
Click For Summary

Discussion Overview

The discussion revolves around implementing the `addInOrder` method for the `LRStruct` class in Java, specifically focusing on how to ensure that strings are added in the same order as they appear in the input list. The context includes coding challenges and debugging efforts related to this method.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • Post 1 outlines the requirement for the `addInOrder` method to add strings in the order they are provided, with a specific test case demonstrating the expected behavior.
  • Post 1 includes an initial implementation of the method, which only adds the last string from the input due to the use of a static counter.
  • Post 2 suggests that a loop is necessary to iterate through the input list, implying that the current implementation does not fulfill the requirement.
  • Post 3 expresses frustration, indicating that the feedback received does not address the underlying issue of the implementation.
  • Post 4 reiterates the need for a loop and mentions the availability of different types of loops in Java, suggesting that the participant should consider using one of them.

Areas of Agreement / Disagreement

Participants generally agree that a loop is necessary for the implementation, but there is no consensus on how to effectively modify the existing code to achieve the desired outcome.

Contextual Notes

The discussion does not clarify the specific requirements for the loop structure or how to handle the input list beyond the suggestion to use a loop. There may be additional assumptions about the structure of `LRStruct` that are not fully explored.

MFlood7356
Messages
37
Reaction score
0
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/research/a4obj1st/tchjava/Rice_MBS/RiceMBS.student/docs/lrs/LRStruct.html

Test:
Code:
@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:
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;
	}
 
Physics news on Phys.org
I think what you're looking for is a loop. I.e.:
Code:
int counter = 2;

if(counter>=0){
			lrstruct.insertFront(input.get(counter));
			counter--;
		}
will do the exact same thing as
Code:
lrstruct.insertFront(input.get(2));
 
Okay I understand that but that's the exact same thing I wrote. That's not helping me.
 
You need a loop

Doesn't Java have while loops, or for loops? Or for-each loops?
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
6K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 16 ·
Replies
16
Views
5K
Replies
58
Views
14K