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 array

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
X. (10 points) In the box provided show the output for this program segment public static...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT