Question

In: Computer Science

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]

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Scanner;

public class PetsTest {
public static void main(String[] args) {
   ArrayList<String>list= new ArrayList<String>();
   int num;
   String str;
   Scanner sc = new Scanner(System.in);
   System.out.println("Enter number of pets");
   num=sc.nextInt();
   sc.nextLine();
   // reading names
   System.out.println("Enter "+num+" pet names");
   for(int i=0;i<num;i++){
       list.add(sc.next());
   }
   //reading another pet name
   System.out.println("Enter petname");
   str=sc.next();
   // checking if it is in list , if not adding to front
   if(!list.contains(str)){
       list.add(0, str);
   }
   // prints list
   System.out.println(list);
   //reading more 2 pet names
   System.out.println("Enter 2 pet names");
   str=sc.next();
   //
   String str1=sc.next();
   // checking if it is in list , if yes than replacing it with second one,
   if(list.contains(str)){
       list.set(list.indexOf(str), str1);
   }
   // else adding it in the end
   else{
       list.add(str1);
   }
   System.out.println(list);
}
}


Related Solutions

Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist to store/ add product in stock 3. Product must have name, code number and quantity in stock 4. Print list of the products and their stock quantity 5. Search products based on their name 6. Remove product based on their code number 7. Sell product and deduct quantity from stock 8. Stock Class contains methods of search product, add product and check quantity of...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist to store/ add product in stock 3. Product must have name, code number and quantity in stock 4. Print list of the products and their stock quantity 5. Search products based on their name 6. Remove product based on their code number 7. Sell product and deduct quantity from stock 8. Stock Class contains methods of search product, add product and check quantity of...
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c]...
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c] , [d,e], [f] ] ----> [ [a,d,f], [a,e,f], [b,d,f], [b,e,f], [c,d,f], [c,e,f]] [ [a,b], [a,b,c]] ----->[[a,a],[a,b],[a,c], [b,a],[b,b],[b,c] assuming abc are integer Thanks in advance!
In Java, you can iterate an ArrayList in different ways. Write the following methods to print...
In Java, you can iterate an ArrayList in different ways. Write the following methods to print Integers in an ArrayList iterating in different ways: 1. // Using basic while / for loop void printArrayListBasicLoop(ArrayList<Integer> al); 2. // Using enhanced for loop (:) void printArrayListEnhancedLoop(ArrayList<Integer> al); 3. // Using basic for loop with iterator void printArrayListForLoopListIterator(ArrayList<Integer> al); 4. // Using basic while loop with iterator void printArrayListWhileLoopListIterator(ArrayList<Integer> al);
Java. You are creating a 'virtual pet' program. The pet object will have a number of...
Java. You are creating a 'virtual pet' program. The pet object will have a number of attributes, representing the state of the pet. You will need to create some entity to represent attributes in general, and you will also need to create some specific attributes. You will then create a generic pet class (or interface) which has these specific attributes. Finally you will make at least one subclass of the pet class which will be a specific type of pet,...
Two different genes controlling pod morphology in peas are found on chromosome 5. The narrow pod...
Two different genes controlling pod morphology in peas are found on chromosome 5. The narrow pod trait is recessive to wide pods and yellow pods is recessive to green pods. A heterozygous plant is generated by mating a narrow green pod plant with a wide yellow pod plant. This plant was testcrossed and the following progeny were obtained. What is the linkage relationship between the two genes? 144 Narrow green pods , 150 wide yellow pods , 110 narrow yellow...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and a method to get all the values of the queue. (This is not writing a file implementing the java class PriorityQueue, but rather you are writing a program that is a priority queue).
Financial Ratios Financial statements for Paulson's Pet Store are shown. Paulson's Pet Store Income Statement For...
Financial Ratios Financial statements for Paulson's Pet Store are shown. Paulson's Pet Store Income Statement For Year Ended December 31, 20-- Revenue from sales:    Sales $326,040    Less: Sales returns and allowances 5,360       Net sales $320,680 Cost of goods sold:    Merchandise inventory, January 1, 20-- $29,300    Estimated returns inventory, January 1, 20-- 900 $30,200    Purchases $162,630    Less: Purchases returns and allowances $4,080    Less: Purchases discounts 3,200 7,280    Net purchases $155,350    Add freight-in 1,600       Cost of goods purchased 156,950    Goods available for...
In java, (include javadoc comments for each method, please use ArrayList, not Array, and ensure that...
In java, (include javadoc comments for each method, please use ArrayList, not Array, and ensure that the program compiles). design a class named Contact that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then, write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that...
Use Java A pet shop wants to give a discount to its clients if they buy...
Use Java A pet shop wants to give a discount to its clients if they buy one or more pets and at least five other items. The discount is equal to 20 percent of the cost of the other items, but not the pets. Use a class Item to describe an item, with any needed methods and a constructor public Item(double price, boolean isPet, int quantity) An invoice holds a collection of Item objects; use an array or array list...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT