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

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!
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).
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...
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...
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");           &
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");             list.add("Sauer");             list.add("Maynard");             list.add("Namath");             list.add("Boozer");             list.add("Snell");             list.add("Namath");             list.add("Atkinson");             list.add("Lammonds");             list.add("Dockery");             list.add("Darnold");             list.remove(2);             list.set(2, "Brady");             list.remove(2);             list.set(4,"Unitas");             list.add(1,"Lamomica");             list.add(3,"Hanratty");             list.remove("Namath");             list.remove(list.size()-1);             list.remove(2);             list.set(7, "Riggins");             Iterator iter = list.iterator();           while (iter.hasNext())         {   System.out.print(iter.next() + " ");                         }                     } }
If you wanted to store a birthday within code, what class would you use in Java?...
If you wanted to store a birthday within code, what class would you use in Java? and why??
in java Jimmy wants to store course grades and corresponding student last names using two parallel...
in java Jimmy wants to store course grades and corresponding student last names using two parallel arraylists (Double and String). She also wants to identify the average of all the grades. Use a while loop. Prompt the user for each grade and use -1 as the sentinel. Ask for the student name if -1 has not been entered for the grade. Be sure to output the average of the grades. (**Hint: You will need to figure out the sum first)...
A pet store completed a survey of customers regarding pet preferences and ownership. The results for...
A pet store completed a survey of customers regarding pet preferences and ownership. The results for dog ownership and feelings about cats are displayed in the table. Use them to answer the following questions. Likes cats & Owns a dog =13 Likes cats & does not own a dog= 30 Total=43 Dislikes cats & owns a dog=18 Dislikes cats & does not own a dog=18 Total=36 No opinion about cats & owns a dog= 0 No opinion about cats &...
how to use java ArrayList to get the result? removing consecutive duplicates eg [1 2 2...
how to use java ArrayList to get the result? removing consecutive duplicates eg [1 2 2 3 2 2 1] ------->[1 2 3 2 1]
Computer Science - Java Programming How do you read a text file and store three different...
Computer Science - Java Programming How do you read a text file and store three different pieces of information in the database when the given text file contains this info.: 12345 Computer Science Bob Stone 23456 Art James G. Ocean? These are written in the format as ID Class Name. I was going to take the three different pieces of information by separating them by spaces, but the number of spaces is random and I don't know how to adjust...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT