Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

String To List of Words a method called stringToListOfWords() which takes in a String converts it into a list of words. We assumes that each

String To List of Words

a method called stringToListOfWords() which takes in a String converts it into a list of words. We assumes that each word in the input string is separated by whitespace.

If our input String is "Hello, world!", then the output should be ["Hello,", "world!"]. For extra credit, sanitize the String, cleaning it up so that we remove the punctuation and other extraneous characters such that the output in the above example would become ["Hello", "world"] This method returns List.

Remove All Instances

a method called removeAllInstances() which takes in a List and item4 . The method then proceeds to remove each item in the list that matches the given item. For example, if the method is passed the List [1, 4, 5, 6, 5, 5, 2] and the Integer 5, the method removes all 5's from the List. The List then becomes [1, 4, 6, 2]. It should return nothing, since the changes the List it was provided.

Use static genetic methods if it need to be there.

image text in transcribed
1 Static Generic Methods Most of the methods we write in class won't be static, but that's no reason not to learn how to do that. Java makes writing generic methods look intimidating, but in reality, it's not so bad. I'll walk you through how to do it by showing what we want to do, hitting an error, and then showing you how to resolve the error. Suppose we want to write a method called in, which given a List and an item, checks to see if the List contains that item. 1 We don't know what type the item will, nor do we know what kind of stuff the List will be holding, as that will change from one program to another, and we want this method to be able to be used in any kind of context. That means we want it to be generic. However, if we write //this is an error public static boolean in(List list, E item){ } We get \"E cannot be resolved to a type\" as an error. This happens because Java doesn't know that you want to use E as the symbol for generics for this method. We can x this by adding a in between static and our return type, like so: public static (E) boolean in(List list, E item){ } The big difference here between a class that uses a generic, like ArrayList, and the static methods you write here is that the generic type only exists for the method

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

k = n; while( k > 0 ) { for( j = 1; j Answered: 1 week ago

Answered: 1 week ago