Question

JAVA In this PoD you will use an ArrayList to store different pet names (there are...

JAVA

In this PoD you will use an ArrayList to store different pet names (there are no repeats in this list). This PoD can be done in your demo program (where your main method is) – you don’t have to create a separate class for today.

Details

Create an arraylist of Strings, then using a Scanner object you will first read in a number that will tell you how many pet names (one word) you will add to the arraylist.

Once you have added all the names, print the list.

Then read in one more pet name. If this is pet name already exists, do nothing, but if the pet name isn’t in the list, add it to the front of the list (e.g., the first position).

Print the list again.

Next read in two more pet names. If the first pet name is in the list, replace it with the second pet name. If the first pet name isn’t in the list, add the second pet name to the end of the list.

Print the list again.

Input

Example Input:
5 Whiskers Benji Lassie Smokey Bob Triffy Bob Max

Output

Example Output:
[Muffin, Benji, Snowball, Fluffy]
[Muffin, Benji, Snowball, Fluffy]
[Muffin, Benji, Snowball, Whiskers, Fluffy]

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

//import required library files
import java.util.ArrayList;
import java.util.*;

//class definition
public class petNames {
//main function definition
public static void main(String[] args) {
   
    //Create the arraylist
    ArrayList<String> petNames = new ArrayList<String>();
   
    //create scanner object to take user input
    Scanner sc = new Scanner(System.in);
   
    //prompt user to enter number of pet names and store in count
    System.out.print("How many pet names to enter? ");
    int count = sc.nextInt();
   
    //Take user input for petnames and store in arraylist
   
    System.out.println("Enter "+count+" pet names");
    String str="";
    for(int i=0;i<count;i++){
      str=sc.next();
      petNames.add(str);
    }
   
    //print the list
    System.out.println(petNames);
   
    //Prompt user to enter one more pet name
    System.out.println("Enter one more pet name");
   
    String pet = sc.next();
   
    //If pet name not present in arraylist add at index 0
    if(!petNames.contains(pet)){
      petNames.add(0,pet);
    }
   
    //print the list
    System.out.println(petNames);
   
    //prompt user to enter two more pet names
    System.out.println("Enter two more pet names");
   
    //Store user input in two variables
    String pet1,pet2;
   
    pet1=sc.next();
    pet2=sc.next();
   
    //If the first pet name is in the list, replace it with the second pet name.
    // If the first pet name isn’t in the list, add the second pet name to the end of the list.
   
    if(petNames.contains(pet1))
    {
      int index = petNames.indexOf(pet1);
      petNames.remove(index);
      petNames.add(index,pet2);
    }
    else {
      petNames.add(pet2);
    }
   
    //Print the list
    System.out.println(petNames);
   
}

}

Add a comment
Know the answer?
Add Answer to:
JAVA In this PoD you will use an ArrayList to store different pet names (there are...
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
  • JAVA In this PoD you will use an ArrayList to store different cities). This PoD can...

    JAVA In this PoD you will use an ArrayList to store different cities). This PoD can be done in one file. Details Create an arraylist of Strings, then using a Scanner object you will first read in a number that will tell you how many cities you will add to the arraylist. You should make sure as your read in cities, that you do not add cities that are already in the list (e.g., don’t allow any repeats). Once you...

  • Array lists are objects that, like arrays, provide you the ability to store items sequentially and...

    Array lists are objects that, like arrays, provide you the ability to store items sequentially and recall items by index. Working with array lists involves invoking ArrayList methods, so we will need to develop some basic skills. Let's start with the code below: The main method imports java.utii.ArrayList and creates an ArrayList that can hold strings by using the new command along with the ArrayList default constructor. It also prints out the ArrayList and, when it does, we see that...

  • Java ArrayList Sorting Alphabetically

    1. Write a program to input a list of names (strings) from the user and store them in an ArrayList. The input can be terminated by entering the empty string or byentering the string “quit”.2. Add further functionality to your program so that it searches the ArrayList to find the first string and the last string according to dictionary ordering and then prints out these strings (names). Do this exercise without sorting the names in the ArrayList. For example, if...

  • JAVA This PoD, builds off the Book class that you created on Monday (you may copy...

    JAVA This PoD, builds off the Book class that you created on Monday (you may copy your previously used code). For today’s problem, you will add a new method to the class called lastName() that will print the last name of the author (you can assume there are only two names in the author name – first and last). You can use the String spilt (“\s”) method that will split the String by the space and will return an array...

  • 1. Write a program to input a list of names (strings) from the user and store...

    1. Write a program to input a list of names (strings) from the user and store them in an ArrayList. The input can be terminated by entering the empty string or by entering the string “quit”. 2. Add further functionality to your program so that it searches the ArrayList to find the first string and the last string according to dictionary ordering and then prints out these strings (names). Do this exercise without sorting the names in the ArrayList. For...

  • Create a java program that reads an input file named Friends.txt containing a list 4 names...

    Create a java program that reads an input file named Friends.txt containing a list 4 names (create this file using Notepad) and builds an ArrayList with them. Use sout<tab> to display a menu asking the user what they want to do:   1. Add a new name   2. Change a name 3. Delete a name 4. Print the list   or 5. Quit    When the user selects Quit your app creates a new friends.txt output file. Use methods: main( ) shouldn’t do...

  • To do: Now add code to Pet and PetPlay to complete the comments in the code....

    To do: Now add code to Pet and PetPlay to complete the comments in the code. import java.util.ArrayList; public class PetPlay {    //-----------------------------------------------------------------    // Stores and modifies a list of pets    //----------------------------------------------------------------- public static void main (String[] args)    { // always make your array lists generic ArrayList pets = new ArrayList(); pets.add(new Pet("dog", "Bella")); pets.add(new Pet("cat","Blackie")); pets.add(new Pet("bird", "Babs")); pets.add(new Pet("dog", "Lassie")); pets.add(new Pet("horse", "Sam")); pets.add(new Pet("dog", "Blackie")); pets.add(new Pet("turtle", "Joe")); pets.add(new Pet("bird","Annie")); pets.add(new Pet("bird", "Alex"));...

  • *****Requiremrnt: 1. Please do not use any array or arraylist 2. collects only a single String...

    *****Requiremrnt: 1. Please do not use any array or arraylist 2. collects only a single String input 3. uses at least 3 String methods 1) Just Initials: Print just the initials in upper case letters separated by periods 2) Last Name First With Middle Initial: Print the last name in lower case with the first letter capitalized, followed by a comma and the first name in in lower case with the first letter capitalized and then the middle initial capitalized...

  • Need help finishing this code # Description : The Pet class contains fields for name, pet...

    Need help finishing this code # Description : The Pet class contains fields for name, pet # type,and age. The age field is the age now # or age of pet when it passed away. (These # are all dogs I have had.) There is a static or # class variable called number_pets that # tracks how many instances have been created. # The constructor will fill all fields and add # one to the number_pets variable. # The special...

  • Write a java project that reads a sequence of up to 25 pairs of names and...

    Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes for individuals (sample input data is attached). Store the data in an object designed to store a first name (string), last name (string), and postal code (integer). Assume each line of input will contain two strings followed by an integer value, each separated by a tab character. Then, after the input has been read in, print the list in an appropriate...

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