Answered step by step
Verified Expert Solution
Link Copied!

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 1 statement in the body to return the number of items in the foods ArrayList collection Add protective mutator boolean addFood(String foodName) to add a valid foodName to foods AND return result of successful add or not MUST parameter check foodName with if-else to be valid (non-null)o if valid - print ADDED food with actual food value, then use ONLY 1 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 addFood(int 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 (non-null) 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 index/food Add protective mutator void addFoodFirst(String foodName) to add a valid foodName at the beginning of foods as the very FIRST element MUST parameter check foodName with if-else to be valid (non-null)o 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 removeFood(int index) to remove item at index from foods MUST parameter check index (lower & upper bounds) with if-elseo if valid - use ONLY 1 statement to remove item at index AND print REMOVED food from index index with actual food/index 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 removeFood(String 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 if-else to be valid (non-null)o if valid - print REMOVED food with actual food value, then use ONLY 1 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 listFood(int index) to print ONLY the food item specified MUST parameter check index (lower & upper bounds) with if-elseo if valid - use ONLY 1 statement to retrieve the food at the specific index AND print food at index index with actual food/index 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 if-else & isEmpty()o if empty - print the message NO food items to listo else -(only if at least 1 item) print the heading Food items: on its own line, then MUST use a for-each loop to retrieve each item in sequence from the foods collection and print on its own line Add method void listFoodMatching(String 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 intuitively/inherently makes sense at that time(i.e., 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 for-each 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 case)HINT: Use String class .toLowerCase() or .toUpperCase() to help search regardless of case (i.e. case-insensitivity) MUST use boolean local variable to track noMatch for searchFood and print NO food contains searchFood ONLY if(noMatch) foundHINT: Check/print only once OUTSIDE of loop after search complete

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions

Question

90 Basic budgeting and accounting concepts.

Answered: 1 week ago

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago