java cigar challenge

Hi,
I am testing below challenge and wrote code as below and failed. please advise how to fix
http://codingbat.com/prob/p159531


public boolean cigarParty(int cigars, boolean isWeekend) {
  if(isWeekend = false){
  if(cigars>=40&&cigars<=60){
  return true;
  }
  else{
  return false;
}
}

  if(isWeekend = true){
  return true;
}
return true;
}

Expected      Run            
cigarParty(30, false) → false      true      X         
cigarParty(50, false) → true      true      OK         
cigarParty(70, true) → true      true      OK         
cigarParty(30, true) → false      true      X         
cigarParty(50, true) → true      true      OK         
cigarParty(60, false) → true      true      OK         
cigarParty(61, false) → false      true      X         
cigarParty(40, false) → true      true      OK         
cigarParty(39, false) → false      true      X         
cigarParty(40, true) → true      true      OK         
cigarParty(39, true) → false      true      X         
other tests            OK
LVL 7
gudii9Asked:
Who is Participating?
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.

CPColinSenior Java ArchitectCommented:
First off, you're using the assignment operator "=" in your if statements, instead of the equality operator "==", so you're overwriting your isWeekend parameter. Start by fixing that and see if the results make more sense.
0
gudii9Author Commented:
public boolean cigarParty(int cigars, boolean isWeekend) {
boolean success=true;
boolean failure=false;
  if(isWeekend = true && cigars>=40){  
  return success;
  }
  if(isWeekend = false && cigars>=40 && cigars<=60){
  return success;
}
return failure;
}

Open in new window


i tried and failing one now
0
CPColinSenior Java ArchitectCommented:
You're still using "=" instead of "==".
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

gudii9Author Commented:
public boolean cigarParty(int cigars, boolean isWeekend) {
boolean success=true;
boolean failure=false;
  if(isWeekend == true && cigars>=40){  
  return success;
  }
  if(isWeekend == false && cigars>=40 && cigars<=60){
  return success;
}
return failure;
}

Open in new window

above passed all
how to improve
0
CPColinSenior Java ArchitectCommented:
You could say "if (isWeekend)" instead of "if (isWeekend == true)" and "if (!isWeekend)" instead of "if (isWeekend == false)". You could also do "return true" and "return false" instead of having those variables. There's not much else to this challenge.
0
Jim CakalicSenior Developer/ArchitectCommented:
I'd refactor this to extract an inRange method and then use a ternary to express the decision and clarify the return of the method. Here's what you would end up with:
    public boolean cigarParty(int cigars, boolean isWeekend) {
        return isWeekend ? inRange(cigars, 40, Integer.MAX_VALUE) : inRange(cigars, 40, 60);
    }

    private boolean inRange(int num, int min, int max) {
        return num >= min && num <= max;
    }

Open in new window

Two single-line methods that I think are more readable overall.

Regards,
Jim
0
gudii9Author Commented:
I'd refactor this to extract an inRange method and then use a ternary to express the decision and clarify the return of the method.


if isWeekend  is true then do inRange(cigars, 40, Integer.MAX_VALUE)

if isWeekend  is false then do inRange(cigars, 40, 60);
    right?
Integer.MAX_VALUE means unlimited?
what is meaning of below line which has short cut AND operator between two booleans?
return num >= min && num <= max;
0
Jim CakalicSenior Developer/ArchitectCommented:
Your understanding of the ternary expression is correct. Using Integer.MAX_VALUE simplifies the inRange comparison. Because cigars is an int it isn't possible for out to take a value larger than that.

"&&" is the logical and operator meaning both sides of the condition must be true for the expression to be true. If the left side evaluates to false then the right side isn't evaluated. Since the right expression has no side effect that doesn't matter.
0
ozoCommented:
public boolean cigarParty(int cigars, boolean isWeekend) {
  return cigars >= 40 && (isWeekend || cigars <= 60);
}
0
gudii9Author Commented:
You could say "if (isWeekend)" instead of "if (isWeekend == true)" and "if (!isWeekend)" instead of "if (isWeekend == false)". You could also do "return true" and "return false" instead of having those variables.

when i make above changes as below
public boolean cigarParty(int cigars, boolean isWeekend) {

  if(isWeekend && cigars>=40){  
  return true;
  }
  if(!isWeekend && cigars>=40 && cigars<=60){
  return false;
}
return false;
}

Open in new window


