approach on java challenges Approach on java challenges or real world challenges

Hi,
http://codingbat.com/java/Logic-1
I am working on below java challenges.
Before typing my code i want to get clear mental picture of what i am doing and how i am doing particular coding challenge. How do i get mental picture of the approach. What tools will help me do it. Any flowcharts, diagrams or online tools etc etc help me in this purpose. I am getting confused where to use && and where to use || etc with these challenges. Please advise
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.

Jim CakalicSenior Developer/ArchitectCommented:
I'll have to think about logic diagramming and online tools. The only thing that comes to mind is something like this http://logic.ly/demo/ or this https://www.gliffy.com/

Maybe I'm misunderstanding  your trouble, but It might help to replace the symbols with words: && = and, || = or, ! = not

if (!isWeekend && cigars <= 40) would read as
if not isWeekend and cigars less then or equal to 40

If you aren't sure of the precedence then feel free to add parens to group your expressions so you can be sure they mean what you think. You might consider writing the logic as a sentence first and then through a series of transformations arriving at the appropriate Java syntax:

1. if it isn't the weekend and I have 40 cigars or less then return true else if I have between 40 and 60 cigars inclusive return true else return false

2. if !weekend && cigars <= 40 return true else if weekend && cigars >= 40 && cigars <= 60 return true else return false

3. if (!isWeekend && cigars <= 40)
    return true;
else if (weekend && cigars >= 40 and cigars <= 60)
    return true;
else
    return false;

Jim
0
gurpsbassiCommented:
I would get a pen and paper and think it through.
Use a flowchart if you think it will help.
0
gudii9Author Commented:
how to use flow chart when more than 3 , 4 variables are there.

Please advise
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.

gurpsbassiCommented:
You just have multpile decision steps. One for each conditional.
0
awking00Commented:
I think one way to approach these challenges is to try and use as few ANDs and ORs as you can and look for single conditions rather than combinations to reach a decision point. For example, in the squirrelplay challenge you started with "if temp >= 60 and temp <= 90 and isSummer = false, this would return true regardless of it beng summer or not. However, if not, you have two conditions that need to be tested, if temp < 60 and if temp > 90. If condition temp < 60 you can reach the decision point and return false. However, if temp > 90, you still have to test if it's summer or not. In this scenario, the earliest decision point that can be reached is if temp < 60,so make that your first condition. Now, if it's not the next decision would be if it's <= 90, return true. If not, then the final decision point becomes if it's not <= 100, return false, otherwise, if it's summer, return true, else return false.
Notice how that lines up in the following flowchart representation. You should strive to construct your condition tests so that either a yes or a no will provide a decision point and not require additional condition testing to reach a decision point. Remember this whole programming world is based on binary Is the light off or is the light on is much easier to determine than if this light is off and that light is off, or this light is on and that light is on, or this light is off and that light is on, or this light is on and that light is off. Hope this helps.

is temp < 60 ---> Yes ---> return false
     |
   No
     |
    V
is temp <= 90 ---> Yes ---> return true
     |
    No
     |
    V
is temp <= 100 ---> No ---> return false
     |
   Yes
     |
    V
is it summer ---> Yes ---> return true
     |
    No
     |
    V
return 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
ozoCommented:
Start by understanding the requirements of the challenge.
If the description of the requirements seems confusing, it may be helpful to draw a Venn diagram.
You can also use the Venn diagram to translate the requirements into code.
&& would represent an intersection, and || would represent a union.
If you see patterns in the diagram they might be reflected in patterns in the code.
You can optimize for simplicity by using boolean identities to reduce the number of &&'s and ||'s
You can optimize for efficiency by putting more complicated or less likely conditions on the right of the && or || so that they are only evaluated when needed.
0
gudii9Author Commented:
You just have multpile decision steps. One for each conditional.
can you please point me to some example code, example diagram on this?
0
gudii9Author Commented:
If the description of the requirements seems confusing, it may be helpful to draw a Venn diagram.
You can also use the Venn diagram to translate the requirements into code
where to get more info on venn diagrams and any practical examples on this.

I know (atleast thats what i think) venn diagrams circles and intersection  and exclusion which i read in school but i am having hard time in applying those simple diagrams to complex object oriented interactions?

please advise
0
gudii9Author Commented:
I think one way to approach these challenges is to try and use as few ANDs and ORs as you can and look for single conditions rather than combinations to reach a decision point.

where can i read more and get myself more comfortable on this. I think i have lot of room to improve in this area. please advise
0
gudii9Author Commented:
is temp <= 100 ---> No ---> return false

Open in new window


how above is false?
0
gurpsbassiCommented:
I would concentrate on basic programming skills.
You seem to post questions on a variety of random topics - log4j, websphere, SOAPUI, GZipServletResponseWrapper, servlet concurrency, immutability, Stateless EJB's, Servlet context, dependency injection e.t.c

All of these are normally learnt when a developer becomes confident in basic programming concepts such as writing conditional statements. I would seriously take a step back and concentrate on your core java.
0
gudii9Author Commented:
I would seriously take a step back and concentrate on your core java.

core java there are hundreds of books and videos and tutorials everything goes in its own frequency , direction.

I really want to narrow to 1, or 2 to help me read and re read and consolidate those concepts.

Still not able to narrow. please advise
0
gurpsbassiCommented:
core java there are hundreds of books and videos and tutorials everything goes in its own frequency , direction.

you don't need to read them all. Pick one.
You can then read the chapters, practice with code and then post questions on here on areas of confusion in the book.

