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

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

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

  • *****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...

  • 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, street address, city, state, and 10-digit phone number for individuals. Store the data in an object designed to store a first name (string), last name (string), and postal code (integer), street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-' . Assume each...

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • In Java, Please do not forget the ToString method in the Student class! In this exercise,...

    In Java, Please do not forget the ToString method in the Student class! In this exercise, you will first create a Student class. A Student has two attributes: name (String) and gradePointAverage (double). The class has a constructor that sets the name and grade point average of the Student. It has appropriate get and set methods. As well, there is a toString method that prints the student's name and their grade point average (highest is 4.0) You will then write...

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

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