Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

X. (10 points) In the box provided show the output for this program segment publ

ID: 3909849 • Letter: X

Question

X. (10 points) In the box provided show the output for this program segment public static void main (String I args) intl num-(1,2.3,4) System out printin(x+y+ num[2) tryit(y, x, num) System.out printin(x+ynum(2) public static void tryit(int a, int b, intll c) int x c[2]-20 System.out printin(x+ bc2) 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 array

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