StefanTobler

Lesson 34 Activity 2

Dec 8th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. /*
  2.  * Lesson 34 Coding Activity 2
  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
  10.  * and returns the average value of the array as a double.
  11.  *
  12.  *     public static double average(int [] a)
  13.  *
  14.  * For example, average(a) would return 2.0
  15.  * if a = {1, 2, 3} or 1.0 if a = {1, 1, 1}.
  16.  */
  17.  
  18.  
  19. import java.util.Scanner;
  20.  
  21. class Lesson_34_Activity_Two {
  22.  
  23.    public static double average(int [] a)
  24.     {
  25.      double avg = 0;
  26.     for (int i = 0; i < a.length; i ++)
  27.     {
  28.       avg += a[i];
  29.     }
  30.     avg = avg/a.length;
  31.      return avg;
  32.     }
  33.  
  34.     public static void main(String[] args)
  35.      {
  36.       int a[] = {1, 2, 3};
  37.       System.out.println(average(a));
  38.     }
  39. }
Add Comment
Please, Sign In to add comment