i am failing tests as below
Expected      Run            
cigarParty(30, false) → false      false      OK         
cigarParty(50, false) → true      false      X         
cigarParty(70, true) → true      true      OK         
cigarParty(30, true) → false      false      OK         
cigarParty(50, true) → true      true      OK         
cigarParty(60, false) → true      false      X         
cigarParty(61, false) → false      false      OK         
cigarParty(40, false) → true      false      X         
cigarParty(39, false) → false      false      OK         
cigarParty(40, true) → true      true      OK         
cigarParty(39, true) → false      false      OK         
other tests
OK       

please advise
0
gudii9Author Commented:
public boolean cigarParty(int cigars, boolean isWeekend) {
//boolean success=true;
//boolean failure=false;
  if(isWeekend == true && cigars>=40){  
  return true;
  }
  if(isWeekend == false && cigars>=40 && cigars<=60){
  return false;
}
return false;
}

Open in new window


above itself is failing when i remved variables
0
gudii9Author Commented:
public boolean cigarParty(int cigars, boolean isWeekend) {

  if(isWeekend&& cigars>=40){  
  return true;
  }
  if(!isWeekend && cigars>=40 && cigars<=60){
  return true;
}
return false;

}

Open in new window


i made a mistake and corrected as above. Now passed all tests


what is meaning of below line

 return cigars >= 40 && (isWeekend || cigars <= 60);

please advise
0
ozoCommented:
&& means Conditional-AND, which exhibits "short-circuiting" behavior, which means that the second operand is evaluated only if needed.
|| means Conditional-OR, which exhibits "short-circuiting" behavior, which means that the second operand is evaluated only if needed.
so
   return cigars >= 40 && (isWeekend || cigars <= 60);
means
   return cigars >= 40?(isWeekend?true:cigars <= 60):false;
which means
   if( cigars >= 40 ){
      if( isWeekend  ){
         return true;
      }else{
         return cigars <= 60;
      }
   }else{
      return false;
   }
and has the same value as
   return cigars >= 40 & (isWeekend | cigars <= 60);
0
awking00Commented:
Think about the logic of what you're trying to do. Take the conditions in order of their elimination of other conditions. In this case,
if cigars are less than 40,it returns false regardless of the weekend condifion, if that condition is not satisfied then, if cigars are less than or equal to 60, if returns true regardless of the weekend condifion, then if that condition is not satisfied,then it returns true if the isWeekend condition is met, otherwise it returns false if none of the previous conditions are met. You can do this with a series of if/else if statements like -
if (cigars < 40) {return false;} else if (cigars <= 60) {return true;} else if (isWeekend) {return true} else return false; I'm a little partial to the ternary statements, so I would probably do it like this -
return (cigars<40) ? false : (cigars<= 60 ? true : (isWeekend) ? true : false);
0

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
gudii9Author Commented:
You can do this with a series of if/else if statements like -
if (cigars < 40) {return false;} else if (cigars <= 60) {return true;} else if (isWeekend) {return true} else return false


public boolean cigarParty(int cigars, boolean isWeekend) {
//You can do this with a series of if/else if statements like -
if (cigars < 40) 
{return false;} 
else if (cigars <= 60) 
{return true;} 
else if (isWeekend) 
{return true;}
 else return false;
}

Open in new window


above also worked fine.

I am having hard time understanding below
if cigars are less than or equal to 60, if returns true regardless of the weekend condifion, then if that condition is not satisfied,then it returns true if the isWeekend condition is met

I am clear till below

