Question

Write a Java program that does the following: 1. Creates a string array called firstNames that...

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).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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]

Add a comment
Know the answer?
Add Answer to:
Write a Java program that does the following: 1. Creates a string array called firstNames that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Java program application that creates an ArrayList that holds String objects. It needs to...

    Create a Java program application that creates an ArrayList that holds String objects. It needs to prompt the user to enter the names of friends, which are then put into the ArrayList; allow the user to keep adding names until they enter "q" to quit. After input is complete, display a numbered list of all friends in the list and prompt the user to enter the number of the friend they wish to delete. After deleting that entry, output the...

  • Write a Java program in Eclipse. . Write a Java program that: 1. Creates an array...

    Write a Java program in Eclipse. . Write a Java program that: 1. Creates an array of 100 integers. Initialize the array to have all zeros. 2. Puts even numbers starting from 6 (excluding multiples of 12) into the first 50 slots of the array above. So numbers 6,8,10,14,16,18,20,22,26, etc. go into the first 50 positions in the array. Print the array. 3. Puts random numbers between 45 and 55 into the second 50 slots of the array above (second...

  • JAVA PLEASE! Write a method called displayPets that takes an array of strings as input. The...

    JAVA PLEASE! Write a method called displayPets that takes an array of strings as input. The method should display the contents of the pets array on one line. Each name should be separated by a space. Use an enhanced for loop to iterate the array. Make sure to declare modifier, parameters and return values. Given an array String[] pets = {"Lucky", "Slinger", "Beast", "Trumpet", "Lulu", "Shadow", "Daisy"} A sample run when calling displayPets(pets) from main() would look like: Pets names:...

  • Please complete the lab following the guidelines using JAVA Activity 1 Write a program called AnalyzeNumbers....

    Please complete the lab following the guidelines using JAVA Activity 1 Write a program called AnalyzeNumbers. This program will have array that holds 10 random integers from 1 to 5. Output the 10 random numbers to the screen using an enhanced for loop. This program will also have a method called frequency that takes an integer array as the parameter and it will return an array that holds the frequency of numbers. Index 0 will hold the frequency of 1,...

  • Using Java Create an application that creates and displays a list of names using an array....

    Using Java Create an application that creates and displays a list of names using an array. Then modify the application to create and use a list of numbers. Console for the names application Please enter a name or type “exit” to quit: Diane Please enter a name or type “exit” to quit: Joe Please enter a name or type “exit” to quit: Greta Please enter a name or type “exit” to quit: Henry Please enter a name or type “exit”...

  • Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...

    Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called hw1 Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”) Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method). Add a comment to the main that includes your name Write code that demonstrates the use of each of the following basic...

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

  • ( Object array + input) Write a Java program to meet the following requirements: 1. Define...

    ( Object array + input) Write a Java program to meet the following requirements: 1. Define a class called Student which contains: 1.1 data fields: a. An integer data field contains student id b. Two String data fields named firstname and lastname c. A String data field contains student’s email address 1.2 methods: a. A no-arg constructor that will create a default student object. b. A constructor that creates a student with the specified student id, firstname, lastname and email_address...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts...

    JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. "Out of Bounds") and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT