Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: I have made an array ( int [] foodArray=new int [10] ) in my Food1 cla

ID: 639329 • Letter: Q

Question

Question: I have made an array (int [] foodArray=new int [10] ) in my Food1 class and now i am trying to assign random values from other variables from another class to the index of that array but i am unsure how to do it. Please Please please help!

        Food1 foodtest = new Food[10];

        for (int i = 0; i < 10; i++){

            fx = rand.nextInt(5);

            fy = rand.nextInt(5);

            famount = rand.nextInt(8);

           

            foodtest.setXFoodLoc(fx);

            foodtest.setYFoodLoc(fy);

            foodtest.setFoodAmount(famount);

          foodArray[i] = new Food(fx, fy, famount);

           

        }

        System.out.println(foodtest.PrintFood());

Explanation / Answer

//Create an array of Food clas to store the Food objects of size 10
Food[] foodtest = new Food[10];

//Note :It cannot be to store the Food objects into the array
//which is not of the Food class type.


for (int i = 0; i < 10; i++)
{
        fx = rand.nextInt(5);
        fy = rand.nextInt(5);
        famount = rand.nextInt(8);
         
        foodtest.setXFoodLoc(fx);
        foodtest.setYFoodLoc(fy);
        foodtest.setFoodAmount(famount);
      
        //Create a Food class object with fx,fy and famount
        Food foodObject = new Food(fx, fy, famount);
      
        //Now assign foodObject to the foodtest array at location ,i
        foodtest[i]=foodObject;

         
}
//Create a method in the Food class with a name PrintFood
//that displays the all the objects in the Fodd class
System.out.println(foodtest.PrintFood());


//Now we have an array of 10 food objects in the foodtest ,
//if we want to print the objects , then there
//are two possiblities.
// Create a method in the Food class with PrintFood
//like this and pass the array object , foodtest.

//Mehtod1:
class Food
{
   //class code
  
   public void printFood(Food foodtest[])
   {
       //code to print the foodtest objects
   }
  
}

/*Create a another class and write a method PrintFood and pass the Food array object and print the foodtest
object. */

//Mehtod2:
class FoodTester
{
   Food[] foodtest;
  
   FoodTester(Food foodarray[])
   {
       //code to assign the foodtest array
       foodtest=foodarray;
   }
  
   //class code  
   public void printFood()
   {
       //code to print the foodtest objects
       for(int i=0;i<foodtest.length;i++)
           System.out.println(foodtest[i]);
   }
  
}

//Call the method printFood using FoodTester class object

FoodTester foodtester=new FoodTester(Food foodtest[]);
//call the method printFodd
foodtester.PrintFood();

//Hope this helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote