Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. package midtermq1;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. /**
  6. *
  7. * @author steventroung
  8. */
  9. public class Q1 {
  10.  
  11. /**
  12. * @param args the command line arguments
  13. */
  14. public static void main(String[] args) {
  15. ArrayList<String> words = new ArrayList<String>();
  16. words.add("four");
  17. words.add("score");
  18. words.add("and");
  19. words.add("seven");
  20. words.add("years");
  21. words.add("ago");
  22. //here goes the line to display the ArrayList
  23. for (int i = 0; i < words.size(); i++) {
  24. System.out.print(words.get(i) + ", ");
  25. }
  26.  
  27. //here is the call to the method you are to write
  28. System.out.println("\nThe smallest words is " + smallest(words));
  29. }
  30.  
  31. public static String smallest(ArrayList<String> words) {
  32. String short = words.get(0);
  33. for (int i = 1; i < words.size(); i++) {
  34. if (words.get(i).length() < short.length()) {
  35. short = words.get(i);
  36. }
  37. }
  38. return short;
  39. }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement