[Java] I'm not sure how to implement the code. Please check my code at the bottom.
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
Use the following file:
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: []"); } }
[Here's my code]
import java.lang.reflect.Array;
import java.util.ArrayList;
public class Util
{
public static int min(int[] array)
{
int min = array[0];
for(int i = 0; i < array.length;
i++)
{
if(min >
array[i])
{
min = array[i];
}
}
return min;
}
public static boolean contains(String[] array, String
letter)
{
boolean returnValue = false;
for(int i = 0; i < array.length;
i++)
{
if(array[i].contains(letter.toLowerCase()))
{
returnValue = true;
}
else
{
returnValue = false;
}
}
return returnValue;
}
public static ArrayList<String>
contains(ArrayList<String> list, char letter)
{
for(int i = 0; i < list.size();
i++)
{
if(list.get(i).toString().substring(i, i + 1).equals(letter))
{
return ;
}
}
}
}
package as3;
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: []");
}
}
class Util
{
public static int min(int[] array)
{
int min = array[0];
for(int i = 0; i < array.length; i++)
{
if(min > array[i])
{
min = array[i];
}
}
return min;
}
public static boolean contains(String[] array, String letter)
{
boolean returnValue = false;
for(int i = 0; i < array.length; i++)
{
if(array[i].contains(letter.toLowerCase()))
{
returnValue = true;
}
else
{
returnValue = false;
}
}
return returnValue;
}
public static ArrayList<String>
contains(ArrayList<String> list, char letter)
{
ArrayList<String> words = new
ArrayList<String>();
for(int i = 0; i < list.size(); i++)
{
// this is where you have made mistake you just return but you need
to return list so i have defined one arraylist when in the list
match withh the letter i will put that word into the new list and
just return list
if(list.get(i).toString().contains(Character.toString(letter)))
words.add(list.get(i));
}
return words;
}
}
Get Answers For Free
Most questions answered within 1 hours.