Question: Background Write and submit the source code for the following program. The program will use a String array of size 6 to store a small
Background
Write and submit the source code for the following program. The program will use a String array of size 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.
nis lab is part of a unit about Java loops and arrays. Although the program could be written using Java
Collections youll 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:
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 mainString args
int count ;
Declare and instantiate a String array named groceryList with six elements.
Write a while loop to read in the grocery list from the user and add each nonduplicate 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 dunlicate item. If it is a dunlicate outnut an error message to the user. If it is not a duplicate, add the item to
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.
After the while loop has completed, sort the array groceryLi st using the following statement:
Arrays. sortgroceryList;
use a foreach loop to print the sorted grocery list.
To test whether an item is a duplicate you will create the following method:
public static boolean isDuplicateString item, String list, int listent
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 istent 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 only
ONE return statement in this method. Use a for loop in your implementation. You will then call this method from your
main method to check for duplicate grocery items.
As always 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
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
