The goal of the program is to compute the position-wise maxima of two array list
ID: 3684591 • Letter: T
Question
The goal of the program is to compute the position-wise maxima of two array lists. Complete that program, by defining an arrayListMax function, that satisfies the following specs: Function arrayListMax takes two arguments, called A, B. They are both array lists of double numbers. If the two array lists do NOT have the same length, function arrayListMax should return null. Otherwise, the function should return an array list called result, with length equal to the length of A, such that the value at position i of result is the larger among the values at position i of A and B.
import java.util.ArrayList;
public class hw5_task2 { public static void main(String[] args){
ArrayList a = new ArrayList();
a.add(3.2);
a.add(2.1);
a.add(5.3);
a.add(8.0);
a.add(4.9);
a.add(5.7);
ArrayList b = new ArrayList();
b.add(1.1);
b.add(2.2);
b.add(3.3);
b.add(4.4);
b.add(5.5);
b.add(6.6);
ArrayList result = arrayListMax(a, b);
System.out.println("a: " + a);
System.out.println("b: " + b);
System.out.println("result: " + result); } }
The complete program should produce this output:
a: [3.2, 2.1, 5.3, 8.0, 4.9, 5.7]
b: [1.1, 2.2, 3.3, 4.4, 5.5, 6.6]
result: [3.2, 2.2, 5.3, 8.0, 5.5, 6.6]
Explanation / Answer
/***The java program hw5_task2 that prints the two array
* lists a and b. The program also prints the resultant
* array of max elements from the both arrays,a and b.*/
//hw5_task2.java
import java.util.ArrayList;
public class hw5_task2 {
public static void main(String[] args){
//Create an array list,a of double type
ArrayList<Double> a = new ArrayList<Double>();
a.add(3.2);
a.add(2.1);
a.add(5.3);
a.add(8.0);
a.add(4.9);
a.add(5.7);
//Create an array list,b of double type
ArrayList<Double> b = new ArrayList<Double>();
b.add(1.1);
b.add(2.2);
b.add(3.3);
b.add(4.4);
b.add(5.5);
b.add(6.6);
//Call the method arrayListMax that returns arraylist
ArrayList<Double> result = arrayListMax(a, b);
//print array list,a
System.out.println("a: " + a);
//print array list,b
System.out.println("b: " + b);
//print array list,result
System.out.println("result: " + result); }
/**The method arrayListMax that takes two ArrayList objects
* and returns the arraylist, result.
* if the size of two arraylist is not equal then returns
* null, otherwise returns the greater array list elements
* from the both the array lists.
* */
private static ArrayList<Double> arrayListMax(ArrayList<Double> a,
ArrayList<Double> b) {
//Create an array list of type ,double
ArrayList<Double> result = new ArrayList<Double>();
//return null if size of array lists not equal
if(a.size()!=b.size())
return null;
else
{
//run for loop from 0 to size of the arraylist,a
for (int i = 0; i < a.size(); i++) {
//check if element at i-th position of
//a is greater than the i-th position of
//b arraylist then add the element of a
//to result array list
if(a.get(i)>b.get(i))
result.add(a.get(i));
else
//otherwise add the element of arraylist,b
//to the result array list
result.add(b.get(i));
}
}
//returns result of type ArrayList.
return result;
}
}//end of the class
------------------------------------------------------------------------------------------------------------------------
Sample output:
a: [3.2, 2.1, 5.3, 8.0, 4.9, 5.7]
b: [1.1, 2.2, 3.3, 4.4, 5.5, 6.6]
result: [3.2, 2.2, 5.3, 8.0, 5.5, 6.6]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.