?
Solved

boolean program

Posted on 2014-10-02
15
Medium Priority
?
138 Views
Last Modified: 2014-10-07
Hi,

I was trying below program challenge
http://codingbat.com/prob/p165701

public boolean loneTeen(int a, int b) {
  if(a>=13&a<=19){
  return true;
  }
  
   if(b>=13&b<=19){
  return true;
  }
  
  return false;
  
}

Open in new window

i wrote as above.

I have four test scenario cases failing

Expected      Run            
loneTeen(13, 99) → true      true      OK         
loneTeen(21, 19) → true      true      OK         
loneTeen(13, 13) → false      true      X         
loneTeen(14, 20) → true      true      OK         
loneTeen(20, 15) → true      true      OK         
loneTeen(16, 17) → false      true      X         
loneTeen(16, 9) → true      true      OK         
loneTeen(16, 18) → false      true      X         
loneTeen(13, 19) → false      true      X         
loneTeen(13, 20) → true      true      OK         
loneTeen(6, 18) → true      true      OK         
loneTeen(99, 13) → true      true      OK         
loneTeen(99, 99) → false      false      OK         
Correct for more than half the tests

Please advise on why they are failing and how to fix it
Please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +2
15 Comments
 
LVL 28

Accepted Solution

by:
dpearson earned 800 total points
ID: 40358663
Your code is almost correct.

It's just not handling the case where both a and b are teens.  In that case, the instructions say to return false.

Since your code tests to see if a is a teen and if it is, immediately returns true, it gets the case wrong where both a and b are teens.

You may want to consider something like this instead?

public boolean loneTeen(int a, int b) {
  boolean aTeen = (a >= 13 && a <= 19) ;
  boolean bTeen = (b >= 13 && b <= 19) ;
  
  if(aTeen && !bTeen){
  return true;
  }
  
  if(!aTeen && bTeen){
  return true;
  }
  
  return false;

}

Open in new window

0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 400 total points
ID: 40358815
Or in short:

public boolean loneTeen(int a, int b) {
    return (isTeen(a) && !isTeen(b)) || (isTeen(b) && !isTeen(a));
}

private boolean isTeen(int value) {
    return value >= 13 && value <=19;
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40358911
//or
return isTeen(a) ^ isTeen(b);
0
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
LVL 37

Expert Comment

by:zzynx
ID: 40358922
Right you are :-)
0
 
LVL 7

Author Comment

by:gudii9
ID: 40361532
I wonder is there is a way i can modify and make my program to work without introducing aTeen and bTeen.
Why we need extra valraibles called aTeen and bTeen in this case.

if(13<=b<=19){
  return true;
 
  }



If i give both conditions together as above i am getting error below. Why i am getting below error. what it means. I thought it should work fine. Please advise

Error:      if(13<=b<=19){
         ^^^^^^^^^
The operator <= is undefined for the argument type(s) boolean, int


see Example Code to help with compile problems
0
 
LVL 7

Author Comment

by:gudii9
ID: 40361540
I tried like below but lot of test cases are failing
public boolean loneTeen(int a, int b) {
  if(13<=a & a <=19){
  return true;
  
  }
  
    if(13<=b & b<=19){
  return true;
  
  }
  
  
  if((13<=a & a <=19)&!(13<=b & b<=19)){
  return false;
  }
  
  if(!(13<=a & a <=19)&!(13<=b & b<=19)){
  return false;
  }
  
  return false;
}

Open in new window


Expected      Run            
loneTeen(13, 99) → true      true      OK         
loneTeen(21, 19) → true      true      OK         
loneTeen(13, 13) → false      true      X         
loneTeen(14, 20) → true      true      OK         
loneTeen(20, 15) → true      true      OK         
loneTeen(16, 17) → false      true      X         
loneTeen(16, 9) → true      true      OK         
loneTeen(16, 18) → false      true      X         
loneTeen(13, 19) → false      true      X         
loneTeen(13, 20) → true      true      OK         
loneTeen(6, 18) → true      true      OK         
loneTeen(99, 13) → true      true      OK         
loneTeen(99, 99) → false      false      OK         


Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40361546
public boolean loneTeen(int a, int b) {
  if(13<=a & a <=19){
  return true;
  
  }
  
    if(13<=b & b<=19){
  return true;
  
  }
  
  
  if((13<=a & a <=19)&!(13<=b & b<=19)){
  return false;
  }
  
  if(!(13<=a & a <=19)&!(13<=b & b<=19)){
  return false;
  }
  
  return false;
}

Open in new window


I wrote as above base on comment ID: 40358663
How is above program differenmt from program in comment ID: 40358663

public boolean loneTeen(int a, int b) {
  if(13<=a & a <=19){
  return true;
  
  }
  
    if(13<=b & b<=19){
  return true;
  
  }
  
  
  if((13<=a & a <=19)&!(13<=b & b<=19)){
  return false;
  }
  
  if(!(13<=a & a <=19)&!(13<=b & b<=19)){
  return false;
  }
  
  return true;
}

Open in new window


If i change the last method return staement from false to true more than half test cases are failing. I wonder why. I though i covered all the 3 major challenge issues in 3 if loops. So i thought method return type either true or false does not matter. But my results prove that i am wrong in my assumption. Please advise

Expected      Run            
loneTeen(13, 99) → true      true      OK         
loneTeen(21, 19) → true      true      OK         
loneTeen(13, 13) → false      true      X         
loneTeen(14, 20) → true      true      OK         
loneTeen(20, 15) → true      true      OK         
loneTeen(16, 17) → false      true      X         
loneTeen(16, 9) → true      true      OK         
loneTeen(16, 18) → false      true      X         
loneTeen(13, 19) → false      true      X         
loneTeen(13, 20) → true      true      OK         
loneTeen(6, 18) → true      true      OK         
loneTeen(99, 13) → true      true      OK         
loneTeen(99, 99) → false      false      OK
0
 
LVL 84

Expert Comment

by:ozo
ID: 40361602
loneTeen(13, 13) → false      true      X  
in this case, you should be returning false, but this code immediately returns true
 if(13<=a & a <=19){
  return true;
0
 
LVL 7

Author Comment

by:gudii9
ID: 40362472
public boolean loneTeen(int a, int b) {
  if(13<=a & a <=19){
  return true;
  
  }
  
    if(13<=b & b<=19){
  return true;
  
  }
  
  
  if((13<=a & a <=19)&!(13<=b & b<=19)){
  return false;
  }
  
  if(!(13<=a & a <=19)&!(13<=b & b<=19)){
  return false;
  }
  
  return false;
}

Open in new window


How do i fix above code so that it returns false when both a and b are teens. I do not want to introduce aTeen and bTeen if possible. Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40362477
public boolean loneTeen(int a, int b) {
if((13<=a & a <=19)&!(13<=b & b<=19)){
  return true;
  }
  
  
if(!(13<=a & a <=19)&(13<=b & b<=19)){
  return true;
  }
  
  return false;
}

Open in new window


I wrote as above and passed all the test cases


Expected      Run            
loneTeen(13, 99) → true      true      OK         
loneTeen(21, 19) → true      true      OK         
loneTeen(13, 13) → false      false      OK         
loneTeen(14, 20) → true      true      OK         
loneTeen(20, 15) → true      true      OK         
loneTeen(16, 17) → false      false      OK         
loneTeen(16, 9) → true      true      OK         
loneTeen(16, 18) → false      false      OK         
loneTeen(13, 19) → false      false      OK         
loneTeen(13, 20) → true      true      OK         
loneTeen(6, 18) → true      true      OK         
loneTeen(99, 13) → true      true      OK         
loneTeen(99, 99) → false      false      OK         


Does it look fine?
Please advise
0
 
LVL 28

Expert Comment

by:dpearson
ID: 40362578
Yes this is fine.  You should be using "&&" instead of just "&" but that's all.

Can you see how this code is the same as this code (using the variables) except you've written out the test (a >= 13 && a <= 19) each place where the variable would go?

public boolean loneTeen(int a, int b) {
  boolean aTeen = (a >= 13 && a <= 19) ;
  boolean bTeen = (b >= 13 && b <= 19) ;
 
  if(aTeen && !bTeen){
  return true;
  }
 
  if(!aTeen && bTeen){
  return true;
  }
 
  return false;
}

So yes, what you wrote is fine.

Doug
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 800 total points
ID: 40362581
If the goal is to pass the test cases, then it is looks fine for that.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40362827
Can you see how this code is the same as this code (using the variables) except you've written out the test (a >= 13 && a <= 19) each place where the variable would go?


yes i see it
0
 
LVL 32

Expert Comment

by:awking00
ID: 40366835
>>Why we need extra valraibles<<
Sometimes they can simplify coding and make it easier to understand. The following passes the test only using three lines of code employing two variables:
public static boolean loneTeen(int a, int b) {
      boolean aVal = a >= 13 && a <= 19;
      boolean bVal = b >= 13 && b <= 19;

      return aVal && !bVal || !aVal && bVal ? true : false;
      }
0
 
LVL 84

Expert Comment

by:ozo
ID: 40366873
//or,
  return (a >= 13 && a <= 19) ^ (b >= 13 && b <= 19);

//or,
  return (a >= 13 && a <= 19) != (b >= 13 && b <= 19);

//or,
  return a >= 13 ^ a <= 19 ^ b >= 13 ^ b <= 19;

//or,
  return a < 13 ^ b < 13 ^ a > 19 ^ b > 19;

//or even
  return (a-13 ^ b-13 ^ 19-a ^ 19-b)<0;
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question