Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write and submit the source code for the following program. The program will use a String array of size 6 to store a small
Write and submit the source code for the following program. The program will use a String array of size 6 to store a small grocery list. The program will read in a list of six grocery items from the user and store them in the array. The program will then sort the array of grocery items and print the sorted array. The program must not permit duplicate items. If the user attempts to enter a duplicate item, the program will warn the user and will not add the item to the array. This lab is part of a unit about Java loops and arrays. Although the program could be written using Java Collections (you'll learn about them later in the course) the student is expected to use loops and arrays to write this program. Using Java Collections classes and methods (other than the Arrays.sort() method) will result in a grade of zero. Assignment Your program should begin with the following code: package edu.cscc; import java.util.Arrays; import java.util.Scanner; // Student name, date, and program purpose public class Main { private static Scanner input = new Scanner (System.in); public static void main(String[] args) { int count = 0; // Number of items currently in the grocery list 1) Declare and instantiate a String array named groceryList with six elements. 2) Write a while loop to read in the grocery list from the user and add each non-duplicate item to the array. Use the variable count to keep track of the number of items added to the array. As you read in each item test the item to see if it is a duplicate item. If it is a duplicate output an error message to the user. If it is not a duplicate, add the item to the array and increment the variable count. 3) After the while loop has completed, sort the array groceryList using the following statement: Arrays.sort (groceryList); 4) Next, use a for-each loop to print the sorted grocery list. To test whether an item is a duplicate you will create the following method: public static boolean is Duplicate (String item, String[] list, int listcnt) { The isDuplicate method has three parameters: a String parameter item to check to see it it's a duplicate, a String array parameter list to check it against, and an integer parameter listcnt which contains the number of items currently in list (for example the array size might be six, but the array currently may only contain three items, so listent would be three). Implement this method and have it return true if item is found in the list and false otherwise. Use a for loop in your implementation. You will then call this method from your main method in order to check for duplicate grocery items. As always use the package name edu.cscc and include a comment with your name, the date, and the purpose of the program. Sample Output Enter grocery item: apple Enter grocery item: peach Enter grocery item: apple Sorry, item: apple is a duplicate Enter grocery item: lettuce Enter grocery item: lettuce Sorry, item: lettuce is a duplicate Enter grocery item: coca cola Enter grocery item: cheese Enter grocery item: cookies Your Grocery List apple cheese coca cola cookies lettuce peach Process finished with exit code 0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
package educscc import javautilArrays import javautilScanner Your name date ...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