Click here to Skip to main content
16,016,962 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
boolean insert(int index, Object update) - add an object at index and move items at the index and beyond after the inserted object. Return false if index < 0 || index > length() before the insert is complete.

What I have tried:

public boolean insert(int index, Object update)
{
//returns true if index is <= length
for(index = 0; index < length; index++)
{
if (index < 0 || index > length())
{
return false;
}
}
return true;
}
Posted
Updated 3-Oct-16 23:40pm
v3
Comments
Mehdi Gholam 4-Oct-16 3:25am    
Looks like homework.
Member 12774645 4-Oct-16 3:49am    
it is a practise project but I am stuck on how to test for jUnit.....
phil.o 4-Oct-16 3:31am    
There is no question.
Member 12774645 4-Oct-16 3:50am    
i am trying to test for the boolean Insert(int index, Object update)
Member 12774645 4-Oct-16 3:50am    
but i am stuck

1 solution

You should first define the exact behaviour you are expecting from your insert function. As far as we can see, it does not insert anything to anything.

You are just creating an index variable that goes from 0 to (length - 1).
Then you test for index if it is between 0 and length (inclusive).
index will never be less than zero (because its first initialization value is zero).
index will never be greater than length (the loop will exit before that happens).

So, you have to clearly define and understand the behaviour that you need; then write down on a paper the different steps that you will need. And finally, use what you wrote down to code your function and test it.
 
Share this answer
 
Comments
Member 12774645 4-Oct-16 8:59am    
i have the add function, I am only unsure on how to go about the object.
phil.o 4-Oct-16 11:43am    
I am sorry, that is too unclear, for me at least. Could you define 'go about the object'?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900