Advertisement
StefanTobler

Lesson 34 Activity 1

Dec 8th, 2016
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. /*
  2.  * Lesson 34 Coding Activity 1
  3.  *
  4.  * For the Lesson 34 activities, you will be asked to write one or more methods.
  5.  * Use the template to write a main method that tests each of your methods,
  6.  * then paste everything into the code runner box. Your submission should
  7.  * begin with the first import statement and end with the final }.
  8.  *
  9.  * Write a method that takes an array of ints as a parameter and
  10.  * returns the sum of integers in the array.
  11.  *
  12.  *     public static int sum(int [] a)
  13.  *
  14.  * For example, sum(a); would return 6 if a = {1, 2, 3} or 3 if a = {1, 1, 1}.
  15.  */
  16.  
  17.  
  18. import java.util.Scanner;
  19.  
  20. class Lesson_34_Activity_One {
  21.  
  22.    public static int sum(int [] a)
  23.     {
  24.      int sum = 0;
  25.     for (int i = 0; i < a.length; i++)
  26.     {
  27.       sum += a[i];
  28.     }
  29.      return sum;
  30.     }
  31.  
  32.     public static void main(String[] args)
  33.      {
  34.       int a[] = {1,2,3};
  35.       System.out.println(sum(a));
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement