How to Ensure Order in LRStruct with addInOrder Method?

  • Thread starter Thread starter MFlood7356
  • Start date Start date
  • Tags Tags
    Test
AI Thread Summary
To ensure order in LRStruct using the addInOrder method, the implementation must add strings from an input list in the same sequence they are provided. The current attempt only adds the last string due to a static counter and lacks a loop for iteration. A suggestion is made to utilize a loop structure, such as a while or for loop, to iterate through the input list. This will allow each string to be added to the LRStruct in the correct order. Properly implementing a loop is essential for achieving the desired functionality.
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?
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
6
Views
2K
Replies
1
Views
2K
Replies
17
Views
5K
Replies
12
Views
4K
Replies
7
Views
3K
Replies
16
Views
5K
Replies
2
Views
4K
Back
Top