Write a Java program that does the following: 1. Creates a string array called f
ID: 3701443 • Letter: W
Question
Write a Java program that does the following:
1. 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.
For 10 extra credit points, make sure the array list randomNames has only unique names, no duplicate names (hint: look at the set data structure in Java, there are other ways to do this as well).
Explanation / Answer
StringArrayTest.java
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
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();
while(randomNames.size()<50) {
String s = firstNames[r.nextInt(firstNames.length)]+" "+lastNames[r.nextInt(lastNames.length)];
randomNames.add(s);
Set<String> hs = new HashSet<String>();
hs.addAll(randomNames);
randomNames.clear();
randomNames.addAll(hs);
hs.clear();
}
return randomNames;
}
}
Output:
Random Names List:
[Amy Jones, Brittany Thompson, Frank Castle, Stirling Lu, Brittany Smith, Amy Keribarian, Brittany Lu, Nick Keribarian, Amanda Spielberg, Amy Spielberg, Deborah Thompson, Brittany Banafsheh, Frank Smith, Amanda Smith, Brittany Jordan, John Thompson, Brittany Jones, John Castle, Nick Smith, Frank Spielberg, Frank Banafsheh, John Jordan, Amy Banafsheh, Frank Keribarian, Stirling Jordan, Amanda Simpson, Stirling Simpson, Amy Lu, Brittany Spielberg, Nick Castle, Frank Lu, Stirling Spielberg, Deborah Lu, Stirling Keribarian, Deborah Simpson, Amy Smith, Frank Thompson, Stirling Thompson, Amanda Keribarian, Nick Thompson, Amanda Jordan, Amanda Thompson, John Banafsheh, Stirling Banafsheh, John Jones, Stirling Jones, Amy Jordan, Deborah Spielberg, Amy Castle, John Lu, John Keribarian]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.