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?
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;
}
I've been working on my assignment all day and am stuck at just one part. I am pretty good at searching the internet and finding answers and have checked here as well. I still can't seem to figure out what is going on.
For the assignment we need to write a class with methods that will test a triangle. I am struggling with understanding classes, but have been able to get most of the code to work. The only issue I have now is with the is_equilibrium(); method. When running with the program provided with the assignment, the equilibrium method will return true every time.
problem code:
public boolean is_equilateral()
{
if(longest == shortest)
{
return true;
}
else
{
return false;
}
}
When I change it to:
public boolean is_equilateral()
{
if(side1 == side2 && side2 == side3&& side3 == side1)
{
return true;
}
else
{
return false;
}
}
I the code works fine and will return the true or false values.
I hope I am asking everything right, I am still trying to learn java so please bear with me! Any help is greatly appreciated!
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.
Can we use .contains(BigDecimal.ZERO) when reading a list in JAVA ??
I am trying:
if (selectPriceList.contains(BigDecimal.ZERO)) {
return true;
}
return false;
But it always returns false.
This seems to work but does it need correction?
BigDecimal zeroDollarValue = new BigDecimal("0.0000");
if (selectPriceList.contains(zeroDollarValue)) {
return true;
}
return false;
The problem occurs because the scale, the number of digits to the right of the decimal point, of BigDecimal.ZERO is set to 0, while the scale of zeroDollarValue is 4.
The equals method of BigDecimal compares both the scale and the value - if either are different, it returns false.
You can probably use
return selectPriceList.contains(BigDecimal.ZERO.setScale(4));
Assuming that all of your prices go out to four decimal places. If not, you might have to use
for(BigDecimal bd : selectPriceList) {
if(bd.compareTo(BigDecimal.ZERO) == 0) {
return true;
}
}
return false;
For more information, see the documentation.
I have the following problem:
I have a hashset of Pairs. Pair is a pair of ints. the pair represents "likes".
let's say my set is :<1,2>,<2,1>,<3,1>,<6,7>,<5,7>,<2,6>
this means 1 likes 2 and 2 likes 1 and 3 likes 1 and so on...
What I'm requested to do is to look at those relations as a graph and given two numbers let's say 2 and 6 I have to find whether there is a route in a graph from 2 to 6 with at most 5 edges connecting between them...
how to write a short recursive method that calculates if the route exists?
I wrote the following code:
private boolean findPath(int from, int to, int count){
System.out.println(from+" "+to+" "+count);
if(from==to && count<=5)
return true;
if(count>5)
return false;
Iterator<CookingParty.Pair> iter=likesSet.iterator();
while(iter.hasNext()){
Pair curr=iter.next();
if(curr.likes==from && curr.liked==to){
return true;
}
if(curr.likes==from)
return findPath(curr.liked, to, count+1);
}
return false;
}
the problem is that it won't continue going over the rest of the possibilities once one was found to be wrong.
how can I change it to work?
this is the update:
private boolean findPath(int from, int to, int count){
System.out.println(from+" "+to+" "+count);
if(from==to && count<=5)
return true;
if(count>5)
return false;
Iterator<CookingParty.Pair> iter=likesSet.iterator();
boolean found=false;
while(iter.hasNext() && !found){
Pair curr=iter.next();
if(curr.likes==from && curr.liked==to){
found=true;
return found;
}
if(curr.likes==from)
return findPath(curr.liked, to, count+1);
}
return found;
}
Currently you return as soon as you find a pair where curr.likes == from. To explore also other paths, you mustn't immediately return in the while loop, but while you haven't yet found a path, check for further possibilities.
boolean found = false;
while(iter.hasNext() && !found){
// check a path
}
return found;
Re update: You are still returning in the loop. That's okay in the case where you found a path, but you absolutely must not return in the general case. If curr.likes == from and curr.liked != to, check that path and update the boolean, do not return. Return after the loop has finished.
To search for a path in a graph you can use Depth-First or Breadth-First search. Depth-first is traditionally recursive because it uses a stack. Have a look at the pseudocode here:
http://en.wikipedia.org/wiki/Depth-first_search#Pseudocode