Created
March 21, 2016 04:59
-
-
Save bhnascar/6b627290cd0011692d9c to your computer and use it in GitHub Desktop.
ArrayList practice questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class Practice | |
{ | |
public static void main(String[] args) { | |
// An example list of words to test against. | |
ArrayList<String> words = new ArrayList<String>(10); | |
words.add("foo"); | |
words.add("bar"); | |
words.add("baz"); | |
words.add("slinky"); | |
words.add("dinky"); | |
words.add("blinky"); | |
words.add("bleh"); | |
words.add("meh"); | |
words.add("heh"); | |
// Test solutions to questions below. | |
System.out.println("Sup? Good luck on your exam. Hope these practice questions help. :)"); | |
System.out.println("Given the list of words: foo bar baz slinky dinky blinky bleh meh heh"); | |
// Test removeEveryOther. | |
ArrayList<String> result = new ArrayList<String>(words); | |
removeEveryOther(result); | |
System.out.println( | |
"\nRemoving every other word should give you: [foo, baz, dinky, bleh, heh]\n" | |
+ "Your answer is: " + result | |
); | |
// Test removeEh. | |
result = new ArrayList<String>(words); | |
removeEh(result); | |
System.out.println( | |
"\nRemoving words that end with \"eh\" give you: [foo, bar, baz, slinky, dinky, blinky]\n" | |
+ "Your answer is: " + result | |
); | |
// Test removeWordsWithSuffix. | |
result = new ArrayList<String>(words); | |
removeWordsWithSuffix(result, "linky"); | |
System.out.println( | |
"\nRemoving words that end with \"linky\" give you: [foo, bar, baz, dinky, bleh, meh, heh]\n" | |
+ "Your answer is: " + result | |
); | |
// Test removeInky. | |
result = new ArrayList<String>(words); | |
removeInky(result); | |
System.out.println( | |
"\nRemoving words that end with \"inky\" should give you: [foo, bar, baz, bleh, meh, heh]\n" | |
+ "Your answer is: " + result | |
); | |
// Test shortestString. | |
result = new ArrayList<String>(words); | |
String shortestString = shortest(result); | |
System.out.println( | |
"\nThe shortest string should be \"heh\" (if you break ties by taking the one closest to the end).\n" | |
+ "Your answer is: " + shortestString | |
); | |
// Test toCaps. | |
result = new ArrayList<String>(words); | |
toCaps(result); | |
System.out.println( | |
"\nIf you convert everything to caps you should have: [FOO, BAR, BAZ, SLINKY, DINKY, BLINKY, BLEH, MEH, HEH]\n" | |
+ "Your answer is: " + result | |
); | |
} | |
/* Removes every other word from the given list | |
* of words. For example, if the input consists of | |
* 10 words, remove the second, fourth, sixth, eigth, | |
* and tenth words. | |
* | |
* @param words - an ArrayList of Strings | |
*/ | |
public static void removeEveryOther(ArrayList<String> words) { | |
// TODO: implement | |
} | |
/* Removes every word that ends with the suffix "eh". | |
* | |
* @param words - an ArrayList of Strings | |
*/ | |
public static void removeEh(ArrayList<String> words) { | |
// TODO: implement | |
} | |
/* Removes every word that ends with the given suffix. | |
* (this is really similar to the previous question). | |
* | |
* @param words - an ArrayList of Strings | |
* @param suffix - the suffix. Remove words that end with this suffix. | |
*/ | |
public static void removeWordsWithSuffix(ArrayList<String> words, String suffix) { | |
// TODO: implement | |
} | |
/* Removes every word that ends with the suffix "inky". | |
* How can you use the previous method (removeWordsWithSuffix) | |
* to implement the solution for this one? | |
* | |
* @param words - an ArrayList of Strings | |
*/ | |
public static void removeInky(ArrayList<String> words) { | |
// TODO: implement | |
} | |
/* Finds the shortest word (where you compare words by the number | |
* of characters) in the given list of words. | |
* | |
* If there is a tie, then take the one that occurs LATER in the | |
* list. | |
* | |
* @param words - an ArrayList of Strings | |
*/ | |
public static String shortest(ArrayList<String> words) { | |
// TODO: implement | |
return null; | |
} | |
/* BONUS QUESTION WOOHOO. Because why not. | |
* | |
* Converts every word in the given list of words to caps. | |
* You can assume in a word contains only alphabetic characters. | |
* | |
* @param words - an ArrayList of Strings | |
*/ | |
public static void toCaps(ArrayList<String> words) { | |
// TODO: implement | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment