1. Write a static method that takes an array of integers as a parameter and retu
ID: 3597903 • Letter: 1
Question
1. Write a static method that takes an array of integers as a parameter and returns true if the count of strictly positive values is larger than the count of strictly negative values in the array, false otherwise. A number is strictly positive if it is strictly larger than zero (i.e. it cannot equal zero). A number is strictly negative if it is strictly less than zero.
2. Write a static method that takes an array of integers as a parameter and returns back an integer that is the sum of all of the integers in the array.
3. What does this method do? In one short English sentence describe the task accomplished by this method.
1
2
3
4
5
6
7
8
9
10
public static ArrayList<Integer> foo(ArrayList<Integer> a)
{
ArrayList<Integer> b = new ArrayList<Integer>();
for (int i = 0; i < a.size(); i++) {
if (a.get(i) < 0) {
b.add(i);
}
}
return b;
}
4. What does this method do? In one short English sentence describe the task accomplished by this method.
1
2
3
4
5
6
7
8
9
10
11
public static ArrayList<String> bar(ArrayList<String> a)
{
ArrayList<String> tmp = new ArrayList<String>();
for (int i = 0; i < a.size(); i++) {
if (a.get(i).length() > 10) {
tmp.add(a.get(i));
}
}
return tmp;
}
5. Write a static method named concatenate that takes two ArrayLists of Strings as a parameter and returns a new ArrayList of Strings that contains all of the elements of the first list followed by all of the elements of the second list.
1
2
3
4
5
6
7
8
9
10
public static ArrayList<Integer> foo(ArrayList<Integer> a)
{
ArrayList<Integer> b = new ArrayList<Integer>();
for (int i = 0; i < a.size(); i++) {
if (a.get(i) < 0) {
b.add(i);
}
}
return b;
}
Explanation / Answer
1.
public static boolean checkPositive(List<Integer> values) {
int posCount = 0, negCount = 0;
for(Integer i: values) {
if(i > 0) {
posCount++;
} else if(i < 0) {
negCount++;
}
}
return posCount > negCount;
}
2.
public int sumArray(int arr[]) {
int sum = 0;
for(int i: arr) {
sum += i;
}
return sum;
}
3.
Picks only the negative integers from the original list and creates a new list and return it.
4. Returns only the strings having lenght greater than 10, contained in form of a list.
5.
public static ArrayList<String> concatenate(ArrayList<String> first, ArrayList<String> second) {
ArrayList<String> output = new ArrayList<>();
output.addAll(first);
output.addAll(second);
return output;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.