Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA LANGUAGE 1. In Eclipse, create a program in your submit project called LetsGoshopping Ask for a main() to be added to your starter code.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedJAVA LANGUAGE

1. In Eclipse, create a program in your submit project called LetsGoshopping Ask for a main() to be added to your starter code. 2. When your starter code appears in the edit window, add "throws Exception" to the end of the main() method declaration line 3. Then, as usual, print welcoming messages on the console that explain what our program will do and what will be expected of the user. Here is a sample. System.out.println("2023"); System.out.println("Welcome to the Let's Go Shopping program!"); System.out.println("This program maintains a shopping list on the disk and "); system.out.println("let's you add items to the list (typically before you go shopping)"); System.out.println("or remove items from the list (typically as you are shopping)."); System.out.println("To remove an item you simply enter the item's number shown on the list,"); System.out.println("To remove an item you simply enter the item's number shown on the list,"); System.out.println("The list is sorted as you add so you can more easily group and find items."); System.out.println("(Be aware that the sorting will change an item's 'item number' in the list.)"); System.out.println("(Be aware that the sorting will change an item's 'item number' in the list.)"); 4. TEST! Do you see your welcome messages? The Eclipse console should show "terminated" in the upp left corner since that's all we have in our program so far. string[] shoppinglist; 6. See if a shopping list already exists on disk. If so, read it in and print it for our user to see. If we can't find a shopping list on the disk, make an empty array to use (this is probably the first execution of this program). You will have to help the compiler find the FileInputStream and ObjectInputStream. (push the yellow lightbulb in the left margin and select the appropriate inport statement). Note that we will use 2 l/O classes (passing the address of the first one "fis" to the second one) and that we will be reading the array object (with all it's fields) from the disk. to the FilelnputStream where we declared it above. The .ser file type is short for a "serialized"Java object.) program will be terminated with a ClassCastException. This keeps us from trying to use an Elephant as a shoppingList. directory.) Arrays.fill(shoppingtist, "zzz"); // all slots will be initially set to indicate it is empty/available using above to initialize all the slots with a String "zzz". 8. If an existing shopping list was found on the disk, print it on the console so the user can see what is already in/on the list. // show to user System.out.println("Here are the items in the current shopping list:"); for (int i=0;i i++ ) if (!shoppinglist[i].equals("zzz")) // an empty slot the "item number". The user will enter this number when they are shopping to delete this item from the shopping list once they have found it. BUT WAIT JUST A MINUTE! (branch/jump to it and then return) whenever we need to print-the-list in our main() method code. 9. Create a separate method to do the print-the-list function. Here's the method: System.out.print1n("Here are the itens in the current shopping list:"); printShoppinglist(shoppingList); // "call" (branch to) the printShoppinglist() method, passing it the // address of the array (the shoppinglist) we want printed. 0. TEST! there are red errors in it.) a very good likelyhood that the problem is in the code you just added. We have added more than a few lines to discover that there is or is not a shopping list on the disk. So run your program again now. We have done this in the Percentage and Rainfall programs, so it may be getting familiar done and uses the break; statement to exit the loop!) At the very top of the new loop we just added, prompt and accept entry from the user: System. out.printl1n("Enter an item \# to delete it, or a description to add a new iten, or ENo."); String itemvumberorName = scanner.nextLine().trim(); // read keyboard, droping leading/trailing bla string itemWumberorName = scanner.nextLine().trim(); // read keyboard, droping leading/trailing blanks if (itemNumberorName.equalsIgnoreCase("ENo")) break; // exit the while loop (then the main() method, then the program) trailing (but not imbedded) blanks so if they enter " END" or "end " or " End " our compare for END on the next line will work. int itemvumber = Integer.parseInt(itemNumberorName); appropriate action in the try (found it) and catch (didn't find it) blocks. try \{ int itemNumber = Integer.parseInt(itemNumberorName); // will throw NumberformatException if not whole numeric \# // ***** DELETE ITEM PRocESSING **** shoppingList[itenNumber] = "zzz"; // mark this slot as empty/available i/ "** ADD-A-NEW-ITEM processing *** This try/catch structure (in a loop) is the core code of our program! 5. Let's implement the catch block code next so we can run a test to see items we add to the shopping list. (Then we will have some items in the list and can test our delete function as well.) for (int i=0; i i++ ) if (shoppingtist[i].equals("zzz")) // found an empty slot shoppinglist [i]= itemNumberorName; break; // exit out bottom of local for loop 3 // end of for loop Start by declaring a boolean variable called foundAnEmptyslot prior to entering the for loop and setting it's initial value to false. (boolean variables can have only the value true or Then, if you find an empty entry in the list, set the value of the boolean variable to true (right before the break; statement in the for loop). while(true) loop. Perfect! That more sophisticated looping looks like this: We can simply call the sort method of the Arrays class to sort the shoppinglist array: Arrays.parallelSort(shoppinglist); // "zzz"s to the bottom! Then we can call our print-the-list subroutine to show the updated list to the user: printShoppingList(shoppingList); 7. Then we better save the updated shoppingList on the disk in case our program crashes or the computer crashes or the battery runs out or... Here's the method to add: Then, in the main() method, right after you call the printShoppingList() method in the catch block, add to call the "save" subroutine as the last statement in the catch block. 8. TEST ADDING ITEMS Finally we can test some of this code! Run the program and enter Strings like "apples" and "milk" etc. (no numbers to get into the DELETE function yet). Is the list reprinted with each entry? Is the "item number" printed for each item in the list? (The item number will change as the sorting changes the order of the items in the list.) 9. Complete the DELETE function code. array, the number could be negative, or the item with the entered index/offset item number could already have been deleted. So we need to add some additional tests. Add this code to the try block AFTER the DELETE PROCESSING comment. if (itemNumber >= shoppinglist.length) \{ System.out.println("Item number is off the end of the list!"); continue; \} if (itemNumber

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

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions