Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. package school;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. class L14
  7. {
  8.  
  9. /*
  10. * Returns the number of Strings in the input list that start with the given
  11. * letter. Implementation should use linear search, and ignore the case of
  12. * the input letter.
  13. */
  14. public static int countLetter(ArrayList<String> list, String letter)
  15. {
  16. int count = 0;
  17. for (int i = 0; i < list.size()-1; i++) {
  18. if (Character.toLowerCase((list.get(i)).charAt(0)) == Character.toLowerCase((letter.charAt(0)))) count++;
  19. }
  20. return count;
  21. }
  22.  
  23. public static void main(String str[]) throws IOException
  24. {
  25. /*
  26. * Initialize an ArrayList of animals called zoo.
  27. */
  28. ArrayList<String> zoo = new ArrayList<String>();
  29. zoo.add("Zebra");
  30. zoo.add("Ardvark");
  31. zoo.add("Emu");
  32. zoo.add("Hippo");
  33. zoo.add("Aligator");
  34. zoo.add("Lion");
  35. zoo.add("Giraffe");
  36. zoo.add("Seal");
  37. zoo.add("Tiger");
  38. zoo.add("Elephant");
  39.  
  40. /*
  41. * Print the contents of the zoo.
  42. */
  43. System.out.println(zoo);
  44.  
  45. /*
  46. * Print the output from calling countLetter with various letters. For
  47. * example, countLetter (zoo, "e") should return 2 while
  48. * countLetter(zoo, "W") should return 0.
  49. */
  50. System.out.println("A: " + countLetter(zoo, "A"));
  51. System.out.println("B: " + countLetter(zoo, "B"));
  52. System.out.println("C: " + countLetter(zoo, "C"));
  53. System.out.println("L: " + countLetter(zoo, "L"));
  54. System.out.println("T: " + countLetter(zoo, "T"));
  55.  
  56. System.out.println("a: " + countLetter(zoo, "a"));
  57. System.out.println("b: " + countLetter(zoo, "b"));
  58. System.out.println("c: " + countLetter(zoo, "c"));
  59. System.out.println("l: " + countLetter(zoo, "l"));
  60. System.out.println("t: " + countLetter(zoo, "t"));
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement