in1To10 java challenge

Hi,

I am working on below challenge
http://codingbat.com/prob/p137365

i worote as below
public boolean in1To10(int n, boolean outsideMode) {

if(n<=10&&n>=1){

return true;
}

else if(n<=1&&outsideMode==true){

return true;
}


else if(n>=10&&outsideMode==true){

return true;
}


  else 
  return false;
}

Open in new window


i am failig one test as below
Expected      Run            
in1To10(5, false) → true      true      OK         
in1To10(11, false) → false      false      OK         
in1To10(11, true) → true      true      OK         
in1To10(10, false) → true      true      OK         
in1To10(10, true) → true      true      OK         
in1To10(9, false) → true      true      OK         
in1To10(9, true) → false      true      X         
in1To10(1, false) → true      true      OK         
in1To10(1, true) → true      true      OK         
in1To10(0, false) → false      false      OK         
in1To10(0, true) → true      true      OK         
in1To10(-1, false) → false      false      OK         
other tests

how to fix that and improve my approach and design of this challenge. How do i make a graphical venn or ascii diagram to proceed further. Please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karthik RamachandranCommented:
As per your code, all of the given cases should pass. Which of your test case is failing?
CEHJCommented:
if(n<=10&&n>=1){

return true;
}

Open in new window

has not tested outsideMode. Hint: start by making testing for outsideMode the first thing you do.

Also good methods have one exit point only (usually)
gudii9Author Commented:
public boolean in1To10(int n, boolean outsideMode) {

if(n<=10&&n>=1 && outsideMode==false ){

return true;
}

else if(n<=1&&outsideMode==true){

return true;
}


else if(n>=10&&outsideMode==true){

return true;
}


  else 
  return false;
}

Open in new window


i fixed and passing all tests. Please suggest how to improve my code. Description is too short and i have to read in between lines too to exactly understand to get below

has not tested outsideMode
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

CEHJCommented:
Description is too short and i have to read in between lines too to exactly understand to get below
Nonetheless, you fixed it by following my suggestion - but not quite in the way i meant.

Test ONLY for outsideMode at first. That will give you two code paths (outsideMode and !outsideMode) and your other code can go into those
ozoCommented:
   return outsideMode?n<=1||10<=n:1<=n&&n<=10;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gurpsbassiCommented:
public boolean in1To10(int n, boolean outsideMode) {
  if(outsideMode){
    return n <= 1 || n >= 10;
  }
  return n >=1 && n <= 10;
}

Open in new window

gudii9Author Commented:
how to draw venn diagram for this challenge?

please advise

i tried as attached but not complete
vennDia.png
ozoCommented:
If I were to diagram this one, I might do it slightly differently

This table shows the answer you are trying to get
      outsideMode
      True | False
---+-------+-------
 0 |  True | False
---+-------+-------
 1 |  True |  True
---+-------+-------
 2 | False |  True
---+-------+-------
 3 | False |  True
---+-------+-------
 4 | False |  True
---+-------+-------
 5 | False |  True
---+-------+-------
 6 | False |  True
---+-------+-------
 7 | False |  True
---+-------+-------
 8 | False |  True
---+-------+-------
 9 | False |  True
---+-------+-------
10 |  True |  True
---+-------+-------
11 |  True | False

Open in new window

And this table shows some operators you might wish to combine in some way in order to get those answers:
      <1   |  <=1  | ==1   |  >=1  |   >1  |  <10  |  <=10 |  ==10 |  >=10 | > 10
   +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------
 0 |  True |  True | False | False | False |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------
 1 | False |  True |  True |  True | False |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 2 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 3 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 4 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 5 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 6 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 7 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 8 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 9 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
10 | False | False | False |  True |  True | False |  True |  True |  True | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
11 | False | False | False |  True |  True | False |  True | False |  True |  True

Open in new window

ozoCommented:
Which suggests another expression:
  return (1<=n&&n<=10) ^ (outsideMode&&n!=1&&n!=10);
awking00Information Technology SpecialistCommented:
gudii9,
I would also suggest you get out of the habit of using equality with Booleans. We've all grown up with "=" meaning equals but in Java "==" means equals and "=" is the assignment operator. Looking at the results we know that in1To10(9,true) returns false. But suppose, in our code we had something like if(outsideMode = false && n<=10 && 1<=n) return true, then in1To10(9,true) would return true because the "=" assigned the value of false to outsideMode even though our method call was for outsideMode to be true. Sooner or later you will end up doing something like that and not know why it's not working. Even to say if outsideMode == false is superfluous, just say if !outsideMode
gudii9Author Commented:
has not tested outsideMode. Hint: start by making testing for outsideMode the first thing you do.