if (cigars < 40) 
{return false;} 
else if (cigars <= 60) 
{return true;

Open in new window



then not clear on below
else if (isWeekend) 
{return true;}
 else return false;

Open in new window


How the flow through happened in above 2 lines.

is else if (isWeekend)  is not related to top if condition if (cigars < 40) ?
0
gudii9Author Commented:
filtering need not happen in specific order from top to bottom?
we can filter anything randomely first on cigar number in below code

else if (cigars <= 60)
{return true;

then weekend or not as below code

else if (isWeekend)
{return true;}

finally what filters out to else?
0
gudii9Author Commented:
i mean it does not filter like funnel top(wide ) to bottom(narrow) as we go down from if to Else-if to else-if to else?
0
awking00Commented:
if (cigars < 40) {return false;} ==> all values of cigars must be greater than or equal to 40 after this
else if (cigars <= 60) {return true;}  ==> values of cigars now have to be between 40 and 60 inclusive
else if (isWeekend) {return true;} ==> all cigars are now greater than 60 so if it's a weekend that's good
 else return false; ==> all cigars are still greater than 60 so if it's not a weekend that's no good
}
0
gudii9Author Commented:
-------------------------------|------------------------------------|--------------------------------
less than 40 true in if(40) else if b/n40-60 true(60)else if true only weekend


did i capture cigar count diagram properly?

Is there a better way to draw this logic before beginning to code. I need a way to draw/design all these challenges before attempting to write code to choose best approach by avoiding redundancy?
please advise
0
gudii9Author Commented:
i like venn diagram circles but not sure how to relate with java objects and OOD. please advise
0
gudii9Author Commented:
how to represent this challenge as venn diagram ?
i should imagine number o cigars as one circle and then isWeekend other circle?
please advise
0
gudii9Author Commented:
venn diagram for this challenge please?


   if( cigars >= 40 ){
      if( isWeekend  ){
         return true;
      }else{
         return cigars <= 60;

      }
   }else{
      return false;
   }
and has the same value as
   return cigars >= 40 & (isWeekend | cigars <= 60);


inner if -else can be placed inside () as above highlighted right?



I'm a little partial to the ternary statements, so I would probably do it like this -
return (cigars<40) ? false : (cigars<= 60 ? true : (isWeekend) ? true : false);
how to understand 3 '?' as above. Please advise??




else if (isWeekend) {return true;} ==> all cigars are now greater than 60 so if it's a weekend that's good
 else return false; ==> all cigars are still greater than 60 so if it's not a weekend that's no good
}

as we go down we are going more in the coed flow above conditions still remain good on top of that we are adding new codition of isWeekend within else if?
0
gudii9Author Commented:
so
   return cigars >= 40 && (isWeekend || cigars <= 60);
means
   return cigars >= 40?(isWeekend?true:cigars <= 60):false;

how above two returns are same.

I see only  highlighted part is same
Please advise

Where i can get lot of good info on ternary statements esp with 3,4 '?' like here preferably with short circuit operators??
0
ozoCommented:
You could use Boolean algebra identities to transform one to another,
but it may be simpler to just list all the possibilities, at least until you become more familiar with Boolean transforms
                                                    (impossible)
                     >=40 | T | T | T | T | F | F | F
--------------------------+---+---+---+---+---+---+--------------
                     >=60 | T | T | F | F | T | T | F 
--------------------------+---+---+---+---+---+---+--------------
                  Weekend | T | F | T | F | T | F |
--------------------------+---+---+---+---+---+---+
            Weekend||<=60 | T | T | T | F | T | T | \
--------------------------+---+---+---+---+---+---+  > same
           Weekend?T:<=60 | T | T | T | F | T | T | /
--------------------------+---+---+---+---+---+---+
  >=40&&(isWeekend||<=60) | T | T | T | F | F | F | \  
--------------------------+---+---+---+---+---+---+  > same
>=40?(isWeekend?T:<=60):F | T | T | T | F | F | F | /

Open in new window

0
gudii9Author Commented:
  return cigars >= 40 && (isWeekend || cigars <= 60);
means
   return cigars >= 40?(isWeekend?true:cigars <= 60):false;
how above two are same?

please advise
0
ozoCommented:
as enumerated in http:#a41261444, for all six possible combinations of  truth values for >= 40, <= 60 and Weekend
  >=40&&(isWeekend||<=60)
and
 >=40?(isWeekend?T:<=60):F
both yield the same result.

