Question

Need help coding this List. Lists are a lot like arrays, but you’ll be using get,...

Need help coding this List. Lists are a lot like arrays, but you’ll be using get, set, add, size and the like instead of the array index operators [].​

package list.exercises;
import java.util.List;
public class ListExercises {

   /**

   * Counts the number of characters in total across all strings in the supplied list;

   * in other words, the sum of the lengths of the all the strings.

   * @param l a non-null list of strings

   * @return the number of characters

   */

   public static int countCharacters(List<String> l) {

       return 0;
}

   /**

   * Splits a string into words and returns a list of the words.

   * If the string is empty, split returns a list containing an empty string.

   *

   * @param s a non-null string of zero or more words

   * @return a list of words

   */

   public static List<String> split(String s) {
return null;
}

   /**

   * Returns a copy of the list of strings where each string has been

   * uppercased (as by String.toUpperCase).

   *

   * The original list is unchanged.

   *

   * @param l a non-null list of strings

   * @return a list of uppercased strings

   */

   public static List<String> uppercased(List<String> l) {

       return null;

   }

   /**

   * Returns true if and only if each string in the supplied list of strings

   * starts with an uppercase letter. If the list is empty, returns false.

   *

   * @param l a non-null list of strings

   * @return true iff each string starts with an uppercase letter

   */

   public static boolean allCapitalizedWords(List<String> l) {

       return false;

   }

   /**

   * Returns a list of strings selected from a supplied list, which contain the character c.

   *

   * The returned list is in the same order as the original list, but it omits all strings

   * that do not contain c.

   *

   * The original list is unmodified.

   *

   * @param l a non-null list of strings

   * @param c the character to filter on

   * @return a list of strings containing the character c, selected from l

   */

   public static List<String> filterContaining(List<String> l, char c) {

       return null;

   }

  

   /**

   * Inserts a string into a sorted list of strings, maintaining the sorted property of the list.

   *

   * @param s the string to insert

   * @param l a non-null, sorted list of strings

   */

   public static void insertInOrder(String s, List<String> l) {

   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.ArrayList;
import java.util.List;


public class ListExercises {

    /**

     * Counts the number of characters in total across all strings in the supplied list;

     * in other words, the sum of the lengths of the all the strings.

     * @param l a non-null list of strings

     * @return the number of characters

     */

    public static int countCharacters(List<String> l) {
        return l.size();
    }

    /**

     * Splits a string into words and returns a list of the words.

     * If the string is empty, split returns a list containing an empty string.

     *

     * @param s a non-null string of zero or more words

     * @return a list of words

     */

    public static List<String> split(String s) {
        List<String> list = new ArrayList<>();
        String[] arr = s.split(" ");

        for ( String ss : arr) {
            list.add(ss);
        }
        return list;
    }

    /**

     * Returns a copy of the list of strings where each string has been

     * uppercased (as by String.toUpperCase).

     *

     * The original list is unchanged.

     *

     * @param l a non-null list of strings

     * @return a list of uppercased strings

     */

    public static List<String> uppercased(List<String> l) {

        List<String> list = new ArrayList<>();
        for (String str : l) {
            list.add(str.toUpperCase());
        }

        return list;

    }

    /**

     * Returns true if and only if each string in the supplied list of strings

     * starts with an uppercase letter. If the list is empty, returns false.

     *

     * @param l a non-null list of strings

     * @return true iff each string starts with an uppercase letter

     */

    public static boolean allCapitalizedWords(List<String> l) {
        boolean isAllCaps = true;
        for (String str : l) {
            for (int i=0; i<str.length(); i++)
            {
                if (Character.isLowerCase(str.charAt(i)))
                {
                    isAllCaps = false;
                    break;
                }
            }
        }

        return isAllCaps;

    }

    /**

     * Returns a list of strings selected from a supplied list, which contain the character c.

     *

     * The returned list is in the same order as the original list, but it omits all strings

     * that do not contain c.

     *

     * The original list is unmodified.

     *

     * @param l a non-null list of strings

     * @param c the character to filter on

     * @return a list of strings containing the character c, selected from l

     */

    public static List<String> filterContaining(List<String> l, char c) {
        List<String> list = new ArrayList<>();
        for (String str : l) {
            if (str.indexOf(c) != -1) {
                list.add(str);
            }
        }
        return list;

    }



    /**

     * Inserts a string into a sorted list of strings, maintaining the sorted property of the list.

     *

     * @param s the string to insert

     * @param l a non-null, sorted list of strings

     */

    public static void insertInOrder(String s, List<String> l) {
        l.add(s);
        java.util.Collections.sort(l);
    }

}
Add a comment
Know the answer?
Add Answer to:
Need help coding this List. Lists are a lot like arrays, but you’ll be using get,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  •    /**    * Returns true if and only if each string in the supplied list...

       /**    * Returns true if and only if each string in the supplied list of strings    * starts with an uppercase letter. If the list is empty, returns false.    *    * @param l a non-null list of strings    * @return true iff each string starts with an uppercase letter    */    public static boolean allCapitalizedWords(List<String> l) {       } Here are the tests: @Test    public void testAllCapitalizedWordsEmptyList() {        assertFalse(ListExercises.allCapitalizedWords(Arrays.asList()));...

  • Objectives Problem solving using arrays and ArrayLists. Abstraction. Overview The diagram below illustrates a banner constructed...

    Objectives Problem solving using arrays and ArrayLists. Abstraction. Overview The diagram below illustrates a banner constructed from block-letters of size 7. Each block-letter is composed of 7 horizontal line-segments of width 7 (spaces included): SOLID                        as in block-letters F, I, U, M, A, S and the blurb RThere are six distinct line-segment types: TRIPLE                      as in block-letter M DOUBLE                   as in block-letters U, M, A LEFT_DOT                as in block-letters F, S CENTER_DOT          as in block-letter...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Need Help Using Java programming only import java.util.ArrayList; //Import anything you need here public class Dictionary...

    Need Help Using Java programming only import java.util.ArrayList; //Import anything you need here public class Dictionary {     // Declare any variables you feel necessary here     public Dictionary(/*DO NOT PUT ANY PARAMETERS HERE!*/) {         // Initialise those variables     }     /***      * @return number of terms currently in the dictionary hint: starts at 0 when      *         there are no terms      */     public int numberOfTerms() {         return 0;     }     /***     ...

  • Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all...

    Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all others intact and follow similar guidelines. The methods that need to be changed are in the code below. - Rewrite the indexOf() method. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead. - Rewrite the isPrefix() method so that it uses iteration. Remove the existing recursive implementation of the method, and replace it with one that...

  • I need help with this. I need to create the hangman game using char arrays. I've...

    I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!! 1. /** * This class contains all of the logic of the hangman game. * Carefully review the comments to see where you must insert * code. * */ public class HangmanGame { private final Integer MAX_GUESSES = 8; private static HangmanLexicon lexicon = new HangmanLexicon();...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = '...

    This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = ' '; private static final char UPPER_BOUND = '_'; private static final int RANGE = UPPER_BOUND - LOWER_BOUND + 1; /** * This method determines if a string is within the allowable bounds of ASCII codes * according to the LOWER_BOUND and UPPER_BOUND characters * @param plainText a string to be encrypted, if it is within the allowable bounds * @return true if all characters...

  • Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray();...

    Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray(); char[] reverse = new char[original.length]; for(int i =0; i<s.length(); i++){ reverse[i] = original[original.length-1-i]; } return new String(reverse); } /**  * Takes in a string containing a first, middle and last name in that order.  * For example Amith Mamidi Reddy to A. M. Reddy.  * If there are not three words in the string then the method will return null  * @param name in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT