Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

. Creates a string array called firstNames that holds these values \"John\", \"F

ID: 3701442 • Letter: #

Question

. Creates a string array called firstNames that holds these values "John", "Frank", "Nick", "Amanda", "Brittany", "Amy", "Deborah", and "Stirling".

2. Creates another string array called lastNames that holds these values "Thompson", "Smith", "Jones", "Keribarian", "Lu", "Banafsheh", "Spielberg", "Jordan", "Castle" and "Simpson".

3. Now add a method that creates an array list called randomNames which picks a random first name from the array in step 1 and a random last name in the array from step 2 and creates a new string containing the first name followed by a space " " followed by the last name (for example "John Lu") and adds it to the array list called randomNames. Do this 50 times.

Explanation / Answer

StringArrayTest.java

import java.util.ArrayList;

import java.util.Random;

public class StringArrayTest {

public static void main(String[] args) {

String firstNames[] ={"John", "Frank", "Nick", "Amanda", "Brittany", "Amy", "Deborah", "Stirling"};

String lastNames[] = {"Thompson", "Smith", "Jones", "Keribarian", "Lu", "Banafsheh", "Spielberg", "Jordan", "Castle","Simpson"};

System.out.println("Random Names List: ");

System.out.println(generateNames(firstNames, lastNames));

}

public static int fun(int[] a, int n) {

int s = 0;

for (int i = 0; i < n&&s <= 1000; i++) {

if (a[i] % 5 == 1 || a[i] % 5 == 3) {

s += a[i];

} else if (a[i] % 5 == 2) {

s -= a[i];

} else {

s = s << 1;

}

}

return s;

}

public static ArrayList<String> generateNames(String firstNames[], String lastNames[]) {

ArrayList<String> randomNames = new ArrayList<String>();

Random r = new Random();

for(int i=0;i<50;i++) {

String s = firstNames[r.nextInt(firstNames.length)]+" "+lastNames[r.nextInt(lastNames.length)];

if(randomNames.contains(s)) {

i--;

} else {

randomNames.add(s);

}

}

return randomNames;

}

}

Output:

Random Names List:
[John Lu, Amanda Banafsheh, Stirling Castle, Frank Simpson, Deborah Spielberg, Nick Thompson, Frank Banafsheh, John Banafsheh, Stirling Banafsheh, Nick Jordan, Stirling Lu, Brittany Banafsheh, Frank Jordan, Nick Spielberg, Amanda Spielberg, Amanda Keribarian, Deborah Smith, Amanda Thompson, Frank Keribarian, Frank Spielberg, Deborah Thompson, Amanda Simpson, Deborah Simpson, Amy Castle, Brittany Jordan, Nick Lu, Brittany Castle, Brittany Spielberg, Stirling Keribarian, Brittany Thompson, Nick Keribarian, Amy Keribarian, Deborah Castle, Stirling Smith, John Thompson, Stirling Jones, Deborah Jordan, Amy Smith, John Jones, Stirling Spielberg, Amy Thompson, Amanda Jones, Brittany Keribarian, Amy Lu, Frank Smith, Nick Banafsheh, Frank Lu, Deborah Lu, John Keribarian, Brittany Lu]