Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a New Class named ShoppingTrip IMPORT the necessary Java package for the ArrayList library class Declare an ArrayList of String field named foods (
Create a New Class named ShoppingTrip IMPORT the necessary Java package for the ArrayList library class Declare an ArrayList of String field named foods no initialization Write a CONSTRUCTOR with NO parameters that instantiates foods MUST be explicit with base data type of generic collection included Add accessor getFoods returning appropriate type for the foods field MUST be explicit with base data type of generic collection included Add accessor getNumberOfFoods containing ONLY statement in the body to return the number of items in the foods ArrayList collection Add protective mutator boolean addFoodString foodName to add a valid foodName to foods AND return result of successful add or not MUST parameter check foodName with ifelse to be valid nonnullo if valid print ADDED food with actual food value, then use ONLY statement to add the item AND return the result of successful add HINT: look in API to utilize the return of the ArrayList add methodo else print Error: can NOT add invalid food then return false Add overloaded void addFoodint index, String foodName also as a protective mutator to add a valid foodName at a valid index MUST validity check index lower & upper bounds AND foodName nonnull within ONE compound expression for the conditiono if valid print ADDED food at index index with actual food and index values, then add foodName to foods at the indexo else print Error: can NOT add with invalid indexfood Add protective mutator void addFoodFirstString foodName to add a valid foodName at the beginning of foods as the very FIRST element MUST parameter check foodName with ifelse to be valid nonnullo if valid print ADDED food as first item with actual food value, then add the item at the beginning with the appropriate indexo else print Error: can NOT add invalid food Add void removeFoodint index to remove item at index from foods MUST parameter check index lower & upper bounds with ifelseo if valid use ONLY statement to remove item at index AND print REMOVED food from index index with actual foodindex values HINT: look in API to utilize the return of ArrayList remove methodo else print Error: NO food found at index index with actual index Add overloaded boolean removeFoodString foodName also as a protective mutator to remove the FIRST instance found of foodName in foods AND return the result of whether the removal was successful MUST parameter check foodName with ifelse to be valid nonnullo if valid print REMOVED food with actual food value, then use ONLY statement to remove the first instance of the item if it exists AND return the result of successful removal or notHINT: look in API to utilize the return of ArrayList remove methodo else print Error: can NOT remove invalid food then return false Add method listFoodint index to print ONLY the food item specified MUST parameter check index lower & upper bounds with ifelseo if valid use ONLY statement to retrieve the food at the specific index AND print food at index index with actual foodindex valueso else print NO food found at index index with actual index value Add method named listAllFoods to print ALL the food items in sequence from the foods ArrayList collection MUST first check any items in foods with ifelse & isEmptyo if empty print the message NO food items to listo else only if at least item print the heading Food items: on its own line, then MUST use a foreach loop to retrieve each item in sequence from the foods collection and print on its own line Add method void listFoodMatchingString searchFood to ONLY print the food items in foods that CONTAIN the searchFood String case insensitive and regardless of case MUST declare and use a boolean local variable named noMatch to track if any instances of searchFood have yet been foundHINT: MUST also initialize all local variables at time of declaration with an initial value that intuitivelyinherently makes sense at that timeie is there initially no match true or false? ALWAYS print on its own line the heading Food items containing searchFood: where searchFood is the actual String searching for MUST use a foreach loop with nested if statement to search the entire foods collection one item at a time and ONLY print food items that contains the searchFood String regardless of caseHINT: Use String class toLowerCase or toUpperCase to help search regardless of case ie caseinsensitivity MUST use boolean local variable to track noMatch for searchFood and print NO food contains searchFood ONLY ifnoMatch foundHINT: Checkprint only once OUTSIDE of loop after search complete
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started