a method that takes an arraylist string as argument and
play

// A method that takes an ArrayList<String> as argument and // - PDF document

// A method that takes an ArrayList<String> as argument and // returns true if the first String has length 5 // false otherwise // If list is null or the ArrayList is empty, return false public boolean


  1. // A method that takes an ArrayList<String> as argument and // returns true if the first String has length 5 // false otherwise // If list is null or the ArrayList is empty, return false public boolean FirstStringOfLength5(ArrayList<String> list) { if (list == null || list.size() == 0) { return false; } String firstString = list.get(0); return firstString != null && firstString.length() == 5; } // A method that takes an ArrayList<String> as argument and // returns true if contains is a String of length 5 // false otherwise // If list is null or the ArrayList is empty, return false public boolean HasStringOfLength5(ArrayList<String> list) { if (list == null || list.size() == 0) { return false; } for (String s : list) { if (s != null && s.length() == 5) { return true; } } return false; }

  2. // A method that takes an ArrayList<String> as argument and // returns the number of Strings of length 5 it contains // // If list is null or the ArrayList is empty, return 0 // // USE AN ENHANCED FOR LOOP public int HowManyStringsOfLength5_v1(ArrayList<String> list) { if (list == null || list.size() == 0) { return 0; } int total = 0; for (String s : list) { if (s != null && s.length() == 5) { total = total + 1; } } return total; }

  3. // A method that takes an ArrayList<String> as argument and // returns the number of Strings of length 5 it contains // // If list is null or the ArrayList is empty, return 0 // // USE A (REGULAR) FOR LOOP public int HowManyStringsOfLength5_v2(ArrayList<String> list) { if (list == null || list.size() == 0) { return 0; } int total = 0; for (int i = 0; i < list.size(); i = i + 1) { String s = list.get(i); if (s != null && s.length() == 5) { total = total + 1; } } return total; }

  4. // A method that takes an ArrayList<String> as argument and // returns the number of Strings of length 5 it contains // // If list is null or the ArrayList is empty, return 0 // // USE AN ITERATOR-CONTROLLED WHILE LOOP public int HowManyStringsOfLength5_v3(ArrayList<String> list) { if (list == null || list.size() == 0) { return 0; } int total = 0; Iterator<String> it = list.iterator(); while (it.hasNext()) { String s = it.next(); if (s != null && s.length() == 5) { total = total + 1; } } return total; }

  5. // A method that takes an ArrayList<String> and an int as arguments and // returns the number of Strings of the given length it contains // If list is null or the ArrayList is empty, return 0 public int howManyStringsOfGivenLength (ArrayList<String> list, int len) { int total = 0; for (String s : list) { if (s.length() == len) { total = total + 1; } } return total; }

  6. // A method that takes a String as an argument and // returns true if the String starts with the uppercase // letter 'A', false otherwise public boolean StartsWithUpperCaseA(String s) { return s.charAt(0) == 'A'; } // A method that takes a char as an argument and // returns true if is is a lower case vowel (one // of 'a', 'e', 'i', 'o' or 'u', false otherwise. public boolean isVowel(char ch) { return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'; }

  7. // A method that takes a String as an argument and // prints each char in the String on its own line, // with single quotes around each character. public void PrintChars(String s) { for (int i = 0; i < s.length(); i = i + 1) { System. out .println("'" + s.charAt(i) + "'"); } } // A method that takes a String as an argument and // returns true if the String contains an upper case // letter 'A', false otherwise public boolean ContainsUpperCaseA(String s) { for (int i = 0; i < s.length(); i = i + 1) { if (s.charAt(i) == 'A') { return true; } } return false; }

  8. // A method that takes a String as an argument and // returns true if the String contains a lower case // vowel, false otherwise. public boolean ContainsVowel(String s) { for (int i = 0; i < s.length(); i = i + 1) { if (isVowel(s.charAt(i))) { return true; } } return false; } // A method that takes a String as an argument and // returns how many lower case vowels it contains. public int HowManyVowels(String s) { int total = 0; for (int i = 0; i < s.length(); i = i + 1) { if (isVowel(s.charAt(i))) { total = total + 1; } } return total; }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend