gudii9
asked on
front11 challenge
Hi,
I am trying below challenge
http://codingbat.com/prob/p128270
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
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;
}
}
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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]};
}
. . . and in this bit of your code :
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[].
else{
return null;
}
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[].
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;
}
}
i modified as above and failing few tests. please advise where i am making mistake
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;
}
}
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.ArrayIndexOutOfB oundsExcep tion: 1 (line number:23} X
front11({}, {}) → {} {ul} X
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
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;
}
}
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[]{};
}
}
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;
}
}
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)
(unless the method is declared void)
say c[0]==0 when you are testing whether c[0] is 0