For a beginner I would recommend something like Head First Java. I think the last release was based on Java 1.6 but the majority should be relevant to you.
0
gudii9Author Commented:
Sure
0
awking00Commented:
>>is temp <= 100 ---> No ---> return false
                                         
 how above is false? <<
If the temp is not less than or equal to 100 means the temp is greater than 100 which can never return true umder anyu conditions.
0
gudii9Author Commented:
&& would represent an intersection, and || would represent a union.

You can optimize for efficiency by putting more complicated or less likely conditions on the right of the && or || so that they are only evaluated when needed.

i was not aware of these above valuable points. I will try to follow them in future
0
gudii9Author Commented:
logic.ly costs 39$ and it will never expire right? seems no free edition there?
0
gudii9Author Commented:
words: && = and, || = or, ! = not

if (!isWeekend && cigars <= 40) would read as
if not isWeekend and cigars less then or equal to 40



how to write below in sentence?

words: & = ??, | = or, ! = not

if (!isWeekend & cigars <= 40) would read as
if not isWeekend and cigars less then or equal to 40
0
gudii9Author Commented:
1. if it isn't the weekend and I have 40 cigars or less then return true else if I have between 40 and 60 cigars inclusive return true else return false

2. if !weekend && cigars <= 40 return true else if weekend && cigars >= 40 && cigars <= 60 return true else return false

3. if (!isWeekend && cigars <= 40)
    return true;
else if (weekend && cigars >= 40 and cigars <= 60)
    return true;
else
    return false;

above i am still not completely clear.

i got till

3. if (!isWeekend

then why we get && ?
0
gudii9Author Commented:
else if I have between 40 and 60 cigars inclusive

how above translated to below


else if (weekend && cigars >= 40 and cigars <= 60)

i got second part ( ie cigars >= 40 and cigars <= 60))but not first part(weekend) as it was not given in the description right?


 if it isn't the weekend and I have 40 cigars or less then return true else if I have between 40 and 60 cigars inclusive return true else return false
0
awking00Commented:
To do logic puzzles, requires you to think logically. It doesn't matter what day of the week it is if you have fewer that 40 cigars return false. If you don't have fewer than 40 cigars, then you must have 40 or more so then, if you have 60 or fewer cigars, return true, then if you don't have fewer than 60 cigars, you must have more than 60, so then, if it's weekend return true and if not return false.
0
gudii9Author Commented:
is temp < 60 ---> Yes ---> return false
     |
   No
     |
    V
is temp <= 90 ---> Yes ---> return true
     |
    No
     |
    V
is temp <= 100 ---> No ---> return false
     |
   Yes
     |
    V
is it summer ---> Yes ---> return true
     |
    No
     |
    V
return false

Open in new window


how to translate above into code

after below step where i am struck

  if(temp<60)
return false;
else if(temp<=90)
return true;
else if(temp<=10)//if No then i have to do below steps...i know if yes to go below though


 No ---> return false
     |
   Yes
     |
    V
is it summer ---> Yes ---> return true
     |
    No
     |
    V
return false
0
gudii9Author Commented:
public boolean squirrelPlay(int temp, boolean isSummer) {
//return (temp<60)?false:(temp < =90)?true:(isSummer&& temp<=100)?true: false;
  if(temp<60)
return false;
else if(temp<=90)
return true;
else if(!(temp<=100))
return false;
else if(isSummer)
return true;
else
return false;
}

Open in new window

did i translate from picture to code as above correctly?

I did not like below that much but i did not get any other idea

else if(!(temp<=100))
0
gudii9Author Commented:
i guess i am not clear on below??

is temp <= 100 ---> No ---> return false
     |

does above condition checks temp is in between 90-100 whether no or not?//how this is false??

with summer yes is true i got that next part
0
awking00Commented:
If temp < 60 --> false (you got this)
else if temp <= 90  --> true (you got this)
if temp <= 100 --> No means it must be > 100
so the next condition is really if temp > 100 --> false no matter what season
so now temp must be between 91 and 100 so if summer --> true else --> false
0
gudii9Author Commented:
if temp <= 100 --> No means it must be > 100
it did not strike to me earlier
public boolean squirrelPlay(int temp, boolean isSummer) {
//return (temp<60)?false:(temp < =90)?true:(isSummer&& temp<=100)?true: false;
  if(temp<60)
return false;
else if(temp<=90)
return true;
else if(!(temp<=100))
return false;
else if(isSummer)
return true;
else
return false;
}

Open in new window


is my code is correct based on the picture you have given?
is temp < 60 ---> Yes ---> return false
     |
   No
     |
    V
is temp <= 90 ---> Yes ---> return true
     |
    No
     |
    V
is temp <= 100 ---> No ---> return false
     |
   Yes
     |
    V
is it summer ---> Yes ---> return true
     |
    No
     |
    V
return false

i am not habituated to  No to the right and Yes to yes bottom as above

I am habituated more to Yes to right and No to bottom. But your picture is graduall fall through or flow through
0
gudii9Author Commented:
my original code

else if(temp<=100&&isSummer==true)
return true;
else
return false;

can be replaced as below??

else if(!(temp<=100))
return false;
else if(isSummer)
return true;
else
return false;


I wonder what is flaw in my code? i definitely see it is much longer. Any other flaws there?
0
gurpsbassiCommented:
why not replace
else if(isSummer)
return true;
else
return false;


with just

return isSummer
0
gudii9Author Commented:
public boolean squirrelPlay(int temp, boolean isSummer) {
//return (temp<60)?false:(temp < =90)?true:(isSummer&& temp<=100)?true: false;
  if(temp<60)
return false;
else if(temp<=90)
return true;
else if(!(temp<=100))
return false;
else return isSummer;
}

Open in new window


this also passed all tests
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.