Advertisement
StefanTobler

T2 Lesson 14 Activity

Mar 9th, 2017
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 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).indexOf(letter.toUpperCase()) == 0)
  22.        count++;
  23.        
  24.   }
  25.    return count;
  26.  }
  27.  
  28.  public static void main(String str[]) throws IOException
  29.  {
  30.   /*
  31.    * Initialize an ArrayList of animals called zoo.
  32.    */
  33.   ArrayList<String> zoo = new ArrayList<String>();
  34.   zoo.add("Zebra");
  35.   zoo.add("Ardvark");
  36.   zoo.add("Emu");
  37.   zoo.add("Hippo");
  38.   zoo.add("Aligator");
  39.   zoo.add("Lion");
  40.   zoo.add("Giraffe");
  41.   zoo.add("Seal");
  42.   zoo.add("Tiger");
  43.   zoo.add("Elephant");
  44.  
  45.   /*
  46.    * Print the contents of the zoo.
  47.    */
  48.   System.out.println(zoo);
  49.  
  50.   /*
  51.    * Print the output from calling countLetter with various letters. For
  52.    * example, countLetter (zoo, "e") should return 2 while
  53.    * countLetter(zoo, "W") should return 0.
  54.    */
  55.   System.out.println("A: " + countLetter(zoo, "A"));
  56.   System.out.println("B: " + countLetter(zoo, "B"));
  57.   System.out.println("C: " + countLetter(zoo, "C"));
  58.   System.out.println("L: " + countLetter(zoo, "L"));
  59.   System.out.println("T: " + countLetter(zoo, "T"));
  60.  
  61.   System.out.println("a: " + countLetter(zoo, "a"));
  62.   System.out.println("b: " + countLetter(zoo, "b"));
  63.   System.out.println("c: " + countLetter(zoo, "c"));
  64.   System.out.println("l: " + countLetter(zoo, "l"));
  65.   System.out.println("t: " + countLetter(zoo, "t"));
  66.  }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement