1 // PROGRAM RO PREFORM SOME BASIC OPERATIONS ON A LIST OF STRING NAMES 2 package arraylists; 3 4 import java.util.ArrayList; 5 import java.util.List; 6 7 public class ArrayListProcessing { 8 9 public static void main(String[] args) { 10 //POINT A: ADD SOME STRINGS WHICH REPRESENT NAMES TO AN ARRAY LIST 11 List<String> names = new ArrayList<>(); 12 names.add("Holiday, Billie"); 13 names.add("Claudel, Camille"); 14 names.add("Picasso, Pablo"); 15 names.add("Gallen-Kalela, Akseli "); 16 names.add("Zotto, Osvaldo"); 17 names.add("Lovell, Owen"); 18 names.add("Gomez, Selena"); 19 names.add("Eilish, Billie"); 20 21 22 //POINT B: CREATE AN ARRAYLIST OF JUST THE FIRST NAMES OF THE NAMES 23 List<String> firstNamesList = firstNames(names); 24 25 System.out.print("First names: "); 26 for (String firstName : firstNamesList){ 27 System.out.print(firstName + " "); 28 } 29 System.out.println(); 30 31 // POINT C: Use String’s join function to create and print a String of 32 // just the first names of the names ArrayList with each name 33 // separated by a comma. 34 String firstNames = String.join(", ", firstNamesList); 35 System.out.println("First names: " + firstNames); 36 37 // point d; by analogy from point b and c, print a comma-separate 38 //list of the last names in the namse ArrayList. 39 40 List<String> lastNamesList = lastNames(names); 41 42 System.out.print("Last names: "); 43 for (String lastName : lastNamesList){ 44 System.out.print(lastName + " "); 45 } 46 System.out.println(); 47 48 String lastNames = String.join(", ", lastNamesList); 49 System.out.println("Last names: " + lastNames); 50 51 // POINT E: Print a comma-separated list of all uppercase first names 52 //from the list of names in the names ArrayList. 53 List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names)); 54 String upperCaseFirstNames = String.join(", ", upperCaseFirstNamesList); 55 System.out.println("Uppercase first names: " + upperCaseFirstNames); 56 57 //point f : print a comma seperated list of all hyphenated last names fomr the list 58 // of names in the namse Array List\ 59 List<String> hyphenatedNamesList = hyphenatedNames(lastNames(names)); 60 String hyphenatedNames = String.join(", ",hyphenatedNamesList); 61 System.out.println("Hyphenated names: " + hyphenatedNames); 62 63 //point g : print the integer value of the total length of all names 64 //in the names ArrayList 65 int totalLength = totalNameLength(names); 66 System.out.println("Total length: " + totalLength); 67 68 //point h: print the integer value of the total length of all first names in the names array list 69 int firstLength = totalfirstNameLength(names); 70 System.out.println("First Name length: " + firstLength); 71 72 //point I: print the integer value of the product 73 //of the length of all the first names 74 //in the names array List 75 double productoff = productNames(names); 76 System.out.println(("Product Frist Names: " + productoff)); 77 78 79 80 } 81 82 public static List<String> hyphenatedNames(List<String> names) { 83 List<String> hyphenateds = new ArrayList<>(); 84 for(String name : names) { 85 if (name.contains("-")) { 86 hyphenateds.add(name); 87 } 88 } 89 return hyphenateds; 90 } 91 92 93 94 public static List<String> upperCaseNames(List<String> names){ 95 List<String> uppercases = new ArrayList<>(); 96 for (String name : names){uppercases.add(name.toUpperCase()); 97 }return uppercases; 98 } 99 100 101 private static String firstName(String directoryStyleName) { 102 int positionOfComma = directoryStyleName.indexOf(","); 103 String firstName = directoryStyleName.substring(positionOfComma + 2); 104 return firstName; 105 } 106 private static List<String> firstNames(List<String> names){ 107 List<String> firsts = new ArrayList<>(); 108 for (String name : names){ 109 firsts.add(firstName(name)); 110 } 111 return firsts; 112 } 113 114 private static String lastName(String directoryStyleName) { 115 int positionOfComma = directoryStyleName.indexOf(","); 116 String lastName = directoryStyleName.substring(0,positionOfComma); 117 return lastName; 118 } 119 120 121 private static List<String> lastNames(List<String> names){ 122 List<String> lasts = new ArrayList<>(); 123 for (String name : names){ 124 lasts.add(lastName(name)); 125 } 126 return lasts; 127 } 128 public static int totalNameLength(List<String> names) { 129 int totalLength = 0; 130 for (String name : names) { 131 totalLength = totalLength + name.length(); 132 } 133 return totalLength; 134 } 135 136 public static int totalfirstNameLength(List<String> names) { 137 int totalfirstLength = 0; 138 for (String name : names) { 139 totalfirstLength = totalfirstLength + firstName(name).length(); 140 } 141 return totalfirstLength; 142 } 143 144 public static double productNames(List<String> names) { 145 double productofFirsts = 1; 146 for (String name : names) { 147 productofFirsts = productofFirsts * name.length(); 148 } 149 return productofFirsts; 150 } 151 } 152