I'm having a problem in Java, and I'm not even sure how to begin coding it. Basically, I have an arraylist loaded with three types of items, which are subclasses of a superclass type. Each object, regardless of type, has inherited a time attribute from the superclass. I need to have a user enter in a time limit, say 50 minutes, and have my program run through the arraylist AT RANDOM, and without reordering the actual objects in the arraylist essentially "pluck" an object from the arraylist. Then the program needs to get the object's time and compare it to the user entered time. If the object time is less than the user time, print the object. Move on to another object in a random order and "pluck" that object, do the same check. Once the total time matches the user time entered, terminate.
I had the idea to use a new Random int, but I'm not sure how to accomplish what I need to. The pseudocode would look like:
for (random number;random number<arraylist.size();change random number so it grabs a different object)
if (object at random position's time attribute is less than or equal to the user-entered time)
print the object...
I have never had to use Random for anything, sorry for sounding like a noob. Any ideas you can provide will help me a lot, thank you!
Why can't you re-order the objects? It sounds like you don't want to sort the objects, but maybe shuffle them. And if you can't re-order the original list, how about re-ordering a copy of the list?
A couple of other questions: When you 'pluck' an element out of the list, are you just getting a reference to it, or are you removing it entirely from the list? What happens to the 'plucked' object when it is over the user's estimate? Is it put back in the list? Where at? Are objects in the list allowed to be 'plucked' twice?
Joined: Feb 02, 2013
I was thinking against shuffle, because I want the program to print the arraylist in the original order, and if I use shuffle to accomplish my task and then print, it would print in the new shuffled order.
I suppose just pass a reference? If each object's time is less than the user time AND an ongoing variable which adds each object time until the user-entered total time is reached, print the object, and move on until the variable concatenating the object times is equal to or close to the user time. Each time it grabs an object to check, it has to be randomly found.
After it checks each object it just moves on, doesn't delete them or anything. Its just printing a random list for the user from a pre-set arraylist order. These are fake video files.
Ex. "generate a random list of movies" in the menu. User agrees. User is promoted: "how long do you want to watch for?"
User enters 46:36 (46 mins, 36 seconds).
(Randomly selected movie files, totaling 46:36 are printed to screen).
Joined: Feb 02, 2013
Sorry, I should elaborate. Although a random list of files must be printed, another menu option is just to print the list in original order, hence the reason I don't want to use shuffle. Program needs to be able to print both ways.
You have already been told how to shuffle: take a copy of the List. You can now have two copies, one shuffled and one in its original order.
Choosing films randomly to fill 46min36sec is a very difficult task, particularly if you fill 46min35sec and still have a bit of time left over.