Write and submit the source code for the following program. The program will use a String...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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 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
Expert Answer:
Answer rating: 100% (QA)
package educscc import javautilArrays import javautilScanner Your name date ... View the full answer
Related Book For
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch
Posted Date:
Students also viewed these programming questions
-
The second programming project involves writing a program that examines a file of polynomials and determines whether the polynomials in that file are in strictly ascending order using two different...
-
Let A, B be sets. Define: (a) the Cartesian product (A B) (b) the set of relations R between A and B (c) the identity relation A on the set A [3 marks] Suppose S, T are relations between A and B, and...
-
The objective of this problem is to design and develop a program for Huffman coding algorithm. The discrete source has an alphabet X = {x1, x2, x3, x4, x5, x6, x7, x8, x9} with corresponding...
-
Tibble Corporation recently determined that the recoverable value of one of its milling machines is less than its current carrying amount. In addition, the machine's useful life is now expected to be...
-
Solve the quadratic equation. Check your answers. x +13=0
-
In a 1975 contract, Eureka was given the exclusive right to sell spring water and other products under the Ozarka trade name in 60 Oklahoma counties in exchange for \($9,000\) paid to Arrowhead,...
-
Based on the following graph (which summarizes the demand, marginal revenue, and relevant costs for your product), determine your firm??s optimal price, output, and the resulting profits for each of...
-
Explain entity integrity and referential integrity rules in relational model. Show how these are realized in SQL.
-
1. Forward on Asset Paying Discrete Dividend Consider a 9-month forward contract on a corporate bond. The current price of the corporate bond is $900, and it will pay $40 coupon in 4 months. The...
-
Two point masses are the same distance R from an axis of rotation and have moments of inertia IA and IB HINT (a) If I 161A, what is the ratio MB MA of the two masses? MB mp (b) In terms of R, at what...
-
Include a definition of ethics and explanation of how ethics compares to law. Provide an explanation of corporate social responsibility and how it relates to ethical business practices.?
-
Is it possible to convert the string below into a double so that the code below does the exact same thing but without using DecimalFormat? private void...
-
A bond with a face value of $1000 has a coupon rate of 7%, annual coupon payments, yield to maturity of 10%, and 20 years to maturity. calculate this bond duration.
-
Create a Java file named ArrayUtils.java and follow the steps below: a. (3 points) Define a method, named randomArray with two parameters: 1) an integer indicating the size of the array to be created...
-
Let R be the region in the first quadrant bounded by the graphs of y = x and y -axis is a square. What is the volume of the solid? 4x, as shown in the figure above. The region R is the base of a...
-
Suppose the market is semistrong form efficient. Can you expect to earn excess returns if you make trades based on? a. Your brokers information about record earnings for a stock? b. Rumors about a...
-
Practice Program 2 used two arrays to implement a simple phone book. A more scalable solution is to make an array of an object that stores the name and corresponding phone number. Modify the program...
-
Write an application that implements a trip-time calculator. Define and use a class TripComputer to compute the time of a trip. TripComputer should have the private attributes totalTimethe total time...
-
Write a program that creates two instances of the generic class LinkedList given in Listing 12.12. The first instance is stadiumNames and will hold items of type String. The second instance is...
-
What is a business strategy? Do you agree with the definition proposed? Illustrate your answer with examples.
-
Consider one of the following firms. Read the description of a business strategy in the text. Go to the firm's website and use it to gain an understanding of the business strategy. Look at elements...
-
Which quote at the front of the chapter do you find the most insightful? Why? Under what circumstances would its implications not hold?
Study smarter with the SolutionInn App