Solved

# Randomization

Posted on 2009-04-28
200 Views
Last Modified: 2013-11-23
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");

}

}

}

}

}
``````
0
Question by:Sdurrani
8 Comments

LVL 92

Assisted Solution

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

LVL 92

Assisted Solution

> t compiles successfully though so its a bit strange

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

0

LVL 17

Assisted Solution

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;

}

}
``````
0

LVL 17

Accepted Solution

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;

}

}
``````
0

Author Comment

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

LVL 92

Assisted Solution

>             if (refrigerator.length <= 10) {

should be:

if (top>0) {

0

Author Comment

Sorry I am confused do both lines need to be changed?
0

LVL 92

Assisted Solution

no, this line:

if (refrigerator.length <= 10) {

is incorrect, and should be replaced with:

if (top>0) {

0

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

### Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

#### 779 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!