Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 12, Problem 6E
Write a static method
getCommonStrings(ArrayList<String> list 1,
ArrayList <String> list2)
that returns a new instance of ArrayList containing all of the strings common to both list1 and list2.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
java
Create a static method that:
is called removeAll
returns ArrayList<String>
takes two parameters: an ArrayList of Strings called wordList, and a String called targetWord
This method should go through every element of wordList and remove every instance of targetWord from the ArrayList.
Example:
ArrayList<String> wordList = new ArrayList<String>(Arrays.asList("hi","hey","hi","yo"));
removeAll(wordList,"hi");
wordList: ["hey","yo"]
public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); String target = in.next(); ArrayList<String> list = new ArrayList<>(); for(int i=0; i < size; i++) { list.add(in.next()); }
System.out.println(removeAll(list, target));
}
}
Write a method max() which take a generic singly linked list as an argument and returns the maximum element reference in the
list.
If the list is empty, please throw an empty collection exception.
The method prototype is defined as follows.
public static > T max(SingleLinkedList list) throws EmptyCollectionException
You can use any of public methods in these following three classes:
1. The SingleLinkedList class diagram is as follows:
SingleLinkedNode
1-SingleLinkedNodenext
-T element
O• SingleLinkedNodeg
9+SinglelinkedNode(T elem)
+SingleLinkedlNode first
SingleLinkedNode last
int numNodes
+SingleLinkedList)
+void addFirst(T element)
public static ArrayList findExactString(String s, ArrayList myArray)
{
return null;
}
// Use the above method, findExactString, as a helper method to implement
addWithoutDuplication method.
The addWithoutDuplication method has the following properties:
1- It takes two arguments, a string s and an ArrayList myArray of type string.
2- It returns an integer which represents the number of occurrences of the string s in
myArray while attempting to add it.
3- If myArray is null, it returns -1
4- If adds the string s to the end of the ArrayList myArray if the string s is not
already there.
public static int addwithout Duplication(String s, ArrayList myArray)
{
return -1;
Chapter 12 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
Ch. 12.1 - Suppose aList is an object of the class...Ch. 12.1 - Prob. 2STQCh. 12.1 - Prob. 3STQCh. 12.1 - Prob. 4STQCh. 12.1 - Can you use the method add to insert an element at...Ch. 12.1 - Prob. 6STQCh. 12.1 - Prob. 7STQCh. 12.1 - If you create a list using the statement...Ch. 12.1 - Prob. 9STQCh. 12.1 - Prob. 11STQ
Ch. 12.1 - Prob. 12STQCh. 12.2 - Prob. 13STQCh. 12.2 - Prob. 14STQCh. 12.2 - Prob. 15STQCh. 12.2 - Prob. 16STQCh. 12.3 - Prob. 17STQCh. 12.3 - Prob. 18STQCh. 12.3 - Prob. 19STQCh. 12.3 - Write a definition of a method isEmpty for the...Ch. 12.3 - Prob. 21STQCh. 12.3 - Prob. 22STQCh. 12.3 - Prob. 23STQCh. 12.3 - Prob. 24STQCh. 12.3 - Redefine the method getDataAtCurrent in...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.4 - Revise the definition of the class ListNode in...Ch. 12.4 - Prob. 30STQCh. 12 - Repeat Exercise 2 in Chapter 7, but use an...Ch. 12 - Prob. 2ECh. 12 - Prob. 3ECh. 12 - Repeat Exercises 6 and 7 in Chapter 7, but use an...Ch. 12 - Write a static method removeDuplicates...Ch. 12 - Write a static method...Ch. 12 - Write a program that will read sentences from a...Ch. 12 - Repeat Exercise 12 in Chapter 7, but use an...Ch. 12 - Write a program that will read a text file that...Ch. 12 - Revise the class StringLinkedList in Listing 12.5...Ch. 12 - Prob. 12ECh. 12 - Write some code that will use an iterator to...Ch. 12 - Prob. 14ECh. 12 - Write some code that will use an iterator to...Ch. 12 - Prob. 17ECh. 12 - Revise the method selectionSort within the class...Ch. 12 - Repeat the previous practice program, but instead...Ch. 12 - Repeat Practice Program 1, but instead write a...Ch. 12 - Write a program that allows the user to enter an...Ch. 12 - Write a program that uses a HashMap to compute a...Ch. 12 - Write a program that creates Pet objects from data...Ch. 12 - Repeat the previous programming project, but sort...Ch. 12 - Repeat the previous programming project, but read...Ch. 12 - Prob. 9PPCh. 12 - Prob. 10PPCh. 12 - Prob. 11PPCh. 12 - Prob. 12PPCh. 12 - Prob. 13PPCh. 12 - Prob. 14PPCh. 12 - Prob. 15PP
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
What would be the hexadecimal representation of the largest memory address in a memory consisting of 4MB if eac...
Computer Science: An Overview (12th Edition)
In Exercises 55 through 60, find the value of the given function where a and b are numeric variables of type Do...
Introduction to Programming Using Visual Basic (10th Edition)
What are the six common steps needed to access databases from a typical program?
Modern Database Management (12th Edition)
How does the typing system of PHP and JavaScript differ from that of Java?
Concepts Of Programming Languages
(Simpletron with File Processing) In Exercise 7.28, you wrote a software simulation of a computer that used a s...
C How to Program (8th Edition)
Mass and Weight Scientists measure an objects mass in kilograms and its weight in Newtons. If you know the amou...
Starting Out with Programming Logic and Design (4th Edition)
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
- Write a method called compareSum to be considered in a class outside the KWArrayList class. This method has a parameter list of class type KWArrayList . The method finds the summation of even elements for the first half of list and the summation of odd elements for the second half of list, then compares between the two summations and returns the greater sum. Assume the length of List is even. Method heading: public static int compareSum( KWArrayList<Integer> list) Before run: list= 10 5 50 3 14 33 20 5 (note: the numbers in bold represent the first half) After run: list= 3 5 50 14 33 20 5 Sum1 (for the first half) = 60 Sum2 (for the second half) = 38 Returned value is 60arrow_forwardImport the ArrayList and List classes from the java.util package to create a list of phone numbers and also import the HashSet and Set classes from the java.util package to create a set of unique prefixes. Create a class called PhoneNumberPrefix with a main method that will contain the code to find the unique prefixes. Create a List called phoneNumbers and use the add method to add several phone numbers to the list. List<String> phoneNumbers = new ArrayList<>(); phoneNumbers.add("555-555-1234"); phoneNumbers.add("555-555-2345"); phoneNumbers.add("555-555-3456"); phoneNumbers.add("444-444-1234"); phoneNumbers.add("333-333-1234"); Create a Set called prefixes and use a for-each loop to iterate over the phoneNumbers list. For each phone number, we use the substring method to extract the first 7 characters, which represent the prefix, and add it to the prefixes set using the add method. Finally, use the println method to print the prefixes set, which will contain all of…arrow_forwardJavaarrow_forward
- Java programming Do the same question with List Array Write a method with the name "eraser" that will get two arguments: “maparr_plants" (Map array of objects of a kind of class Plant), and "eraselist" (One dimensional array of integers). That method should erase the elements of the maparr_plants which their keys are matching the elements of the eraselist. (Hint: Check all elements of eraselist, and erase the matching (key) elements of the maparr_plants.) Solution: public static void eraser(Map maparr_plants, int [] eraselist) { for (i=0; iarrow_forwardWrite a method called addSmallest to be considered in a class outside the KWArrayList class. This method will have a parameter list of class type KWArrayList. The method will add the smallest element of the list, at the end of the list. Method heading: public static void addSmallest (KWArrayList list) Before run: 3 5 100 40 67 22 After run: 3 5 100 40 67 22 3arrow_forwardThe class PetShop has a single field as defined below: private ArrayList<Pet> petList; Define a method to print out the details of all cats in petList in PetShop.arrow_forwardProject Description: In this project you implement an ArrayStack ADT and use the stack for implementing the following methods: a) Reverse an array of Words: Accept an array of words as input parameter and return an array of words in reverse order. Use the method signature: public static String[] reverse Words (String[] wordList) Example Input: Bird Cat Dog Elephant Output: Elephant Dog Cat Birdarrow_forwardWrite a method called addBiggest to be considered in a class outside the KWArrayList class. This method will have a parameter list of class type KWArrayList. The method will add the biggest element of the list, at the end of the 1ist. Method heading: public static void addBiggest (KWArrayList list). Before run: 35 100 40 67 22 After run: 3 5 100 40 67 22 100arrow_forwardExercise 3: Provide a static method that checks whether a generic array list is a palindrome; that is, whether the values at index i and n- 1- i are equal to each other, where n is the size of the array list. Write a tester class to check your method.arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_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
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License