(line 4 of http:#a41261444 should have been <=60)
0
gudii9Author Commented:
In general,
  (boolean expression)?true:false
is the same as just
  (boolean expression)


this helped.
but where is boolean expression in below two statements?
  return cigars >= 40 && (isWeekend || cigars <= 60);
means
   return cigars >= 40?(isWeekend?true:cigars <= 60):false;
0
gudii9Author Commented:
i did not undertsnad algebra diagram

 >=40 | T | T | T | T | F | F | F

why we get F F F (Looks like for friday, saturday and sunday to me)
0
gudii9Author Commented:
what is impossible case there?
please advise
0
gudii9Author Commented:
i like method of 'order of elimination' to approach these challenges to make life simple

You can do this with a series of if/else if statements like -
if (cigars < 40) {return false;} else if (cigars <= 60) {return true;} else if (isWeekend) {return true} else return false; I'm a little partial to the ternary statements, so I would probably do it like this -
return (cigars<40) ? false : (cigars<= 60 ? true : (isWeekend) ? true : false);

i see series of if/else if can be written as ternary as above. i still get confused about where to begin ( and where to end )
0
gudii9Author Commented:
i see gap in my understanding mainly with if statement with more than 4 (not sure what we call them as below)

if//1
else if//2
else if//3
else//4
then i can write simply as
(1)?(2 3 4)
0
gudii9Author Commented:
until you become more familiar with Boolean transforms

where i can get good material on this to familiarize better?
0
ozoCommented:
T is True, F is False,
Each column represents a set of possible truth values for the 3 input booleans.
(same as a region in a Venn diagram, but easier to draw in ASCII text)

there is no number for which number >= 40 and number <= 60 are both false.
(but if you want to fill in the two
F
F
T
and
F
F
F
columns anyway, you should see that bottom two rows are still both the same)

If you want to work symbolically instead of pictorially, there are many references to Boolean algebra identities on the internet
Here's one of many that popped up in a google query.
https://en.wikipedia.org/wiki/Boolean_algebra#Laws
0
gudii9Author Commented:
Each column represents a set of possible truth values for the 3 input booleans.

which three input booleans?
0
gudii9Author Commented:
i did not undertsnad algebra diagram

 >=40 | T | T | T | T | F | F | F

why we get F F F (Looks like for friday, saturday and sunday to me)
columns are not like days of the week even though i see 7 there?
0
ozoCommented:
which three input booleans?
cigars >= 40, cigars <= 60, and isWeekend


With 3 booleans, there are potentially 8 possibilities
true  true  true
true  true  false
true  false true
true  false false
false true  true
false true  false
false false true
false false false
(but in this case, the last two were not possible, so I omitted them in the table)
0
gudii9Author Commented:
which three input booleans?
cigars >= 40, cigars <= 60, and isWeekend


With 3 booleans, there are potentially 8 possibilities

why we have not considered
cigars<40??
0
ozoCommented:
cigars<40 is the same as not(cigars>=40), so it is covered under the False options of  >=40
0
gudii9Author Commented:
i am still not able to understand bolean algebbra

Each column represents a set of possible truth values for the 3 input booleans.

                       
                     >=40 | T | T | T | T | F | F | F
--------------------------+---+---+---+---+---+---+--------------
                     >=60 | T | T | F | F | T | T | F
--------------------------+---+---+---+---+---+---+--------------
                  Weekend | T | F | T | F | T | F |
--------------------------+---+---+---+---+---+---+
            Weekend||<=60 | T | T | T | F | T | T | \
--------------------------+---+---+---+---+---+---+  > same
           Weekend?T:<=60 | T | T | T | F | T | T | /
--------------------------+---+---+---+---+---+---+
  >=40&&(isWeekend||<=60) | T | T | T | F | F | F | \  
--------------------------+---+---+---+---+---+---+  > same
>=40?(isWeekend?T:<=60):F | T | T | T | F | F | F | /

there are 7 columns what are other 4 columns apart from these three columns.

Can you please put this table clearly with row and column names and more description. Any good simple sample example on the boolean algebra apart from wiki link?
please advise
0
ozoCommented:
The 3 predicates, cigars>=40, cigars<=60, Weekend can each have either of two possible truth values.
Together they have 2*2*2=8 combinations of truth values:
    >=40 |  True |  True |  True |  True | False | False | False | False
---------+-------+-------+-------+-------+-------+-------+-------+-------
    <=60 |  True |  True | False | False |  True |  True | False | False 
---------+-------+-------+-------+-------+-------+-------+-------+-------
 Weekend |  True | False |  True | False |  True | False | True  | False

Open in new window

The first column names the predicates, and each of the other 8 columns names a combination of truth values for those predicates.
In the earlier table, I abbreviated True as T and False as F to simplify it, and omitted the last  two columns because no number can be both <40 and >60
(and as I noted earlier ">=60" should have been "<=60")
0
ozoCommented:
Adding another row to illustrate when those truth values may be possible:
 example |       |       |       |       |       |       |
 cigars  | 40,60 | 40,60 | 61,80 | 61,80 | 20,39 | 20,39 |  impossible
 values  |       |       |       |       |       |       |
---------+-------+-------+-------+-------+-------+-------+-------+------- 
    >=40 |  True |  True |  True |  True | False | False | False | False
---------+-------+-------+-------+-------+-------+-------+-------+-------
    <=60 |  True |  True | False | False |  True |  True | False | False 
---------+-------+-------+-------+-------+-------+-------+-------+-------
 Weekend |  True | False |  True | False |  True | False | True  | False

Open in new window

0
gudii9Author Commented:
example |       |       |       |       |       |       |
 cigars  | 40,60 | 40,60 | 61,80 | 61,80 | 20,39 | 20,39 |  impossible
 values  |       |       |       |       |       |       |
---------+-------+-------+-------+-------+-------+-------+-------+-------
    >=40 |  True |  True |  True |  True | False | False | False | False
---------+-------+-------+-------+-------+-------+-------+-------+-------
    <=60 |  True |  True | False | False |  True |  True | False | False
---------+-------+-------+-------+-------+-------+-------+-------+-------
 Weekend |  True | False |  True | False |  True | False | True  | False

now  makes lot of sense. But how you came up with only those column names?

why not 0-9 or 10-19? how you have decided?
0
ozoCommented:
0-9 or 10-19 would produce the same truth values for >=40 and <=60 as
20 and 39 so they could have been squeezed into the table too, but I thought the table would look more cluttered if I squeezed in more than 2 arbitrary examples.
If I had said
cigars<40
40<=cigars&&cigars<=60
60<cigars
it could have specified all possible cigars values for those cases,
but that seemed like putting the cart before the horse, since I thought we wanted to develop our expressions from the original specifications, not start with the expressions.

For an arbitrary illustrative example, any subset of cigars<40 or 40<=cigars&&cigars<=60 or 60<cigars could seem as good as any other.
I'm not sure why I happened to choose the particular examples I did, but perhaps using two numbers approximately 20 apart in each case appealed to a sense of balance.
0
gudii9Author Commented:
ID: 41308787

in above post why we have twice the same name of the column?

also what is each row corresponds to?

each column corresponds to 'example cigars values' right?
0
ozoCommented:
http:#a41308787 has a columns when Weekend is True and a colunms when Weekend is False
Each row is a set of values for a particular expression
Each column corresponds to a combination of input values.
0
gudii9Author Commented:
http:#a41308787 has a columns when Weekend is True and a colunms when Weekend is False
first weekend is true then the folowing next one is weekend false right


As attached what does highlighted cell represents if we say it is false

i should understand as


if weekend is false and then <=60 then what is false?
i think i am still confused on this table?
cellValue.png
0
ozoCommented:
When the value of cigars is, for example, 61 or 80,
cigars <= 60 is false
0
gudii9Author Commented:
for example, 61 or 80,

heading of column 61,80  means it is 61 or 80? not any other values in between i thought it covers all values between 61 to 80 including both 61 and 80?
0
ozoCommented:
if a predicate like <=60 is true for a and for b, then it will also be true for all values in between a and b

So in this case either interpretation can be correct.
0
gudii9Author Commented:
When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return true if the party with the given values is successful, or false otherwise.

cigarParty(30, false) → false
cigarParty(50, false) → true
cigarParty(70, true) → true


in above challenge where it is talked about lower bound(it only talked about upper bound)

if cigars are less than 40,it returns false regardless of the weekend condition,

how did we come to this conclusion that if less than 40 always false?
0
ozoCommented:
"when the number of cigars is between 40 and 60, inclusive"
40 is a lower bound in this clause
0
gudii9Author Commented:
   if( cigars >= 40 ){
      if( isWeekend  ){
         return true;
      }else{
         return cigars <= 60;
      }
   }else{
      return false;
   }
and has the same value as
   return cigars >= 40 & (isWeekend | cigars <= 60);

Open in new window


how to decide cigars >= 40 inside if loop or isWeekend in if loop? how to go about order of elimination
please advise
0
awking00Commented:
The order is purely logical (i.e. anything less than forty fails, anything between 40 and 60 passes, and anything greater than 60 only passes if it isWeekend).
0
gudii9Author Commented:
The order is purely logical
how to teach my mind to be more logical. I think i need to improve on the logical side of my brain??
0
awking00Commented:
There are hundreds of reference books on logical and analytical thinking. You might just try searching the web and selecting ones that seem to appeal to you.
0
gudii9Author Commented:
One two good title please?
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.