dateFashion java challenge

Hi,
I am working on below challenge
http://codingbat.com/prob/p103360
i wrote as below
public int dateFashion(int you, int date) {
int result_No=0;
int result_Yes=2;
int result_MayBe=1;
  if(you>=8 && date>=8){
  return result_Yes;
  }
  
  if(you<=2 && date<=2){
  return result_No;
  }
  
 
  return result_MayBe;
}

Open in new window



i am failing below

Expected      Run            
dateFashion(5, 10) → 2      1      X         
dateFashion(5, 2) → 0      1      X         
dateFashion(5, 5) → 1      1      OK         
dateFashion(3, 3) → 1      1      OK         
dateFashion(10, 2) → 0      1      X         
dateFashion(2, 9) → 0      1      X         
dateFashion(9, 9) → 2      2      OK         
dateFashion(10, 5) → 2      1      X         
dateFashion(2, 2) → 0      0      OK         
dateFashion(3, 7) → 1      1      OK         
dateFashion(2, 7) → 0      1      X         
dateFashion(6, 2) → 0      1      X         
other tests
X       

Please advise on how to fix and improve
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.

gudii9Author Commented:
public int dateFashion(int you, int date) {
int result;

  if((you>=8 || date>=8)&& !(you<=2 || date<=2)){
  return result=2;
  }
  
  else if(you<=2 ||date<=2){
  return result=0;
  }
  
 else
 return result=1;
  
}

Open in new window

below passed all.
How can i improve. please advise
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Get rid of the result variable, it isn't doing anything at all, and if you re-order the if statements then you can get rid of some of the conditions, like this...

public int dateFashion(int you, int date) {
  if(you<=2 ||date<=2){
    return 0;
  }
  if(you>=8 || date>=8){
    return 2;
  }
  return 1;
}

Open in new window

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
Jim CakalicSenior Developer/ArchitectCommented:
public int dateFashion(int you, int date) {
  return you <= 2 || date <= 2 ? 0 : you >= 8 || date >= 8 ? 2 : 1;   
}

Open in new window

Jim
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.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
@Jim, yes it's a one liner but which is easier to comprehend? ;)
0
gudii9Author Commented:
public int dateFashion(int you, int date) {
  if(you<=2 ||date<=2){
    return 0;
  }
  if(you>=8 || date>=8){
    return 2;
  }
  return 1;
}

Open in new window


can i write above code like below instead of if followed by other if can i write if followed by else if




public int dateFashion(int you, int date) {
  if(you<=2 ||date<=2){
    return 0;
  }
 else if(you>=8 || date>=8){
    return 2;
  }
  return 1;
}

Open in new window

0
gudii9Author Commented:
public int dateFashion(int you, int date) {
  if(you<=2 ||date<=2){
    return 0;
  }
  else if(you>=8 || date>=8){
    return 2;
  }
  return 1;
}

above also passed all.

If with if scenario the flow goes though both if related code right
whereas
if with else-if goes through either if or else-if right but never both same time which seems more efficient right?
please advise
0
Jim CakalicSenior Developer/ArchitectCommented:
If each block wasn't returning that might be true. In this case, it doesn't matter because the return when you or date <= 2 prevents the next of front being evaluated so structuring it as if/if or if/else if doesn't impact the efficiency.

@mccarl I find the ternary to be just as readable and more expressive of the intent of the method. If the expression was more complex I might extract a comparison method or introduce a temp variable.
0
gudii9Author Commented:
If each block wasn't returning that might be true. In this case, it doesn't matter because the return when you or date <= 2 prevents the next of front being evaluated so structuring it as if/if or if/else if doesn't impact the efficiency.

can you please elaborate with example. I did not get completely?
0
gudii9Author Commented:
public int dateFashion(int you, int date) {
  if(you<=2 ||date<=2){
    return 0;
  }
  else if(you>=8 || date>=8){
    return 2;
  }
  return 1;
}

Open in new window

my code is returning in each block if (0)and also else-if so(2) thats why it does not impact efficiency structuring it as if/if or if/else if ?
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.