Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Lesson 34 Coding Activity 6
- *
- * For the Lesson 34 activities, you will be asked to write one or more methods.
- * Use the template to write a main method that tests each of your methods,
- * then paste everything into the code runner box. Your submission should
- * begin with the first import statement and end with the final }.
- *
- * Write a method that takes an array of ints and returns true if all
- * of the values in the array are positive. If the array contains any
- * negative integers, it should return false.
- *
- * public static boolean allPositive(int [] a)
- *
- */
- import java.util.Scanner;
- import java.lang.Math;
- class Lesson_34_Activity_Six {
- public static boolean allPositive(int [] a)
- {
- int flag = 0;
- for (int i = 0; i < a.length; i++)
- {
- if (a[i] + Math.abs(a[i]) == 0)
- flag++;
- }
- if (flag != 0)
- return false;
- return true;
- }
- public static void main(String[] args)
- {
- int a[] = {1,2,3,-4,5};
- System.out.println(allPositive(a));
- }
- }
Add Comment
Please, Sign In to add comment