Guest User

Untitled

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