Lab 13, CSC 102, Spring 2012
For all of the methods in this lab, create your Lists like this:
List<String> l1 = Arrays.asList("Dog", "Horse", "Apple"); |
1 Map-like methods
Implement these "map"-like methods, following the template for map-like methods. All will be static.
/** |
* My Map-like function from List<ClassA> to ArrayList<ClassB> |
*/ |
public static ArrayList<ClassB> myMapLikeFunction(List<ClassA> in, ... extra args ...) { |
ArrayList<ClassB> result = new ArrayList<ClassB>(); |
for (ClassA elt : in) { |
result.add(... elt ...); |
} |
return result; |
} |
Develop the intToString method, that converts an List of Integers into an ArrayList of Strings. Use the Java API documentation (see lab 6) to figure out what Integer method will convert it to a string.
Develop the allToText method, that accepts an List of Displayeds and returns an ArrayList of Strings obtained by calling their convertToString methods.
2 Filter-like methods
Implement these "filter"-like methods, following the template for filter-like methods.
/** |
* My Filter-Like Function from List<ClassA> to ArrayList<ClassA> |
*/ |
public static ArrayList<ClassB> myFilterLikeFunction(List<ClassA> in, ... extra args ...) { |
ArrayList<ClassB> result = new ArrayList<ClassB>(); |
for (ClassA elt : in) { |
if (... elt ...) { |
result.add(elt); |
} |
} |
return result; |
} |
Develop the inRange method, that accepts an List of Integers and a lower and upper limit, and returns a new list containing those elements that are in the range (inclusive) specified by the lower and upper limit.
Develop the onlyImages method, that accepts an List of Displayeds and returns a new ArrayList containing only those elements of the original list that were Images. Use the instanceof operator to see whether an element is an Image.
3 Fold-like methods
Implement these "fold"-like methods, following the template for fold-like methods.
/** |
* My Fold-Like Function from List<ClassA> to ClassB |
*/ |
public static ClassB myFoldLikeFunction(List<ClassA> in, ... extra args ...) { |
ClassB result = ... initial result ...; |
for (ClassA elt : in) { |
result = ... result ... elt ...; |
} |
return result; |
} |
Develop the totalWidth method, that accepts an List of Displayeds and returns the sum of their widths.
Develop the longestString method, that accepts a non-empty List of Strings and returns the longest one.
]