Randomization

The following code needs to generate random items which are listed however - the following error is recieived whenver i have attempted to run the code. It compiles successfully though so its a bit strange
import java.util.Random;
 
class Kalamoo {
 
 
enum Food {EGGS, SUGAR, MEAT, ORANGE_JUICE};
 
 
private int top = 0;
 
 
Food[] refrigerator = new Food[10];
 
 
 
/* This is the method push. It represents pushing food into the
refrigerator. It takes in a Food item and returns true or false
on weather that item fits in the refrigerator. */
boolean push ( Food foodItem ) {
 
.
if (top > 10) {
return false;
}
else {
 
refrigerator [top] = foodItem;
 
// Step 6: Add a statement below to
// Increment the integer variable top by 1
 
for (int top = 0; top++;top =>1);
 
 
return true;
}
}
 
 
Food pop ( ) {
 
if (refrigerator.length <= 10){
 
 
top--;
return refrigerator[top];
}
else {
 
return null;
}
}
 
public static void main (String[] args) {
Kalamoo k = new Kalamoo ( );
 
for ( int i=0; i<100; i++ ) {
 
Random actionGenerator = new Random();
Random foodGenerator = new Random();
int action = actionGenerator.nextInt(2);
if ( action == 0 ) {
int foodBrought = foodGenerator.nextInt(8);
boolean foodFits = false;
if ( foodBrought == 0 ) {
foodFits = k.push ( Food.EGGS );
System.out.println ("Ship Brought Eggs");
}
else if ( foodBrought == 1 ) {
foodFits = k.push ( Food.ORANGE_JUICE );
System.out.println ("Ship Brought Orange Juice");
}
else if ( foodBrought == 2 ) {
foodFits = k.push ( Food.MEAT );
System.out.println ("Ship Brought Meat");
}
else if ( foodBrought == 4 ) {
foodFits = k.push ( Food.SUGAR );
System.out.println ("Ship Brought Sugar");
}
else {
 
}
if ((foodFits == true) && (foodBrought <= 4)) {
System.out.println ("Food added to regrigerator");
}
else if ((foodFits == false) && (foodBrought <= 4)) {
System.out.println ("Food does not fit, thrown away!");
}
else {
System.out.println ("No Action");
}
}
else {
Food item = k.pop ( );
if ( item == null ) {
System.out.println ("Fridge is Empty");
}
else if ( item == Food.EGGS ) {
System.out.println ("Eggs Eaten");
}
else if ( item == Food.ORANGE_JUICE ) {
System.out.println ("OJ Eaten");
}
else if ( item == Food.MEAT ) {
System.out.println ("Meat Eaten");
}
else if ( item == Food.SUGAR ) {
System.out.println ("Sugar Eaten");
}
}
}
}
}

Open in new window

SdurraniAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
try this:

      boolean push(Food foodItem) {

            if (top > 10) {
                  return false;
            } else {

                  refrigerator[top] = foodItem;

                  // Step 6: Add a statement below to
                  // Increment the integer variable top by 1

                  top++;

                  return true;
            }
      }


0
objectsCommented:
> t compiles successfully though so its a bit strange

it didn't compile here, needed to make the above changes


0
Thomas4019Commented:
First of all, there is no need to create two Random objects. So you dont need "actionGenerator" and "foodGenerator". You can use the same random object more than once.

Your code would be much simpler and the error would probably be clear if you change you code to use a method like the one i have attached.
public Food convertIntToFood(int i)
{
	switch(i)
	{
		case 0: return Food.EGGS;
		case 1: return Food.ORANGE_JUICE;
		case 2: return Food.MEAT;
		case 3: return Food.SUGAR;
	}
}

Open in new window

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Thomas4019Commented:
Oops, should look like,
public Food convertIntToFood(int i)
{
	switch(i)
	{
		case 0: return Food.EGGS;
		case 1: return Food.ORANGE_JUICE;
		case 2: return Food.MEAT;
		case 3: return Food.SUGAR;
		default: return null;
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SdurraniAuthor Commented:
Hi there
Nope it com[piled but i did  not Run i got the following error


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
        at Kalamoo.pop(Kalamoo.java:61)
        at Kalamoo.main(Kalamoo.java:113)
Press any key to continue . . .
0
objectsCommented:
>             if (refrigerator.length <= 10) {


should be:

            if (top>0) {

0
SdurraniAuthor Commented:
Sorry I am confused do both lines need to be changed?
0
objectsCommented:
no, this line:

    if (refrigerator.length <= 10) {


is incorrect, and should be replaced with:

            if (top>0) {

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.