quiz حل الأسئلة الجامعية manage_search الأرشيف

تم الحل ✓
categoryبرمجة وتطوير البرمجيات schoolبكالوريوس event_available2026-07-15

السؤال

Transcribed Image Text:

Q1: Suppose you want to design a class to store the contents of grocery bag that can contain duplicate and unordered items. The size of the bag can increase and shrink as necessary and you need to store it to know the current number of items in the bag. Below is the description of the operations that you need to include inside the grocery bag class. public boolean add(String newitem) Task: Adds a new item to the bag. Increases the number of items by one. Param: newltem the object to be added as a new item o Return true if the addition is successful, or false if not public String remove(String anItem) 。 Task: Removes the first occurrence of an item. Decreases the number of items by one. o Param anltem: the object to be removed Return either the entry removed if the removal was successful, or null ⚫ public void clear() Task: Removes all entries from the bag. Reset the number of items. ⚫ public boolean contains(String anltem); o Task: Tests whether the bag contains a given item. 。 Param anltem: the object that is the desired entry Return true if the bag contains anltem, or false if not public int count(String anItem) Task: Determines the number of times anltem appears in a bag. o Param anltem: the object that is the desired item Return: the number of number of times anltem appears in the bag public int getSize() o Task: Gets the size of the bag. Return the integer number of items currently in the bag ⚫ public boolean isEmpty() Task: Sees whether the bag is empty. Return true if the bag is empty, or false if not. ⚫ public void display() Task: Displays all items that are in the bag, one per line, in the order in which they occur in the bag. public GroceryBag union (Grocery Bag anotherBag); o Task: Creates a new bag that combines the contents of the bag and anotherBag. o Param anotherBag: the Bag that is to be added o Return a combined Bag 1 public GroceryBag intersection (Grocery Bag anotherBag) o Task: Creates a new bag that contains those items that occur in both the bag and anotherBag. o Param another Bag: the Bag that is to be compared o Return a combined Bag public GroceryBag difference (Grocery Bag anotherBag) o Task: Creates a new bag of items that would be left in the bag after removing those that also occur in anotherBag. o Param anotherBag: the Bag that is to be removed o Return a combined Bag Your task is to: 1. Choose the best data structure to represent the grocery bag and the items that it contains from (Array, Singly Linked List, Doubly Linked List). Justify your selection. 2. Implement the class Grocery Bag and Item class using your chosen data structure in Java. 3. Implement the above methods in Java. 4. Provide a test class program. In main, create two GrocerryBag objects; bag1, bag2. Provide a list of actions for the user to choose from according to the shown sample output. Hello, welcome to Food Mania Grocery Store. Please, choose one of the following: 1) Add item to a bag 2) Remove an item from a bag 3) Clear the contents of a bag 4) Find an item in a bag 5) Find the quantity of an item in a bag 6) Find the total number of items in a bag 7) Display the contents of a bag 8) Create the union bag 9) Create the intersection bag 10) Create the difference bag that has items of bag1 but not in bag2 11) Exit 1 Which bag you want to add to, 1 or 2? Enter the items you want to add, $ to stop milk egg carrot apple carrot 2

check_circle الجواب — حل مفصل خطوة بخطوة

hourglass_top