I have a boolean method in my Java program and I'm wondering why NetBeans is recommending making this change to my code:
return isPrime != 0;
What I wrote was:
if (isPrime == 0) {
return false;
}
else{
return true;
}
Both work correctly but I cannot understand the logic behind the change NetBeans is suggesting. Neither true or false is being returned. Could someone explain to me the logic behind this? Thanks
NetBeans is exactly correct. The expression is a boolean. You can easily prove it by making the change and trying it.
What value does 0 != 0 evaluate to? (Hint: it's false). What about 1 != 0? (Hint: it's true).
Is that not what your more verbose code says?
Instead of
if (isPrime == 0) {
return false;
} else {
return true;
}
try
return (isPrime != 0);
It is the same thing, I'll show you the refactoring path. Starting with
if (isPrime == 0) {
return false;
} else {
return true;
}
is the same as
if (isPrime != 0) {
return true;
} else {
return false;
}
which can be reduced by substitution, substituting 'isPrime != 0' with the function 'doIsPrime()'
private boolean doIsPrime() {
return isPrime != 0;
}
substituting in
if (doIsPrime()) {
// guaranteed to be the same as above!
return doIsPrime();
} else {
return doIsPrime();
}
which can have both blocks reduced (as duplicated code)
if (doIsPrime()) {
}
return doIsPrime();
And reduced further by removing the if statement around the empty block
return doIsPrime();
Now undo the substitution 'doIsPrime()' back to 'isPrime != 0'
return isPrime != 0;
There was no need to really do the substitution; but, I find it better shows off the reasoning the if statement is redundant.
Neither true or false is being returned.
False. The result of the comparison isPrime != 0, a boolean is being returned, either true or false.
Could someone explain to me the logic behind this?
The first code is equivalent to the second code. If isPrime is 0, then return false, else return true. The != operator will yield false if isPrime is 0, and true if it isn't 0.
It means its returning the result of the predicat isPrime != 0
If isPrime = 0, then the predicat is false, therefore it return false
If isPrime != 0, then the predicat is true, therefore returning true
It's just to reduce code.
The expression isPrime != 0 returns a boolean. It is false if isPrime == 0 and true if isPrime != 0. Thus you can save the if statement and some lines of code.
Just to add to what's already said:
Though your solution and NetBeans recommended approach are exactly correct, one other approach would also be:
if (isPrime == 0) {
return false;
}
return true;
I don't know what NetBeans would suggest with this approach but I guess that it would propose the same recommendation as it did above.
Related
All test case is running successfully except one which is in1To10(9, true) → false.
Question:
Given a number n, return true if n is in the range 1..10,
inclusive. Unless outsideMode is true, in which case return true if
the number is less or equal to 1, or greater or equal to 10.
in1To10(5, false) → true
in1To10(11, false) → false
in1To10(11, true) → true
My Solution:-
public boolean in1To10(int n, boolean outsideMode) {
if(n>=1&&n<=10){
return true;
}
else if(outsideMode==true&&(n<=1||n>=10)){
return true;
}else
return false;
}
As you have mentioned in problem the return boolean value is based on boolean value of outsideMode and the range (1-10) value
But in your program the first return statement value is decided by only the range criteria you did't test the boolean value of outsideMode.
1. Solution :
Either swap the second if(condition) with first if(condition)
public boolean in1To10(int n, boolean outsideMode) {
if((outsideMode==false)&&(n>=1&&n<=10)){
return true;
}
else if(outsideMode==true&&(n<=1||n>=10)){
return true;
}else
return false;
}
2.Solution:
include the outsideMode with first if(condition) same like second if(condition)
public boolean in1To10(int n, boolean outsideMode) {
if(outsideMode==true&&(n<=1||n>=10)){
return true;
}
else if(n>=1&&n<=10){
return true;
}else
return false;
}
When you say "all test cases are running successfully", that is rather misleading since you have too few test cases.
For each interesting integer comparison you need 2 test cases: one in which the comparison succeeds and one in which it fails. As test data you should pick the two integers that make the difference. For example, if the condition is n > 10, you should use 10 and 11 as test data.
In your scenario you have a boolean, and in each case of that boolean you have two integer conditions. Altogether, that makes 2 * 2 * 2 = 8 test cases. That is much more than the 3 test cases that you currently have, but it's necessary.
By testing your code systematically you will produce more reliable code than without these tests, even if it takes some time. But in return you get more confidence in the code, and that is worth it.
Further reading: test coverage, condition coverage.
First of all outsideMode is a boolean so just writte outisdeMode&&(n<=1||n>=10)
Your error is that
if(n>=1&&n<=10){
return true;
}
Will be executed befor
else if(outsideMode==true&&(n<=1||n>=10))
Because 9 >= 1 && 9 <= 10 which means that it will return true
Swap them and you will be fine
public boolean in1To10(int n, boolean outsideMode) {
if(outsideMode&&(n<=1||n>=10)){
return true;
}
else if(!outsideMode&&n>=1&&n<=10){
return true;
}else
return false;
}
There is a Java coding question which returns true if the given non-negative number is 1 or 2 less than a multiple of 20. For example 38 and 39 it returns true, but 40 returns false.
My code:
public boolean less20(int n){
if(n%20==0){
return false;
}else if(n>20 && n%20!=0){
return false;
}else if(20-n>2){
return false;
}else if((n+1)%20!=0||(n+2)%20!=0){
return true;
}else{
return true;
}
}
The code works on most cases, but some of them does not work at all, such as n=58 or n=59.
How can I fix my code and use the easiest way to solve this question?
Another class is going to pass in random numbers into this method(x,y,z). I want to know the boolean that does returns true from my last if() statement, so I can do operations on it. I have explained my logic in the comments.
I am still really new to this, so my logic may be wrong.
public static String FindDate(int x, int y, int z) {
boolean istrue1 =(x >= 1 && x <= 31);
boolean istrue2 =(y >= 1 && y <= 31);
boolean istrue3 =(z >= 1 && z <= 31);
if(istrue1 ^ istrue2){
if(istrue1^istrue3){
if(istrue2^istrue3){//now knowing that no values are the same, i can find the true value.
if(istrue1||istrue2||istrue3){
// I want to store/use/print/know which bool(istrue) that evaluated to true, so I would know if it is
//x,y,z that went through the algorithm successfully.
}
} else{return "Ambiguous";}
}else{return "Ambiguous";}
}else{return "Ambiguous";}
return "true"; //I would actually end up returning the value that went through the algorithm
}
You can store boolean values just like any other type in Java. I'm not sure exactly what your last comment means by "the value that went through", but if you want to keep track of the result of a particular test, you don't necessarily need to write something like
if (a == b) {
return true;
} else {
return false;
}
In that case,
return a == b;
is equivalent. If the expression is more complicated parens are a good idea.
return ((a == b) || c);
And instead of returning, you could always store the result and do something with it later.
bool test = ((a == b) || c);
action(test);
You can your following: But your condition is incorrect 3 variable for two value (true, false) all can't be different. So your logic always return "Ambiguous"
if(istrue1 ^ istrue2){
if(istrue1^istrue3){
if(istrue2^istrue3){
//now knowing that no values are the same, i can find the true value.
if(istrue1||istrue2||istrue3){
// I want to store/use/print/know the bool that evaluated to true, so I would know if it is
//x,y,z that went through the algorithm successfully.
return "true";
}
}
return "Ambiguous";
Instead of:
if(istrue1||istrue2||istrue3)
Its is easiest to just break it down into 3 differnt if statement.
if(istrue1)
if(istrue2)
if(istrue3)
No easy trick that i was hoping for. sad day.
Also the statements i did with the xor(^) operators turns out to be bad logic.
it would be easiest to this:
if(a&&b || a&&c || b&&c)
That would return ambiguous if any combo are both true.
However thats not the original question, but I thought i might as well mention it.
I have four methods that check whether or not a given grid location is next to an occupied location (value of 1). The grid is assumed to wrap around, ie, if in a 50x50 grid[0][1] is the given location and grid[49][1] is occupied, the method should return true/ My checkNorth and checkEast method are working fine, but I get an ArrayIndexOutofBoundsException: -1 error for either the south or west methods every time I run the program. I checked my math and I think it should work - am I using the modulo incorrectly, or am I missing something else?
EDIT: Clarified the wrapping criterion, word use correction.
boolean checkWest(int indexA, int indexB)
{
if (indexA-1 > 0)
{
if (grid[indexA-1][indexB] == 1)
{
return true;
}
}
if (indexA-1 < 0)
{
if (grid[(indexA-1)%width][indexB] == 1)
{return true;}
else return false;
}
return false;
}
I see a couple problems. First, Java arrays are zero-indexed, which means that the first element is at index 0. So it's okay to check grid[indexA-1][indexB] when indexA-1 is equal to 0. Second, you're not properly handling when indexA equals 0. Here is my implementation. I also simplified the logic a bit.
boolean checkWest(int indexA, int indexB)
{
if (indexA > 0)
return grid[indexA - 1][indexB] == 1;
else
return grid[width + indexA - 2][indexB] == 1;
}
EDIT: I'm pretty sure I butchered the math with the second return statement. It should be right now...
my code is
int count = cur.getCount();
if (count > 0) // condition is true
return true;
else
return false; // excute this statement
here count value is 1. and in if condition "count > 0" returns true. but also it jumb to else statement. if statement not excute. it returns false. can any one give a solution for this?
Change it to
if (count > 0) {
return true;
} else {
return false;
}
or just put:
return count > 0;
I've had this issue when special characters got in.
Delete everything and rewrite it (don't copy paste) and see if it still happens.
Also, try placing a watch on Count and see what the value is to identify exactly why the branch happens...