Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a public static method called smallerOfTwo that takes two ArrayList as par

ID: 3830049 • Letter: W

Question

Write a public static method called smallerOfTwo that takes two ArrayList as parameters, and returns another ArrayList. The returned ArrayList should have the same size as the two array lists that are passed in. The elements of the returned ArrayList should be the smaller of 2 elements from the original ArrayList. For example, if the values {1, 8, 4} and {4, 5, 6} are passed in, smallerOfTwo should return the list {1, 5, 4}. You may ignore the case where the original ArrayLists are not the same size.

Explanation / Answer

SmallerTwoTest.java

import java.util.ArrayList;


public class SmallerTwoTest {

  
   public static void main(String[] args) {
       ArrayList<Double> list1 = new ArrayList<Double>();
       ArrayList<Double> list2 = new ArrayList<Double>();
       list1.add(1.0);
       list1.add(8.0);
       list1.add(4.0);
       list2.add(4.0);
       list2.add(5.0);
       list2.add(6.0);
       System.out.println("List1: "+list1);
       System.out.println("List2: "+list2);
       System.out.println("Smaller List: "+smallerOfTwo(list1, list2));
   }
   public static ArrayList<Double> smallerOfTwo(ArrayList<Double> list1, ArrayList<Double> list2) {
       ArrayList<Double> newList = new ArrayList<Double>();
       for(int i=0; i<list1.size(); i++){
           if(list1.get(i) < list2.get(i)) {
               newList.add(list1.get(i));
           }
           else{
               newList.add(list2.get(i));
           }
       }
       return newList;
   }
}

Output:

List1: [1.0, 8.0, 4.0]
List2: [4.0, 5.0, 6.0]
Smaller List: [1.0, 5.0, 4.0]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote