java count positive create a program to count the number of positive number from
ID: 3781609 • Letter: J
Question
java count positive
create a program to count the number of positive number from the given number list
example:
> java A1Q1 1 2 3 0 5 -6 9
5
> java A1Q1 -5 -6 -9
0
details:
public class A1Q1 {
/**
* Returns the number of strictly positive elements in elems.
* We assume that the list is not null.
*
* @param elems the list of integers
* @return the number of strictly positive elements in elems
*/
private static int countPositive(int[] elems) {
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* The main method of this program. Gets an array of
* strings as input parameter. The array is assumed to
* be non-null, and all the strings in the array are
* parsable as integer.
*
* The function prints out the number of positive
* integers parsed in args
* @param args space-separated list of strings parsable as integers
*/
public static void main(String[] args) {
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
}
Explanation / Answer
import java.io.*;
public class A1Q1{
private static int countPositive(int[] elems)
{
int [] a = elems;
int pos_number = 0;
for(int i=0;i<a.length;i++)
{
if(a[i] > 0 )
{
pos_number = pos_number + 1;
}
}
return pos_number;
}
public static void main(String[] args)
{
int count;
int a[] = new int[args.length];
for(int i=0;i<args.length ;i++)
{
a[i] = Integer.parseInt(args[i]);
}
count = countPositive(a);
System.out.println(count);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.