how to know which to start first.

how to draw venn diagram for this?
i tried but not right though.

- but not quite in the way i meant.

public boolean in1To10(int n, boolean outsideMode) {

if(outsideMode==true){

return true;
}


else if(n<=10&&n>=1){

return true;
}

else
return false;

}

Open in new window

i tried as above based on your advise but failing one test case
Expected	Run		
in1To10(5, false) → true	true	OK	    
in1To10(11, false) → false	false	OK	    
in1To10(11, true) → true	true	OK	    
in1To10(10, false) → true	true	OK	    
in1To10(10, true) → true	true	OK	    
in1To10(9, false) → true	true	OK	    
in1To10(9, true) → false	true	X	    
in1To10(1, false) → true	true	OK	    
in1To10(1, true) → true	true	OK	    
in1To10(0, false) → false	false	OK	    
in1To10(0, true) → true	true	OK	    
in1To10(-1, false) → false	false	OK	    
other tests
OK	    

Open in new window

please advise
gudii9Author Commented:
public boolean in1To10(int n, boolean outsideMode) {

if(outsideMode==true &&!(n<=10&&n>=1)){

return true;
}


else if(n<=10&&n>=1){

return true;
}

else
return false;

}

Open in new window


i tried as above still failing one
Expected	Run		
in1To10(5, false) → true	true	OK	    
in1To10(11, false) → false	false	OK	    
in1To10(11, true) → true	true	OK	    
in1To10(10, false) → true	true	OK	    
in1To10(10, true) → true	true	OK	    
in1To10(9, false) → true	true	OK	    
in1To10(9, true) → false	true	X	    
in1To10(1, false) → true	true	OK	    
in1To10(1, true) → true	true	OK	    
in1To10(0, false) → false	false	OK	    
in1To10(0, true) → true	true	OK	    
in1To10(-1, false) → false	false	OK	    
other tests
OK	  

Open in new window


please advise
ozoCommented:
if( outsideMode ){
   return !(n<10&&n>1);
 }
ozoCommented:
in1To10(9, true) → false       true      X      
Is your question, "Why is the expected result false?" or is it "Why did the above code return true?"
If the former, what leads you to believe that the expected answer would not be "false"?
If the later, what leads you to believe that the above code would not return "true"?
Different misunderstandings may call for different explanations.
gudii9Author Commented:
my question is neither of those two
public boolean in1To10(int n, boolean outsideMode) {

if(outsideMode==true &&!(n<=10&&n>=1)){

return true;
}


else if(n<=10&&n>=1){

return true;
}

else
return false;

}

my question is above code written by me is not passing all test cases(failing only 1 for whatever reason). Is there is a way i can fix above code to make it pass all the tests? please advise
gudii9Author Commented:
public boolean in1To10(int n, boolean outsideMode) {

if((n<=10&&n>=1)){

return true;
}


else if(outsideMode){

return true;
}

else
return false;

}

Open in new window


i modified as above which also failing one same test case
diagram.png
gudii9Author Commented:
in1To10(9, true) → false

i think i need clarity on above as well

why for above test test expected result is false

As per description of challenge
Given a number n, return true if n is in the range 1..10, inclusive.
??
CEHJCommented:
Given a number n, return true if n is in the range 1..10, inclusive.
Those criteria are invalidated as outsideMode is true. You can concentrate therefore solely on the outsideMode criteria.
By those criteria the result is false since 9 is NOT less than or equal to 1. Neither is it greater than or equal to 10
gudii9Author Commented:
public boolean in1To10(int n, boolean outsideMode) {

boolean resuklt=false;

 if(outsideMode){
 
 if((n>=10 || n<=1)) 

resuklt=true;

else
resuklt=false;
}

if(!outsideMode){
 
 if((n<=10 && n>=1)) 

resuklt=true;

else

resuklt=false;


}

return resuklt;

}

Open in new window


i passed as above.


'Unless' word is confused me.


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.

