Question
[Java] Not sure what I am doing wrong. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that
[Java] Not sure what I am doing wrong.
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 StuffAndThings. Notice how the methods are invoked in StuffAndThingsTester.
StuffAndThings is a utility class. It has only static methods. Do not give it a constructor or instance variables
Implement these methods:
public static int min(int[] list) Gets the minimum value in the array. You can assume the array has at least one element
public static int min(ArrayList
public static ArrayList
public static String[] wordsEndingIn(String[] list, String letter) Gets an array of all the words in the given array that contain the given letter. The comparison is case-insensitive. 'A' and 'a' are counted as the same letter. You will need to use Arrays.copyOf() to return an array that contains just the words ending in the letter
Use the following file:
StuffAndThingsTester.java
import java.util.ArrayList; import java.util.Arrays; /** * Tests the static methods in StuffAndThings */ public class StuffAndThingsTester { public static void main(String[] args) { //arraylist min ArrayListlist = new ArrayList<>(); list.add(5); list.add(9); list.add(8); list.add(6); list.add(7); System.out.println("Min ArrayList: "+ StuffAndThings.min(list)); System.out.println("Expected: 5"); list.clear(); list.add(-5); list.add(-9); list.add(-8); list.add(-6); list.add(-7); list.add(-10); System.out.println("Min ArrayList: "+ StuffAndThings.min(list)); System.out.println("Expected: -10"); //ArrayList ends in ArrayList words = new ArrayList<>(); System.out.println("Empty Arrayist: " + StuffAndThings.wordsEndingIn(words, "e")); System.out.println("Expected: []"); words.add("The"); words.add("joy"); words.add("of"); words.add("code"); words.add("Java"); words.add("You"); words.add("gotta"); words.add("love"); words.add("it"); System.out.println("ends in e: " + StuffAndThings.wordsEndingIn(words, "e")); System.out.println("Expected: [The, code, love]"); System.out.println("ends in a: " + StuffAndThings.wordsEndingIn(words, "a")); System.out.println("Expected: [Java, gotta]"); System.out.println("ends in x: " + StuffAndThings.wordsEndingIn(words, "x")); System.out.println("Expected: []"); //array ends in String[] empty = new String[0]; String[] answer = StuffAndThings.wordsEndingIn(empty, "e"); System.out.println("Empty array: " + Arrays.toString(answer)); System.out.println("Expected: []"); String[] words2 = {"The", "joy", "of", "code", "Java", "You", "gotta", "love", "it"}; answer = StuffAndThings.wordsEndingIn(words2, "e"); System.out.println("ends in e: " + Arrays.toString(answer)); System.out.println("Expected: [The, code, love]"); answer = StuffAndThings.wordsEndingIn(words2, "a"); System.out.println("ends in a: " + Arrays.toString(answer)); System.out.println("Expected: [Java, gotta]"); answer = StuffAndThings.wordsEndingIn(words2, "x"); System.out.println("ends in x: " + Arrays.toString(answer)); System.out.println("Expected: []"); } }
[Here's my code]
import java.util.ArrayList; import java.util.Arrays;
/** * Static methods working with arrays and arraylists * * */ public class StuffAndThings { /** * Get the minimum number in the array * @param list an array to work with * @return min minimum number in the array */ public static int min(int[] list) { int min = list[0]; for(int i = 0; i < list.length; i++) { if(min > list[i]) { min = list[i]; } } return min; } public static int min(ArrayList
[How's the out put looks like]
pass pass pass pass pass pass pass fail fail fail
Min ArrayList: 5 Expected: 5 Min ArrayList: -10 Expected: -10 Empty Arrayist: [] Expected: [] ends in e: [The, code, love] Expected: [The, code, love] ends in a: [Java, gotta] Expected: [Java, gotta] ends in x: [] Expected: [] Empty array: [] Expected: [] ends in e: [The, The, of, code, code, You, gotta, love, love] Expected: [The, code, love] ends in a: [The, joy, of, code, Java, Java, gotta, gotta, it] Expected: [Java, gotta] ends in x: [The, joy, of, code, Java, You, gotta, love, it] Expected: []
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started