Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

public static ArrayList wordsEndingIn(ArrayList 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

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 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 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 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 wordsEndingIn(ArrayList list, String letter) { ArrayList endsWith = new ArrayList(); 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

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Issues In Database Management A Refernce For The Thinking Practitioner

Authors: Fabian Pascal

1st Edition

0201485559, 978-0201485554

More Books

Students also viewed these Databases questions

Question

How many multiples of 4 are there between 10 and 250?

Answered: 1 week ago

Question

How many three-digit numbers are divisible by 7?

Answered: 1 week ago