File hw5_task2.java contains an incomplete program. The goal of the program is t
ID: 3682870 • Letter: F
Question
File hw5_task2.java contains an incomplete program. 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.
IMPORTANT: You are NOT allowed to modify in any way the main function.
The complete program should produce this output:
Explanation / Answer
import java.util.ArrayList;
public class hw5_task2
{
public static void main(String[] args)
{
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);
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);
ArrayLIst arrayListMax(Double ArrayList a1, Double ArrayList b1)
{
ArrayList<Double> a1 = new ArrayList<Double>();
ArrayList<Double> b1= new ArrayList<Double>();
ArrayList<Double> c1= new ArrayList<Double>();
If(a1.size!=b1.size)
return null;
else
{
for(int counter = 0; counter < a1.size(); counter++)
{
if(a1.get(counter)< b1.get(counter))
{
C1.add(a1.get(counter));
}
else
C1.add(b1.get(counter));
}
return c1;
} //else
}//method
ArrayList<Double> result = arrayListMax(a, b);
System.out.println("a: " + a);
System.out.println("b: " + b);
System.out.println("result: " + result);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.