Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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
Avatar of ozo
ozo
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

. . .  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[].
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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

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

Avatar of gudii9

ASKER

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'
The correct concept is that all paths need a return
(unless the method is declared void)