front11 challenge

Hi,

I am trying below challenge

http://codingbat.com/prob/p128270

public int[] front11(int[] a, int[] b) {
  int len1=a.length;
  int len2=b.length;
  
  int[] c=new int[2];
  
  if(len1>=1&len2>=1){  
  c[0]=a[0];
  c[1]=b[1];
  return c;
  }
  
    if(len1>=1&len2==0){  
  c[0]=a[0];
  c[1]=0;
  return c;
  }
  
  
  
    if(len1==0&len2>=1){  
  c[0]=0;
  c[1]=b[1];
  return c;
  }
  
  else{
  return null;
  }
  
}

Open in new window


i failed tests.

when to say
c[0]=0;
when to say
c[0]==0;

how to fix and improve my code and alternate ways of doing it. 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.

ozoCommented:
say c[0]=0; when you are setting c[0] to 0
say c[0]==0 when you are testing whether c[0] is 0
0
ozoCommented:
public int[] front11(int[] a, int[] b) {
  int lena=a.length;
  int lenb=b.length;
 
  int[] c=new int[(lena>0?1:0)+(lenb>0?1:0)];
 
  if(lena>=1&lenb>=1){  
  c[0]=a[0];
  c[1]=b[0];
  }
 
  if(lena>=1&lenb==0){  
  c[0]=a[0];
  }
 
  if(lena==0&lenb>=1){  
  c[0]=b[0];
  }
 
  return c;
 }
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
krakatoaCommented:
public int[] front11(int[] a, int[] b) {
  return a.length==0&&b.length==0?new int[]{}:a.length==0?new int[] {b[0]}:b.length==0?new int[]{a[0]}:new int[]{a[0],b[0]};
}

Open in new window

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.

krakatoaCommented:
Here's another, short "if" arrangement :

public int[] front11(int[] a, int[] b) {
  if (a.length+b.length==0){return new int[]{};}
  if(a.length==0){return new int[]{b[0]};}
  if(b.length==0){return new int[] {a[0]};}
  return new int[]{a[0],b[0]};
}

Open in new window

0
krakatoaCommented:
. . .  and in this bit of your code :

else{
  return null;
  }

Open in new window


you are returning 'null'. But the challenge asks you to return an int array  - so why are you returning 'null'? You must return the required type. If the product of one of your code passages holds no values, then return an empty container of that type - in this case an empty int[].
0
gudii9Author Commented:
public int[] front11(int[] a, int[] b) {
  int len1=a.length;
  int len2=b.length;
  
  int[] c=new int[(len1>0?1:0)+(len2>0?1:0)];
  
  if(len1>=1&len2>=1){  
  c[0]=a[0];
  c[1]=b[0];
  return c;
  }
  
    if(len1>=1&len2==0){  
  c[0]=a[0];
  //c[1]=0;
  return c;
  }
  
  
  
    if(len1==0&len2>=1){  
 // c[0]=0;
  c[1]=b[1];
  return c;
  }
  
  else{
  return null;
  }
  
}

Open in new window


i modified as above and failing few tests. please advise where i am making mistake
0
gudii9Author Commented:
public int[] front11(int[] a, int[] b) {
  int len1=a.length;
  int len2=b.length;
  
  int[] c=new int[(len1>0?1:0)+(len2>0?1:0)];
  
  if(len1>=1&len2>=1){  
  c[0]=a[0];
  c[1]=b[0];
  return c;
  }
  
    if(len1>=1&len2==0){  
  c[0]=a[0];
  //c[1]=0;
  return c;
  }
  
  
  
    if(len1==0&len2>=1){  
 // c[0]=0;
  c[1]=b[0];
  return c;
  }
  
  else{
  return null;
  }
  
}

Open in new window


i corrected below highlighted
 // c[0]=0;
  c[1]=b[0];
  return c;

still getting error. please advise





Show output only (no red/green)
prev  |  next  |  chance   |  CodingBat  >  Array-1      
Expected      Run            
front11({1, 2, 3}, {7, 9, 8}) → {1, 7}      {1, 7}      OK         
front11({1}, {2}) → {1, 2}      {1, 2}      OK         
front11({1, 7}, {}) → {1}      {1}      OK         
front11({}, {2, 8}) → {2}      {xception:java.lang.ArrayIndexOutOfBoundsException: 1 (line number:23}      X         
front11({}, {}) → {}      {ul}      X
0
gudii9Author Commented:
i corrected further as highlighted

public int[] front11(int[] a, int[] b) {
  int len1=a.length;
  int len2=b.length;
 
  int[] c=new int[(len1>0?1:0)+(len2>0?1:0)];
 
  if(len1>=1&len2>=1){  
  c[0]=a[0];
  c[1]=b[0];
  return c;
  }
 
    if(len1>=1&len2==0){  
  c[0]=a[0];
  //c[1]=0;
  return c;
  }
 
 
 
    if(len1==0&len2>=1){  
 // c[0]=0;
  c[0]=b[0];
  return c;
  }
 
  else{
  return null;
  }
 
}

still failing one test case

front11({}, {}) → {}      {ul}      X         
please advise
0
ozoCommented:
public int[] front11(int[] a, int[] b) {
  int len1=a.length;
  int len2=b.length;
  
  int[] c=new int[(len1>0?1:0)+(len2>0?1:0)];
  
  if(len1>=1&len2>=1){  
  c[0]=a[0];
  c[1]=b[0];
  return c;
  }
  
    if(len1>=1&len2==0){  
  c[0]=a[0];
  //c[1]=0;
  return c;
  }
  
  
  
    if(len1==0&len2>=1){  
 // c[0]=0;
  c[0]=b[0]; //  c[1]=b[1];
  return c;
  }
  
  else{
  return c; //return null;
  }
  
}

Open in new window

0
krakatoaCommented:
public int[] front11(int[] a, int[] b) {
   int len1=a.length;
  int len2=b.length;
  
  int[] c=new int[(len1>0?1:0)+(len2>0?1:0)];
  
  if(len1>=1&len2>=1){  
  c[0]=a[0];
  c[1]=b[0];
  return c;
  }
  
    if(len1>=1&len2==0){  
  c[0]=a[0];
  //c[1]=0;
  return c;
  }
  
  
  
    if(len1==0&len2>=1){  
 // c[0]=0;
  c[0]=b[0];
  return c;
  }
  
  else{
  return new int[]{};
  }
  
}

Open in new window

0
gudii9Author Commented:
public int[] front11(int[] a, int[] b) {
  int len1=a.length;
  int len2=b.length;
  
  int[] c=new int[(len1>0?1:0)+(len2>0?1:0)];
  
  if(len1>=1&len2>=1){  
  c[0]=a[0];
  c[1]=b[0];
  return c;
  }
  
    if(len1>=1&len2==0){  
  c[0]=a[0];
  //c[1]=0;
  return c;
  }
  
  
  
    if(len1==0&len2>=1){  
 // c[0]=0;
  c[0]=b[0]; //  c[1]=b[1];
  return c;
  }
  
  else{
  return c; //return null;
  }
  
}

Open in new window


above solution worked but it has shaken my wrong concept that 'method needs to have separate return statement apart from if block return statement and else block return statement'
0
krakatoaCommented:
0
ozoCommented:
The correct concept is that all paths need a return
(unless the method is declared void)
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

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.