In this problem you will write several static methods to work with arrays and Ar
ID: 3802834 • Letter: I
Question
In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester.
public static int min(int[] array) gets the minimum value in the array
public static boolean contains(String[] array, String letter)determines if the array contains a word that starts with the given letter. Returns true if it does, otherwise returns false. The comparison is case-insensitive. 'A' and 'a' are counted as the same letter
public static ArrayList<String> contains(ArrayList<String> list, char letter)gets an ArrayList of all the strings in the given ArrayList that contain with the given letter. The comparison is case-insensitive. 'A' and 'a' are counted as the same letter
The last two methods are an example of overloading.
Provide Javadoc. Look at the documentation produced for Util. (Click Project / Generate Javadoc) The Javadoc utility works on static methods, too.
Use the following file:
UtilTester.java
Explanation / Answer
UtilTester.java
import java.util.ArrayList;
public class UtilTester
{
public static void main(String[] args)
{
//testing min method
int[] numbers = {5, 8, 4, 6, 2, 1, 7, 3};
System.out.println(Util.min(numbers));
System.out.println("Expected: 1");
int[] numbers2 = {2, 9, 4, 6, 5, 8, 7, 3};
System.out.println(Util.min(numbers2));
System.out.println("Expected: 2");
int[] numbers3 = {10, 9, 4, 6, 5, 8, 7, 3};
System.out.println(Util.min(numbers3));
System.out.println("Expected: 3");
//testing first contains
String[] javaIdentifiers = {"Integer", "Double", "Float", "Char",
"boolean", "long", "short", "byte"};
System.out.println(Util.contains(javaIdentifiers,"c"));
System.out.println("Expected: true");
System.out.println(Util.contains(javaIdentifiers, "x"));
System.out.println("Expected: false");
System.out.println(Util.contains(javaIdentifiers,"B"));
System.out.println("Expected: true");
//testing second contains
ArrayList<String> words = new ArrayList<String>();
System.out.println(Util.contains(words, 'b'));
System.out.println("Expected: []");
words.add("Big");
words.add("Java");
words.add("is");
words.add("best");
words.add("Be");
words.add("the");
words.add("computer");
words.add("CS46A/B");
System.out.println(Util.contains(words, 'e'));
System.out.println("Expected: [best, Be, the, computer]");
System.out.println(Util.contains(words, 'B'));
System.out.println("Expected: [Big, best, Be, CS46A/B]");
System.out.println(Util.contains(words, 'a'));
System.out.println("Expected: [Java, CS46A/B]");
System.out.println(Util.contains(words, 'k'));
System.out.println("Expected: []");
}
}
Util.java
import java.util.ArrayList;
public class Util {
public static int min(int[] numbers){
int minValue = numbers[0];
for(int i=0; i<numbers.length; i++){
if(minValue > numbers[i]){
minValue=numbers[i];
}
}
return minValue;
}
public static boolean contains(String javaIdentifiers[], String s){
for(int i=0; i<javaIdentifiers.length; i++){
if(javaIdentifiers[i].toLowerCase().contains(s.toLowerCase())){
return true;
}
}
return false;
}
public static boolean contains(ArrayList<String> words, char ch){
for(int i=0; i<words.size(); i++){
if(words.get(i).toLowerCase().contains(ch+"".toLowerCase())){
return true;
}
}
return false;
}
}
Output:
1
Expected: 1
2
Expected: 2
3
Expected: 3
true
Expected: true
false
Expected: false
true
Expected: true
false
Expected: []
true
Expected: [best, Be, the, computer]
true
Expected: [Big, best, Be, CS46A/B]
true
Expected: [Java, CS46A/B]
false
Expected: []
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.