Question

JAVA You will be given a grocery list, filed by a sequence of items that have already been purchased.

 Instructions

 You will be given a grocery list, filed by a sequence of items that have already been purchased. You are going to determine which items remain on the the list and output them so that you know what to buy.

 You will be give an integer n that describes how many items are on the original grocery list. Following that, you will be given an array of n grocery list items (strings) that you need to buy. After your grocery list is complete, you will receive a list of items that had already been purchased. For each of these items, if it matches any item on your grocery list, you can mark that item as purchased. You will know that you are at the end of the list of items already purchased when you receive the string "DONE".

 At that point, you will output a list of items left to buy (each item on its own line).

 Write the body of the program called PoD. java to the left.


 Input

 The program reads in the following:

 an integer (n) defining the length of the original grocery list

 in strings that make up the grocery list a list of items that had already been purchased (strings)

 in strings that make up the grocery list a list of items that had already been purchased (strings)

 the string "DONE", marking the end of all required input

 Processing

 Determine which items on the grocery list have already been purchased.

 Output

 Output the items from the grocery list that remain to be purchased (i.e. all items from the original n grocery items that were not

 included in the list of items already purchased). Each grocery item must be printed on its own line. The text must be

 purchased). Each grocery item must be printed on its own line. The text must be terminated by a new-line character.

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

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class PoD
{
   public static void main (String[] args)
   {
       Scanner sc = new Scanner(System.in);
       int n = sc.nextInt();
       HashMap grocery_list = new HashMap<>();
      
       // add item to grocery list initially assuming none of item has been purchased
       // so make them zero
       for(int i=0;i            String item = sc.next();
           grocery_list.put(item, 0);
       }
      
       // as item list are purchased we mark them purchased by making it 1
       // if DOne is found we break out of loop
       while(true){
           String item = sc.next();
           if(item.equals("DONE"))
                break;
           grocery_list.put(item, 1);
       }
      
       //print item which are not purchased
       Set itemset = grocery_list.keySet();
       for(String key: itemset){
           if(grocery_list.get(key) == 0)
                System.out.println(key);
       }
   }
}

INPUT

5 apple orange eggplant carrots bananas orange carrots DONE

OUTPUT

apple
bananas
eggplant
Add a comment
Know the answer?
Add Answer to:
JAVA You will be given a grocery list, filed by a sequence of items that have already been purchased.
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
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