Question
Java: A grocery item has an item code and a shelf life. The shelf life is measured in days, and can range from 1 day
Java:
A grocery item has an item code and a shelf life. The shelf life is measured in days, and can range from 1 day to 30 days. Both data elements are integers. Write a program that creates 50 GroceryItem objects. The item code for each object can be generated sequentially, and the shelf life should be generated randomly.
Using ArrayList objects, the program should put those items with a shelf life greater than 7 days into one ArrayList object and those with a shelf life of 7 days or less into another ArrayList object.
The program should display the number of items with a shelf life greater than seven days, the number of items with a shelf life of 7 days or less, and display each list of items.
For this exercise, you can use the java.util.Random class to generate random numbers, as follows:
Random rnGenerator = new Random( rnSeed ); // creates a Random object
shelfLife = rnGenerator.nextInt( 30 ) + 1; // next int in range 0 to 30
The first statement above instantiates a Random object called rnGenerator. The constructor argument rnSeed is called a random number seed. It is of type long, and is used to help ensure that the same sequence of random numbers is generated each time your program is run. The nextInt() method returns a random integer in the range 0 to 29.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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