X. (10 points) In the box provided show the output for this program segment publ
ID: 3912620 • Letter: X
Question
X. (10 points) In the box provided show the output for this program segment public static void main (String [) args) intl num-(1,2.3,4) System.out printinx+y+num12) tyit(y, x. num): System.out printin(x+ynum(2): public static void tryit(int a, int b, intl c) int x c[2]-20 System.out printin(x+bCI2) XI. (7 points) a. Write a method NEGATIVE that receives as parameters a one-dimensional array variable LIST of integers. The method should return the sum of the negative elements in the array. You may not assume you know the values in the arrayExplanation / Answer
Output for VIII
3 4 3
7 4 3 20
3 4 20
Explanation:
public class Tester {
public static void main(String[] args){
int x=3, y=4;
int[] num = {1,2,3,4};
//Printing x, y, array 3rd element with spaces in between
System.out.println(x+" "+y+" "+num[2]);
//This method doesn't return anything, so next output is same as previous one
tryit(y,x,num);
//Printing x, y, array 3rd element with spaces in between
System.out.println(x+" "+y+" "+num[2]);
}
public static void tryit(int a, int b, int[] c) {
// TODO Auto-generated method stub
int x;
x=a+b;
c[2]=20;
System.out.println(x+" "+a+" "+b+" "+c[2]);
}
}
//End of Tester.java class
Program for XI:
import java.util.Arrays;
public class Tester {
public static void main(String[] args){
int[] num = {1,2,-3,-4};
//Printing array before calling the method
System.out.println(Arrays.toString(num));
//This method doesn't return anything, so next output is same as previous one
int sum = negative(num);
//Printing the sum
System.out.println("Negative sum = "+sum);
}
public static int negative(int[] num) {
// TODO Auto-generated method stub
int total=0;
//iterating over array
for (int i=0; i<num.length; i++){
//selecting only negative numbers
if(num[i]<0){
total = total+num[i];
}
}
//return negative sum
return total;
}
}
//End of Tester.java class
Output:
[1, 2, -3, -4]
Negative sum = -7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.