AlarmClock Challenge

Hi,

I am working on below chalenge

http://codingbat.com/prob/p160543

I wrote as below

public String alarmClock(int day, boolean vacation) {

		if(day==0||day==6){

		if(vacation){
		return "off";
		}
		else{
		return "10:00";
		}  
		
		}
		
		else {
		if(vacation){
		return "10:00";
		}else{
		return "7:00";
		}
		}
		
	}

Open in new window


How do i improve my code.

To start appoaching this problem i tok a paper and pen and wrote  3 variables
1. day starting 0 to 6
2. vacation yes or no
3. this third variable not being passed as method variable/parameter but extra one whose name is weekend or not

Then  wrote if then else loop. Again in If i put nested if similary i put inside else nested if.

How to draw a flow diagram to this to understand the flow clearly as there are 3 variables here not 1 variable(if one i can draw flow chart/diagram).

Eclipse has built in UML tool or plugin to draw these kind of diagram?
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.

ozoCommented:
public String alarmClock(int day, boolean vacation) {
  String alarm[][]={
    {"7:00","10:00"},
    {"10:00","off"}
  };
  return alarm[vacation?1:0][day==0||day==6?1:0];
}
0
gudii9Author Commented:
 return alarm[vacation?1:0][day==0||day==6?1:0];

Open in new window


can you please elaborate.

if vacation is true then forst element of array {"7:00","10:00"},
otherwise second element  {"10:00","off"}

then i have not understood?
0
gudii9Author Commented:
also how to put in diagram these kind of challenges with 3 variables?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.

ozoCommented:
If vacation is true then 1st element of array {
    {"7:00","10:00"},
    {"10:00","off"}
  }
which is  {"10:00","off"}
otherwise the 0th element,
which is {"7:00","10:00"}
0
ozoCommented:
A 2 dimensional array can explicitly represent a 2 dimensional diagram.
A 3 dimensional diagram could be explicitly represented by a 3 dimensional array.

3 boolean variables might be diagrammed like
https://upload.wikimedia.org/wikipedia/commons/6/6b/Venn_diagram_ABC_BW_Explanation.png
or
http://www.xkcd.com/112/
0
gudii9Author Commented:
how to relate 3 intersection circles with this 3 dimensional challenge lets say this AlarmClock challenge to design the solution?
0
ozoCommented:
I would treat the AlarmClock challenge as 2 dimensional.
0
gudii9Author Commented:
return alarm[vacation?1:0][day==0||day==6?1:0];

in above solution i understand below

return alarm[vacation?1:0]

but i have not understood below?
[day==0||day==6?1:0];

why we need to add above also??
0
gudii9Author Commented:
return alarm[vacation?1:0][day==0||day==6?1:0];
i thought we should say like below instead of above?

return alarm[vacation?1:0][day==0||day==6?0:1];
0
gudii9Author Commented:
1. day starting 0 to 6
2. vacation yes or no
3. this third variable not being passed as method variable/parameter but extra one whose name is weekend or not

when to treat as 2D array when to treat as 3 D array?

please advise
0
ozoCommented:
i thought we should say like below instead of above?

return alarm[vacation?1:0][day==0||day==6?0:1];
You could do that, in which case you would use
 String alarm[][]={
    {"10:00","7:00"},
    {"off","10:00"}
  };
0
ozoCommented:
when to treat as 2D array when to treat as 3 D array?
If you can draw the Venn diagram with 2 circles, it's usually simpler to use a 2D array.
0
gudii9Author Commented:
what is first circle and what is second circle in this challenge?How you repesent. Can you please share the diagram for clear understanding of it?

please advise
0
ozoCommented:
Alarm.pngThe simplicity of the 2D diagram could also suggest a simpler 1D answer:
  String alarm[]={"7:00","10:00","off"};
  return alarm[(vacation?1:0)+(day==0||day==6?1:0)];
0
gudii9Author Commented:
i like above diagram whole outer rectange represents weekday right whih has 7 in it?
0
gudii9Author Commented:
more than one ? always confuses me.

i did not get this last part as below in 1D solution  (  String alarm[]={"7:00","10:00","off"};
  return alarm[(vacation?1:0)+(day==0||day==6?1:0)];)


+(day==0||day==6?1:0)];

can you please advise
0
gudii9Author Commented:
i made some sketch like below

 isweekday -----yes----vacation---no--7
   |                              yes--10
   |
   No-------------------vacation---no--10
                                   yes--off
0
gudii9Author Commented:
public String alarmClock(int day, boolean vacation) {
  String alarm[][]={
    {"7:00","10:00"},
    {"10:00","off"}
  };
  return alarm[vacation?1:0][day==0||day==6?1:0];
}
above 2D solution

below 1D solution
String alarm[]={"7:00","10:00","off"};
  return alarm[(vacation?1:0)+(day==0||day==6?1:0)];

Open in new window


how return statement for both 2D and 1D solutons are same.

Also how above solutions are different from my orignal solution except for ternary. please advise

public String alarmClock(int day, boolean vacation) {

		if(day==0||day==6){

		if(vacation){
		return "off";
		}
		else{
		return "10:00";
		}  
		
		}
		
		else {
		if(vacation){
		return "10:00";
		}else{
		return "7:00";
		}
		}
		
	}

Open in new window

0
gudii9Author Commented:
String alarm[]={"7:00","10:00","off"};
  return alarm[(vacation?1:0)+(day==0||day==6?1:0)];

Open in new window

?

in above solution why we have not used off(ie 3rd elemnt of array with index 2)?
how it is still working fine?
0
ozoCommented:
when vacation is true and day is 0 or 6,
(vacation?1:0) is 1 and (day==0||day==6?1:0) is 1
1+1=2
0
gudii9Author Commented:
1D solution is clear now.
2D solution as below not yet clear
public String alarmClock(int day, boolean vacation) {
  String alarm[][]={
    {"7:00","10:00"},
    {"10:00","off"}
  };
  return alarm[vacation?1:0][day==0||day==6?1:0];
}

Open in new window

if vacation true then it is 1 then there is no + right then how 2 corresponding to off will come?

can you please elaborate on below line??

  return alarm[vacation?1:0][day==0||day==6?1:0];
0
gudii9Author Commented:
i see alarmClock(1, false) we got 7 as we returned alarmClock[0,0]
0
gudii9Author Commented:
i got both 1D and 2D ssolutions now

The simplicity of the 2D diagram could also suggest a simpler 1D answer:
  String alarm[]={"7:00","10:00","off"};
  return alarm[(vacation?1:0)+(day==0||day==6?1:0)];

How the venn diagram suggested above solution?
How did you go about writing above code based on the venn diagram. please advise
0
ozoCommented:
I think the array looks like the diagram, which to me makes it easier to see the correspondence between the challenge and the code
               weekend
               no | yes
            ------------
          no| 7:00|10:00
vacation ---------+-----
         yes|10:00| off

Open in new window

but if you notice that the array has a symmetry along the diagonal,
you can see that you can project the 2 dimensions onto 1 dimension
0
gudii9Author Commented:
in venn diagram where are we representing week day. I see only 2 circlese one for weekend other for vacation?
0
gudii9Author Commented:
how to write solution as 1D array rather than 2D array?
0
gudii9Author Commented:
can we draw weekday as separte 3rd circle?
0
ozoCommented:
Weekday is the same as not(weekend), so the weekday region would be the outside of the weekend circle.

Or you could turn the circle inside out, and draw it with weekday as the inside and weekend as the outside.
0
gudii9Author Commented:
Weekday is the same as not(weekend),

sorry i am not clear. can you elaborate with modified picture

so the weekday region would be the outside of the weekend circle.
0
ozoCommented:
Everything outside the green circle in http:#a41224304 is weekday
0
gudii9Author Commented:
where is intersection of weekday and vacation represented in diagram?
0
ozoCommented:
Outside the green circle, and inside the blue circle
0
gudii9Author Commented:
public String alarmClock(int day, boolean vacation) {

              String alarm[][]={
    {"7:00","10:00"},
    {"10:00","off"}
  };
  return alarm[vacation?1:0][day==0||day==6?1:0];
            
      }

since 2D array only 4 possible answers
alarm[0][0] or
alarm[0][1] or
alarm[1][0] or
alarm[1][1]
if 1 then only alarm[1][x] will come x is 0 or 1depending on weekend or not day==0||day==6 accordingly  either alarm[1][0] or
alarm[1][1]  printed
0
gudii9Author Commented:
solution 1:
public String alarmClock(int day, boolean vacation) {
            if(day==0||day==6){
            if(vacation){
            return "off";
            }
            else{
            return "10:00";
            }              
            }            
            else {
            if(vacation){
            return "10:00";
            }else{
            return "7:00";
            }
            }            
      }
solution 2:
public String alarmClock(int day, boolean vacation) {
              String alarm[][]={
    {"10:00","7:00"},
    {"off","10:00"}
  };
  return alarm[vacation?1:0][day==0||day==6?0:1];
            
      }
solution 3:
public String alarmClock(int day, boolean vacation) {
              String alarm[][]={
    {"7:00","10:00"},
    {"10:00","off"}
  };
  return alarm[vacation?1:0][day==0||day==6?1:0];            
      }
solution 4:public String alarmClock(int day, boolean vacation) {
String alarm[]={"7:00","10:00","off"};
  return alarm[(vacation?1:0)+(day==0||day==6?1:0)];
}
solution 5:public String alarmClock(int day, boolean vacation) {
String alarm[]={"10:00","7:00","off"};
  return alarm[(vacation?1:0)+(day==0||day==6?0:1)];
}




i see total 5 solutions.

i wonder solution 5 is not working by jumbling last 0 and 1 and rest simlar to solution 4. please advise on how to fix that?
0
ozoCommented:
solution 5 could be fixed with
return alarm[((vacation?2:0)+(day==0||day==6?0:1))%3];
but it would make more sense to have "10:00" in between "off" and "7:00"
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
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.