?
Solved

Randomization

Posted on 2009-04-28
8
Medium Priority
?
206 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");
}
}
}
}
}

Open in new window

0
Comment
Question by:Sdurrani
  • 4
  • 2
  • 2
8 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 24257092
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

by:objects
objects earned 1000 total points
ID: 24257099
> 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

by:Thomas4019
Thomas4019 earned 1000 total points
ID: 24257106
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
Thomas4019 earned 1000 total points
ID: 24257109
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
 

Author Comment

by:Sdurrani
ID: 24257117
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

by:objects
objects earned 1000 total points
ID: 24257127
>             if (refrigerator.length <= 10) {


should be:

            if (top>0) {

0
 

Author Comment

by:Sdurrani
ID: 24260880
Sorry I am confused do both lines need to be changed?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 24265328
no, this line:

    if (refrigerator.length <= 10) {


is incorrect, and should be replaced with:

            if (top>0) {

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

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

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

Join & Ask a Question