Consider the following Java class. Write decorators that will (i) upper-case all
ID: 3673825 • Letter: C
Question
Consider the following Java class. Write decorators that will
(i) upper-case all letters of all words
(ii) filter the list of words, eliminating all words greater than a given word, and
(iii) reverse the list of words. Prove your solution works by providing code that
(iv) initializes a list of words, upper-cases them, reverses the list, and removes all words less than a word of your choosing.
class WordList {
private List<String> words;
public WordList() { words = new ArrayList<String>(); }
public String toString() { String retString = ""; for (String word:words) retString += word + ";";
return retString; } }
Explanation / Answer
import java.io.*;
public class Test{
public static void main(String args[]){
String Str = new String("Welcome to Java");
System.out.print("Return Value :" );
System.out.println(Str.toUpperCase() );
}
}
===========================================
int maxLength = 5;
StringTokenizer tokenizer = new StringTokenizer(input, " ");
StringBuilder builder = new StringBuilder();
while(tokenizer.hasMoreTokens()){
String token = tokenizer.nextToken();
if(token.length() < maxLength){
builder.append(token);
builder.append(" ");
}
}
return builder.toString();
=================================================
public static String reverseWords2(String sentence) {
StringBuilder sb = new StringBuilder(sentence.length() + 1);
String[] words = sentence.split(" ");
for (int i = words.length - 1; i >= 0; i--) {
sb.append(words[i]).append(' ');
}
sb.setLength(sb.length() - 1); // Strip trailing space
return sb.toString();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.