In: Computer Science
Complete answer will be given an immediate upvote :)
1. Explain the rationale for returning a Boolean value from the add bag operation.
2. Explain why writing a test program before implementing a class is a good idea.
3. Why is it a safer practive for the toArray method to return a copy of the array instead of a reference to the array?
1) This originates from Collection <E> prefix, because the interface is used as the ancestral component of all types of clusters, it makes sense to return the boolean notification when adding a cluster to a successful operation.
2) This is called the test-driven approach. It is recommended to avoid errors and bugs in a program. To understand this, imagine 2 cases.
Case 1 : When u write a program first (i.e implementing a class) and then test its functions - While working on a big project with multiple objective you might forget implementing a task over the time. projects can go for a long time even months to complete and thus it might happen that a programmer might forget to insert those features. If he forgets, it is detectec during the testing phase by testers and will fail in production.
Case 2 : When you write test cases first and then implement a class, the programmer makes sure test cases of all requirements are already mentioned. This helps the programmer identify any bugs, errors or mistakes before it goes into production.
Thus it is recommended to use the test-driven approach.
3) It can be very inconsistent for changes in the returned array to affect the source at times and sometimes not. ToArray works the same way for the same reason. Thus, you will always get a copy of the array instead of the same reference to it to avoid the confusion of changes in the orignal array and its address.