Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The pre condition is: pre: 0 <= row < numRows(), 0 <= col < numCols() I set it u

ID: 3649123 • Letter: T

Question

The pre condition is:

pre: 0 <= row < numRows(), 0 <= col < numCols()

I set it up like this:
//check the preconditions.
if (!(row >= 0 && row < numRows() || col >= 0 && col < numCols()))
throw new IllegalArgumentException("Violation of precondition:" +
"changeElement. Row and Column must be positive and lie within matrix.");

Could someone please tell me if i set up the conditional statement correctly? If not couldn't you provide the correct way to to it with an explanation? Thanks

Explanation / Answer

The pre condition is:

0 <= row < numRows(), 0 <= col < numCols()

The correct way to set it up will be:

if (!((row >= 0 && row < numRows()) && (col >= 0 && col < numCols())))
throw new IllegalArgumentException("Violation of precondition:" +
"changeElement. Row and Column must be positive and lie within matrix.");

The reason why we should use && instead of || is that:

In case of ||, even if one condition does not hold but the other holds, the program will continue to run instead of giving an exception.

However, in case of &&, if both the conditions are satisfied, then only the program will continue running further, else it will throw an exception.

-------------------------------------------------------------------------------

Hope this helps!!!

All The Best!!!

Please do award me Karma Points and rate me!!!

Thanks!!!


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote