Question

   /**    * 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()));
   }

   @Test
   public void testAllCapitalizedWordsEmptyString() {
       assertFalse(ListExercises.allCapitalizedWords(Arrays.asList("")));
   }

   @Test
   public void testAllCapitalizedWordsThreeStringsFalse1() {
       assertFalse(ListExercises.allCapitalizedWords(Arrays.asList("Asdf", "Jkl;", "qwer")));
   }

   @Test
   public void testAllCapitalizedWordsThreeStringsFalse2() {
       assertFalse(ListExercises.allCapitalizedWords(Arrays.asList("Asdf", "Jkl;", "!@#$")));
   }

   @Test
   public void testAllCapitalizedWordsThreeStringsFalse3() {
       assertFalse(ListExercises.allCapitalizedWords(Arrays.asList("Asdf", "", "Jkl;")));
   }

   @Test
   public void testAllCapitalizedWordsOneStringTrue() {
       assertTrue(ListExercises.allCapitalizedWords(Arrays.asList("Asdf")));
   }

   @Test
   public void testAllCapitalizedWordsThreeStringsTrue() {
       assertTrue(ListExercises.allCapitalizedWords(Arrays.asList("Asdf", "Jkl;", "Qwer")));
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code(Text Format):

import java.io.*;
import java.util.*;
import java.lang.Object;
import org.junit.Test;
import static org.junit.Assert.*;
class ListExercises {
/*allCapitalizedWords(List<String>)
1: If the list is empty, then return false
2: Now iterate over each string in list
   2.a: If the string is a empty string(i.e. ""), then return false
2.b: If the character at 0th index is not uppercase, then return false
2.c: If all string got checked and false is not returned, then return true at the end
*/
public static boolean allCapitalizedWords(List<String> l) {
if(l.size() == 0)
return false;
//declaring a iterator for the list through which the list can be iterated
ListIterator<String> itr = l.listIterator();
while(itr.hasNext()){
String str = itr.next();
if(str.length() == 0){
return false;
}
if(str.charAt(0) < 'A' || str.charAt(0) > 'Z'){
return false;
}
}
return true;
}
  
@Test
public void testAllCapitalizedWordsEmptyList() {
assertFalse(ListExercises.allCapitalizedWords(Arrays.asList()));
}

@Test
public void testAllCapitalizedWordsEmptyString() {
assertFalse(ListExercises.allCapitalizedWords(Arrays.asList("")));
}

@Test
public void testAllCapitalizedWordsThreeStringsFalse1() {
assertFalse(ListExercises.allCapitalizedWords(Arrays.asList("Asdf", "Jkl;", "qwer")));
}

@Test
public void testAllCapitalizedWordsThreeStringsFalse2() {
assertFalse(ListExercises.allCapitalizedWords(Arrays.asList("Asdf", "Jkl;", "!@#$")));
}

@Test
public void testAllCapitalizedWordsThreeStringsFalse3() {
assertFalse(ListExercises.allCapitalizedWords(Arrays.asList("Asdf", "", "Jkl;")));
}

@Test
public void testAllCapitalizedWordsOneStringTrue() {
assertTrue(ListExercises.allCapitalizedWords(Arrays.asList("Asdf")));
}

@Test
public void testAllCapitalizedWordsThreeStringsTrue() {
assertTrue(ListExercises.allCapitalizedWords(Arrays.asList("Asdf", "Jkl;", "Qwer")));
}
public static void main(String[] args) {
ListExercises obj = new ListExercises();
obj.testAllCapitalizedWordsEmptyList();
obj.testAllCapitalizedWordsEmptyString();
obj.testAllCapitalizedWordsThreeStringsFalse1();
obj.testAllCapitalizedWordsThreeStringsFalse2();
obj.testAllCapitalizedWordsThreeStringsFalse3();
obj.testAllCapitalizedWordsOneStringTrue();
obj.testAllCapitalizedWordsThreeStringsTrue();

}
}

Output (on provided test cases):

Completed with exit code: 0
Add a comment
Know the answer?
Add Answer to:
   /**    * Returns true if and only if each string in the supplied list...
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
  • 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   ...

  • In Java, you'll find that it contains several methods that have not yet been finished. For now, the methods contain...

    In Java, you'll find that it contains several methods that have not yet been finished. For now, the methods contain only a placeholder return value so that the code will compile. Fill in your own implementation. public class SomePracticeStringMethods { /* * returns true if c is a letter in the word "temple" or false otherwise */ public static boolean inTU(char c) { /* placeholder just so that the function * compiles. fill in your implementation here. * * there...

  • Need some assistance coding this assembler exercise. I have included the test file. Thank you very...

    Need some assistance coding this assembler exercise. I have included the test file. Thank you very much. package sequencer; import java.util.List; public class Assembler {    /**    * Creates a new Assembler containing a list of fragments.    *    * The list is copied into this assembler so that the original list will not    * be modified by the actions of this assembler.    *    * @param fragments    */    public Assembler(List<Fragment> fragments) {   ...

  • JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {    ...

    JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {     /**      * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList      * containing all the indexes of Strings in arr that match String s. For this method, two Strings,      * p and q, match when p.equals(q) returns true or if both of the compared references are null.      *      * @param s The string...

  • Please help complete the items marked TODO in the code and get the tests to pass:...

    Please help complete the items marked TODO in the code and get the tests to pass: import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestIterator { private List<Integer> list; // See the Java List Interface documentation to understand what all the List methods do ... @Before public void setUp() throws Exception { list = new ArrayList<Integer>(); // TODO also try with a...

  • Need help with a number guessing game in java 1) You should store prior guessses in...

    Need help with a number guessing game in java 1) You should store prior guessses in a linked list, and the nodes in the list must follow the order of prior guesses. For example, if the prior guesses are 1000, 2111, 3222 in that order, the nodes must follow the same order 2) You should store the candidate numbers also in a linked list, and the nodes must follow the order of numbers (i.e. from the smallest to the largest)....

  • Implement a function that returns true if a String is Palindrome or false otherwise. A Palindrome...

    Implement a function that returns true if a String is Palindrome or false otherwise. A Palindrome is a String that reads from right and left the same. An empty String, and a String with one character are Palindrome. public static boolean isPalindrome (String str) {

  • 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...

  • JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth    *    * Returns the number of nodes with...

    JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth    *    * Returns the number of nodes with depth == d    * Precondition: none    *    * param: d the depth to search for    *    * hint: use a recursive helper function    *        * ToDo 4    */    public int numNodesAtDepth(int d) {        return -1;    } **********USEFUL CODE FROM THE ASSIGNMENT:*********** public class simpleBST<Key extends Comparable<Key>, Value> {    private Node root;            ...

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