Question

Create 3 user-defined methods called add(). 1 accepts a single integer array parameter; this method should...

Create 3 user-defined methods called add().

1 accepts a single integer array parameter; this method should loop the array adding the values and return the summation (use 4, 1, 4, 6, 10, 15, 32, 79, 18 as the values in the array)

1 accepts 2 integer parameters; this method should add them and return the value

1 accepts 3 integer parameters; this method should add them all and return the value

In main, define the parameters above (you may use random number generators if you like), output the values. Example:

Method 1 sum: 1234

Method 2 sum: 1234

Method 3 sum: 1234

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class AddNumbers {

    public static int add(int arr[]) {
        int sum = 0;
        for(int i = 0; i < arr.length; ++i) {
            sum += arr[i];
        }
        return sum;
    }

    public static int add(int n1, int n2) {
        return n1 + n2;
    }

    public static int add(int n1, int n2, int n3) {
        return n1 + n2 + n3;
    }

    public static void main(String[] args) {
        System.out.println("Method 1 sum: " + add(new int[] {4, 1, 4, 6, 10, 15, 32, 79, 18}));
        System.out.println("Method 2 sum: " + add(8, 4));
        System.out.println("Method 3 sum: " + add(8, 4, 7));
    }

}

\color{blue}Hey,\;Please\;let\;me\;know\;if\;you\;need\;me\;to\;make\;any\;changes. \\ I\;would\;really\;appreciate\;an\;upvote.\;Thank\;you!

Add a comment
Know the answer?
Add Answer to:
Create 3 user-defined methods called add(). 1 accepts a single integer array parameter; this method should...
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
ADVERTISEMENT