
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
thumb_up100%
![Write a method that returns all strings from an array list of a given length, without changing the original array list. Complete the following code.
ArrayListUtil.java
1 import java.util.ArrayList;
2
3 public class ArrayListUtil
4 {
5
/**
Gets all strings from an array list of a given length.
@param words an array list of strings
@param wordLength the length of the words to get
@return an array list with the words of length wordLength
* /
public static . . . wordsOfLength(. . . words,
{
6
7
8
9
10
11
. wordLength)
12
13
}
15 }
14
Tester.java
1 import java.util.ArrayList;
3 public class Tester
4 {
public static void main(String[] args)
{
ArrayList<String> words = new ArrayList<String>();
words.add("Mary");
words.add("had");
words.add ("a");
words.add ("little");
words.add("lamb");
words.add("its");
words.add("fleece");
words.add("was");
words.add("white");
words.add("as");
words.add("snow");
5
7
8
9
10
11
12
13
14
15
16
17
18
19
System.out.println(ArrayListUtil.words0fLength(words, 3));
System.out.println("Expected: [had, its, was]");
System.out.println (ArrayListUtil.words0fLength(words, 6));
System.out.println("Expected: [little, fleece]");
System.out.println(ArrayListUtil.words0fLength(words, 7));
System.out.println("Expected: []");
// Check that words hasn't been changed
System.out.println(words);
System.out.println ("Expected: [Mary, had, a, little, lamb, its, fleece, was,"
20
21
22
23
24
25
26
27
28
29
+
white, as, snow]");
}
31 }
30](https://content.bartleby.com/qna-images/question/3200e892-7b83-4670-8aa5-c4c84f2a6adb/3711a1a8-23e9-479a-9e07-93c6c6e2adb9/luilpi_thumbnail.png)
Transcribed Image Text:Write a method that returns all strings from an array list of a given length, without changing the original array list. Complete the following code.
ArrayListUtil.java
1 import java.util.ArrayList;
2
3 public class ArrayListUtil
4 {
5
/**
Gets all strings from an array list of a given length.
@param words an array list of strings
@param wordLength the length of the words to get
@return an array list with the words of length wordLength
* /
public static . . . wordsOfLength(. . . words,
{
6
7
8
9
10
11
. wordLength)
12
13
}
15 }
14
Tester.java
1 import java.util.ArrayList;
3 public class Tester
4 {
public static void main(String[] args)
{
ArrayList<String> words = new ArrayList<String>();
words.add("Mary");
words.add("had");
words.add ("a");
words.add ("little");
words.add("lamb");
words.add("its");
words.add("fleece");
words.add("was");
words.add("white");
words.add("as");
words.add("snow");
5
7
8
9
10
11
12
13
14
15
16
17
18
19
System.out.println(ArrayListUtil.words0fLength(words, 3));
System.out.println("Expected: [had, its, was]");
System.out.println (ArrayListUtil.words0fLength(words, 6));
System.out.println("Expected: [little, fleece]");
System.out.println(ArrayListUtil.words0fLength(words, 7));
System.out.println("Expected: []");
// Check that words hasn't been changed
System.out.println(words);
System.out.println ("Expected: [Mary, had, a, little, lamb, its, fleece, was,"
20
21
22
23
24
25
26
27
28
29
+
white, as, snow]");
}
31 }
30
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 3 images

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Given an array of numbers, and an integer, find the last index that that integer appears in the array. If the number is not found, return -1. import java.util.ArrayList;public class LastIndexFound{public static int solution(ArrayList<Integer> nums, int numToFind){// ↓↓↓↓ your code goes here ↓↓↓↓return 0;}}arrow_forwardWrite the Java method averageOfDoubles that is passed an array of double values, and returns the average of the array values.You can assume the array length is a positive integer.arrow_forwardjava Create a static method that: is called repeatAll returns ArrayList of Booleans takes in a single parameter - an ArrayList of Booleans This method should modify its ArrayList parameter by repeating its ArrayList values. For example, if the parameter is (true, false, false) The modified ArrayList should be (true, false, false, true, false, false) public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); ArrayList<Boolean> list = new ArrayList<>(); for(int i=0; i < size; i++) { list.add(in.nextBoolean()); } System.out.println(repeatAll(list)); } }arrow_forward
- java Create a method that: is called timesTwo returns an ArrayList of Integers takes in a single parameter - an ArrayList of Integers called nums This method should take the ArrayList parameter and multiply every value by two. public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); ArrayList<Integer> list = new ArrayList<>(); for(int i=0; i < size; i++) { list.add(in.nextInt()); } System.out.println(timesTwo(list)); } }arrow_forwardMethod Details: public static java.lang.String getArrayString(int[] array, char separator) Return a string where each array entry (except the last one) is followed by the specified separator. An empty string will be return if the array has no elements. Parameters: array - separator - Returns: stringThrows:java.lang.IllegalArgumentException - When a null array parameter is providedarrow_forwarduse java to create a card game with the implements listed for the program. If you are able to do the first couple of steps that would be helpful and for step one use an array list with the following code. The code below should create 52 cards. String[] suits = {"Hearts", "Clubs", "Spades", "Diamonds"}; String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; for(String oneSuit : suits){ for(String num : numbers){ System.out.println(oneSuit + " " + num); } }arrow_forward
- java Create a static method that: is called appendPosSum returns an ArrayList of Integers takes one parameter: an ArrayList of Integers This method should: Create a new ArrayList of Integers Add only the positive Integers to the new ArrayList Sum the positive Integers in the new ArrayList and add the Sum as the last element For example, if the incoming ArrayList contains the Integers (4,-6,3,-8,0,4,3), the ArrayList that gets returned should be (4,3,4,3,14), with 14 being the sum of (4,3,4,3). The original ArrayList should remain unchanged. public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); ArrayList<Integer> list = new ArrayList<>(); for(int i=0; i < size; i++) { list.add(in.nextInt()); } System.out.println(appendPosSum(list));arrow_forwardpublic class arrayOutput ( public static void main (String [] args) { final int NUM ELEMENTS = 3; int[] userVals = new int [NUM_ELEMENTS]; int i; } Type the program's output userVals [0] = 2; userVals [1] = 6; userVals [2] = 8; for (i = userVals.length - 1; i >= 0; −−1) { System.out.println(userVals [1]); } C.C. ? ? ??arrow_forwardNeed help with menu loop pleasearrow_forward
- Write a Java method (do not write a class, only the method) with the following signature: Return type: void Parameters: 2D integer array Name: printDiagonal Implement the method code that will print out the values of the 2D array along the Diagonal from left to right. For example, if the array had the following values: 56 78 98 21 33 55 98 44 11 The method should print to the console: 56,33,11 The above 2D array example is only given for you to understand the problem Do not hard code the values in your solution or assume the array is the same as the given data. Your method should work for all 2D integer arrays.arrow_forwardJavaTimer.java: import java.util.Arrays;import java.util.Random; public class JavaTimer { // Please expand method main() to meet the requirements.// You have the following sorting methods available:// insertionSort(int[] a);// selectionSort(int[] a);// mergeSort(int[] a);// quickSort(int[] a);// The array will be in sorted order after the routines are called!// Be sure to re-randomize the array after each sort.public static void main(String[] args) {// Create and initialize arraysint[] a = {1, 3, 5}, b, c, d;// Check the time to sort array along startTime = System.nanoTime();quickSort(a);long endTime = System.nanoTime();long duration = (endTime - startTime) / 1000l;// Output resultsSystem.out.println("Working on an array of length " + a.length + ".");System.out.println("Quick sort: " + duration + "us.");}// Thanks to https://www.javatpoint.com/insertion-sort-in-javapublic static void insertionSort(int array[]) {int n = array.length;for (int j = 1; j < n; j++) {int key = array[j];int…arrow_forwardWrite a static method that displays all combinations of picking two numbers from the array list: public static void combinations(ArrayList<Integer> list) Include the driver program that prompts the user to enter 10 integers and displays all combinations of picking two numbers from the array list.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education