need help doing #5, what i have so far is from another program which im trying t
ID: 3700433 • Letter: N
Question
need help doing #5, what i have so far is from another program which im trying to change using Arraylist
5. Add a static method to the class Pa6_your-id that receives an ArrayList-Double> and nothing else, then outputs the sum of the elements in the ArrayList Double> with an even index. Note that zero is not even. The method returns nothing. The sum output is to be limited to 2 decimal places 6. Add a static method to the class Pa6_your-id that receives an ArrayList and nothing else, thern outputs the number of negative values stored in the ArrayListDouble> elements. The method returns nothing.Explanation / Answer
public static double sumEvenIndexElements(ArrayList<Double> arr)
{
int i;
double sum = 0.0;
// start from first even index 2
// increment i by 2 after each iteration
for( i = 2 ; i < arr.size() ; i += 2 )
sum += arr.get(i);
// get sum upto 2 decimal places using format() function
String temp = String.format("%.2f", sum);
// convert String to double
return Double.parseDouble( temp );
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.