/do/conference?toggle mode-read&action; list forums C Java How To Program Please
ID: 3849206 • Letter: #
Question
Explanation / Answer
Q.1 Would the logic above work? Is there anything missing inside processOrder() method ?
Ans: Yes.The above logic will work fine as if we declared 'itemFound' as boolean but there is missing thing inside the code that the for loop declared inside while loop will continue its iteration if the item is found i.e. we should stop the loop if the item is found and get out of loop. For this example(as there are only 5 records) the time required to loop is not noticable but consider that there are 90000 records and the item found location is at 3rd then the loop will continue to iterate and we need to wait for its completion. The best way is to stop the loop by using 'break' once the item is found.
Q2.What is the function of itemPosition?
Ans: The function of itemPosition is to hold the position index of the found item and by using this index we can display the information of found item by providing it as array index. For example in above program to display the item name as' itemName[itemPosition]' and 'unitPrice[itemPosition]'.
Q3a.Is the for loop as writtned now is operationally efficient as it can be?
Ans: Operationally the For loop is efficient, The function of the loop is to only iterate for the specified criteria where as it is the way logic using it that makes it more efficient.
Q3b
Ans: If the item is in slot zero we should stop the loop by using 'break' to avoid unnecessary iteration and time consumption.
To be more concise with loop iteration,search and time follow this link:
http://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.