StefanTobler

Lesson 34 Activity 6

Dec 8th, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. /*
  2.  * Lesson 34 Coding Activity 6
  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 and returns true if all
  10.  * of the values in the array are positive. If the array contains any
  11.  * negative integers, it should return false.
  12.  *
  13.  *     public static boolean allPositive(int [] a)
  14.  *
  15.  */
  16.  
  17.  
  18. import java.util.Scanner;
  19. import java.lang.Math;
  20.  
  21. class Lesson_34_Activity_Six {
  22.  
  23.   public static boolean allPositive(int [] a)
  24.     {
  25.     int flag = 0;
  26.     for (int i = 0; i < a.length; i++)
  27.      {
  28.       if (a[i] + Math.abs(a[i]) == 0)
  29.          flag++;
  30.      }
  31.      if (flag != 0)
  32.      return false;
  33.      return true;
  34.     }
  35.  
  36.     public static void main(String[] args)
  37.      {
  38.      int a[] = {1,2,3,-4,5};
  39.      System.out.println(allPositive(a));
  40.     }
  41. }
Add Comment
Please, Sign In to add comment