How to Ensure Order in LRStruct with addInOrder Method?

  • Thread starter MFlood7356
  • Start date
  • Tags
    Test
In summary, the conversation discusses defining a method that adds strings in input to an empty LRStruct in the same order as they appear. The method is tested using a given LRStruct class and a list of strings. The attempt at the method involves a counter and an if statement, but a loop is needed for it to work correctly. The conversation also mentions the availability of different types of loops in Java.
  • #1
MFlood7356
39
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
  • #2
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));
 
  • #3
Okay I understand that but that's the exact same thing I wrote. That's not helping me.
 
  • #4
You need a loop

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


Your attempt at the method addInOrder is close, but there are a few issues that need to be addressed.

Firstly, your counter variable is starting at 2 and decreasing by 1 each time, so it will only go through the loop once. You need to start the counter at the size of the input list and decrease it by 1 each time.

Secondly, your if statement is only checking if the counter is greater than or equal to 0, which will always be true. You need to check if the counter is greater than or equal to 0 AND less than the size of the input list.

Lastly, your insertFront() method is only adding the last element of the input list to the LRStruct. You need to use a loop to add each element in the input list to the LRStruct.

Here is a revised version of your method that should work correctly:

public LRStruct<String> addInOrder(List<String> input) {

LRStruct<String> lrstruct = new LRStruct<String>();
int counter = input.size()-1; //start counter at size of input list

while(counter >= 0){ //check if counter is greater than or equal to 0 AND less than size of input list
lrstruct.insertFront(input.get(counter)); //add element to front of LRStruct
counter--; //decrease counter by 1
}

return lrstruct;
}
 

Related to How to Ensure Order in LRStruct with addInOrder Method?

1. What is LRStruct and how does it help in passing a test?

LRStruct is a data structure used in computer science to store and organize data. It stands for "Linked Representation Structure" and is commonly used in programming languages such as C and Java. It can help in passing a test by allowing for efficient and organized storage and retrieval of data, making it easier to process and analyze information.

2. How do you use LRStruct to pass a test?

To use LRStruct to pass a test, you first need to understand the specific requirements of the test. Then, you can use LRStruct to store and organize the necessary data in a way that aligns with the test requirements. This can help you to effectively study and prepare for the test, as well as efficiently answer questions during the test.

3. Can LRStruct be used for any type of test?

Yes, LRStruct can be used for any type of test as long as the data can be organized and stored in a linked list format. This includes multiple choice tests, essay exams, and even practical exams such as coding tests. However, the specific implementation of LRStruct may vary depending on the type of test and the data being stored.

4. Are there any limitations to using LRStruct for passing a test?

One limitation of using LRStruct for passing a test is that it relies on the accuracy and completeness of the data being stored. If the data is inaccurate or incomplete, it can affect the results and potentially lead to incorrect answers. Additionally, the use of LRStruct may also require some familiarity with computer programming, which may not be accessible to all individuals.

5. Is it necessary to use LRStruct to pass a test?

No, it is not necessary to use LRStruct to pass a test. There are many other study and preparation methods that can be effective in passing a test, such as taking notes, creating flashcards, or practicing with past exams. However, for data-heavy tests, using LRStruct can be a helpful tool in organizing and managing the information needed for success.

Back
Top