outside mode true means i have to use || as one condition is enough to see n less than 1 or  n greater than 10
outside mode false (ie inside mode) means i have to use && as both conditions needed to be tested(  greater than 1 and less than 10)
Expected	Run		
in1To10(5, false) → true	true	OK	    
in1To10(11, false) → false	false	OK	    
in1To10(11, true) → true	true	OK	    
in1To10(10, false) → true	true	OK	    
in1To10(10, true) → true	true	OK	    
in1To10(9, false) → true	true	OK	    
in1To10(9, true) → false	false	OK	    
in1To10(1, false) → true	true	OK	    
in1To10(1, true) → true	true	OK	    
in1To10(0, false) → false	false	OK	    
in1To10(0, true) → true	true	OK	    
in1To10(-1, false) → false	false	OK	    
other tests

Open in new window

gudii9Author Commented:
public boolean in1To10(int n, boolean outsideMode) {

if(outsideMode){

if(n<=1||n>=10){
return true;
}

else 
return false;
}

else{

if(n>=1&& n<=10){
return true;
}
else {
return false;
}

}


}

Open in new window



above also passed all
gudii9Author Commented:
  return (1<=n&&n<=10) ^ (outsideMode&&n!=1&&n!=10);

Open in new window


can you please advise above solution. I have not understood it.

cap ^ means either left or right is true then only true right XOR operator we discussed.

what is the need of != here?
awking00Information Technology SpecialistCommented:
unless means "except when"
gudii9Author Commented:
unless means "except when"

above is helpful
ozoCommented:
Observing the first table in http:#a41371926 we see that outsideMode True results are the opposite of the outsideMode False results except when n is 1 or n is 10
But I would still prefer the solution of http:#a41241525, which more directly parallels the description of the problem.
It even places n outside the 1,10 in the outsideMode case, and inside the 1,10 in the !outsideMode case
gudii9Author Commented:
ID: 41301625

i was not clear on above comment. what is row and column names here. I am pretty confused on the both tables esp column  like 0, 1, 2,....what the mean??

And what the each cell boolean values mean?
ozoCommented:
in http:#a41301625,
The cell in the column corresponding to the value of outsideMode,
and the row corresponding to the value of n,
contains the Expected result of in1To10(n,outsideMode)
e.g, for in1To10(1, false)
 n=1, outsideMode=false, and the Expected result is true

I'm not sure what seems ambiguous about that representation
But if you want to use a different representation that makes more sense to you,
then I would suggest using that instead.
There's nothing magical about any particular representation,
and anything  that helps you to see what's going on can be useful.
Different representations may be helpful for different people or for different aspects of different problems.
But having more ways of looking at problems can make it easier to find one that seems a natural fit, or easier to shift from one to another.
If you think of a program as a way to transform one problem into another, then the goal can be to transform the given problem into a set of problems that have already been solved.
gudii9Author Commented:
     outsideMode
      True | False
---+-------+-------
 0 |  True | False
---+-------+-------
 1 |  True |  True
---+-------+-------
 2 | False |  True
---+-------+-------
 3 | False |  True
---+-------+-------
 4 | False |  True
---+-------+-------
 5 | False |  True
---+-------+-------
 6 | False |  True
---+-------+-------
 7 | False |  True
---+-------+-------
 8 | False |  True
---+-------+-------
 9 | False |  True
---+-------+-------
10 |  True |  True
---+-------+-------
11 |  True | False

above is very clear.


     <1   |  <=1  | ==1   |  >=1  |   >1  |  <10  |  <=10 |  ==10 |  >=10 | > 10
   +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------
 0 |  True |  True | False | False | False |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------
 1 | False |  True |  True |  True | False |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 2 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 3 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 4 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 5 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 6 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 7 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 8 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
 9 | False | False | False |  True |  True |  True |  True | False | False | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
10 | False | False | False |  True |  True | False |  True |  True |  True | False
---+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
11 | False | False | False |  True |  True | False |  True | False |  True |  True

in above i got row which represents first argument of the method which is n value which is fine.


Does not column values supposed to be outSide range boolean instead of below??

<1   |  <=1  | ==1   |  >=1  |   >1  |  <10  |  <=10 |  ==10 |  >=10 | > 10
ozoCommented:
In the second table, the columns represent different tests that might be applied to n,
some of which may turn out to be useful, some of which may not, some of which may need to be combined with other tests to be useful.
But seeing them all at once may help you to find a useful combination, if you find it helpful to think visually.

!=1 and !=10 are missing from that table, but all the information they would provide is given in the ==1 and ==10 columns, (and it was not obvious what the natural order would be to place those columns if they were included)
There was also some judgement involved in deciding that columns like ==1000 might not be worth including in the table.
It may be less clear whether something like <11 or <=9 might be useful, so if you are so inclined you might wish to try adding them to see if they suggest any other solutions.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.