1 nt sumNeg(int[] vals) 2- I Description Given an array of ints, iterate over th
ID: 3690116 • Letter: 1
Question
1 nt sumNeg(int[] vals) 2- I Description Given an array of ints, iterate over the array. Skip any elements which are not negative; add the negative ones together, and return the sum. WARNING: The array might have zero elements - or it might have some elements, but all of them are zero or positive! In that case, return zero. (But this should be easy - it won't require any special code, if you code your loop correctly.) 4 Input: vals- an array of ints Output: The sum of the negative elements from vals Hint: This is the same code as a simple sum-the-elements loop, except that you need to add a simple if) loop to it.Explanation / Answer
Hi, I have implemented all of your methods in a class.
public class Main {
//1
int sumNeg(int[] vals){
int sum = 0;
if(vals == null || vals.length ==0)
return 0;
for(int i=0; i<vals.length; i++){
if(vals[i] < 0)
sum = sum + vals[i];
}
return sum;
}
//2
boolean multiple3or5(int val){
if(val%3==0 || val%5==0)
return true;
else
return false;
}
//3
String concatString(String str1, String str2){
return str1+str2;
}
//4
boolean booleanEnd(boolean b1, boolean b2){
if(b1 && b2) // both are true
return true;
else if(!b1 && !b2) // both are false
return true;
else
return false;
}
//5
boolean strCatAndEq(String str1, String str2, String strX){
if(strX.equals(str1+str2))
return true;
else
return false;
}
//6
boolean modEq1(int a, int b){
if(a%b == 1)
return true;
else
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.