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

[Java] Not sure what I am doing wrong. In this problem you will write several st

ID: 3823443 • Letter: #

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 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<Integer> list) Gets the minimum value in the ArrayList as an int. You can assume the ArrayList contains at least one element

public static ArrayList<String> wordsEndingIn(ArrayList<String> list, String letter) Gets an ArrayList of all the words in the given ArrayList that contain the given letter. The comparison is case-insensitive. 'A' and 'a' are counted as the same letter

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

[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<Integer> list)
{
int min = list.get(0);
for(int i = 0; i < list.size(); i++)
{
   if(min > list.get(i))
   {
       min = list.get(i);
   }
}
return min;
}
public static ArrayList<String> wordsEndingIn(ArrayList<String> list, String letter)
{
   ArrayList<String> endsWith = new ArrayList<String>();
   for(int i = 0; i < list.size(); i++)
   {
       String check = list.get(i).toLowerCase();
       if(check.contains(letter.toLowerCase()))
       {
           endsWith.add(list.get(i));
       }
   }
   return endsWith;
}
public static String[] wordsEndingIn(String[] list, String letter)
{
   String[] copy = Arrays.copyOf(list, list.length);
   int currentIndex = 0;
   for(int i = 0; i < list.length; i++)
   {
       currentIndex++;
       String check = list[i].toLowerCase();
       if(check.contains(letter.toLowerCase()))
       {
           copy[currentIndex] = list[i];
       }
   }
   return copy;
     
}
}

[How's the out put looks like]

pass pass pass pass pass pass pass fail fail fail

Explanation / Answer

Hi

Please find below program, This program handles your all test cases. In your logics some changes, these changes handles your expected your output.

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
ArrayList<Integer> list = 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<String> 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: []");
  
//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: []
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: []");
}
}

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 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<Integer> list)
{
int min = list.get(0);
for(int i = 0; i < list.size(); i++)
{
if(min > list.get(i))
{
min = list.get(i);
}
}
return min;
}
public static ArrayList<String> wordsEndingIn(ArrayList<String> list, String letter)
{
ArrayList<String> endsWith = new ArrayList<String>();
for(int i = 0; i < list.size(); i++)
{
String check = list.get(i).toLowerCase();
if(check.contains(letter.toLowerCase()))
{
endsWith.add(list.get(i));
}
}
return endsWith;
}
public static String[] wordsEndingIn(String[] list, String letter)
{
  
List<String> resultList = new ArrayList<String>();
for(int i = 0; i < list.length; i++)
{
String check = list[i].toLowerCase();
int length = check.length();
if(length > 0){
   length = length - 1;
}
int lastCharacterValue = check.lastIndexOf(letter.toLowerCase());
if(lastCharacterValue != -1 && length == lastCharacterValue)
{
   resultList.add(list[i]);
}
}
String copy[] = resultList.toArray(new String[resultList.size()]);
return copy;

}
}

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