timestopMachinist
timestopMachinist

Reputation: 5

Initialize an array of multiple class objects in java?

I need to use a constructor to initialize an array of class objects, but I can't figure out the syntax to make it work. I need to include one String and five ints. Here are some neccessary details from my code.

public Event(String n, int g)
   {
      eventNumber=n;
      guests=g;
   }

From "Event.java"

public DinnerEvent(String number, int guests, int entree, int sideOne, int sideTwo, int dessert)
   {
      super(number, guests);
      entreeID=entree;
      sideOneID=sideOne;
      sideTwoID=sideTwo;
      dessertID=dessert;
   }

From "DinnerEvent.java", which extends Event.java.

for(int x=0; x<EVENTS_QUANTITY; ++x)
      {
         String n;
         int g, e, s1, s2, d;
         Scanner input=new Scanner(System.in);
         System.out.print("Enter event number, number of guests, and ID of entree, two sides, and a dessert.");
         nums[x]=input.nextLine();
         ints[x][0]=input.nextInt();
         ints[x][1]=input.nextInt();
         ints[x][2]=input.nextInt();
         ints[x][3]=input.nextInt();
         ints[x][4]=input.nextInt();
      }
      
      DinnerEvent[][] events={ {nums[0], ints[0][0], ints[0][1], ints[0][2], ints[0][3], ints[0][4]},
                           {nums[1], ints[1][0], ints[1][1], ints[1][2], ints[1][3], ints[1][4]},
                           {nums[2], ints[2][0], ints[2][1], ints[2][2], ints[2][3], ints[2][4]},
                           {nums[3], ints[3][0], ints[3][1], ints[3][2], ints[3][3], ints[3][4]}};

From "DinnerEventDemo.java". Code here is very messy because I was just trying different things to get it to work. So far, no luck. Instructions specifically request this be done using an array of DinnerEvent objects and through using the constructor in DinnerEvent.java. I can send more surrounding code if necessary. If a variable is not declared in these excerpts, it's declared beforehand.
This is my first time posting here, so I apologize if my formatting is off or I don't follow the typical posting etiquette on this site. Help is appreciated.

Upvotes: 0

Views: 1368

Answers (3)

user1438038
user1438038

Reputation: 6079

To create a new dinner event, call the constructor and pass your desired arguments:

DinnerEvent myDinner = new DinnerEvent("One", 5, 3, 4, 5, 6);

The variable myDinner now holds an instance of your DinnerEvent class. I passed some random arguments, because I don't know the exact semantics of your code. I assume that you used integer numbers for most of your arguments, because they correspond to an entry of a menu.

If you want to create multiple instances of DinnerEvent, you need to define multiple variables:

DinnerEvent myFirstDinner = new DinnerEvent("One", 5, 3, 4, 5, 6);
DinnerEvent mySecondDinner = new DinnerEvent("Two", 2, 1, 2, 3, 7);

Furthermore, you can create a collection of DinnerEvent instances, e.g. as an array. Now, however, you need a loop to iterate over each item in that array to initialize the corresponding instance invdividually:

DinnerEvent[] dinners = new DinnerEvent[4]; // Array of four instances

// Initialize array elements in a loop
for (int i = 0; i < dinners.length; ++i) {
  dinners[i] = new DinnerEvent(i + ". Dinner", 1, 2, 3, 4, 5);
}

You will end up with an array of four DinnerEvent instances. We used a loop for initialization, but you could as well initialize them individually. This even might make more sense, because it is very likely that you want to set meaningful arguments:

DinnerEvent[] dinners = new DinnerEvent[2]; // Array of two instances

// Initialize instances individually
dinners[0] = new DinnerEvent("First Dinner", 2, 2, 3, 4, 5);
dinners[1] = new DinnerEvent("Second Dinner", 7, 8, 3, 41, 45);

Now you will end up with an array of two elements, both having individual properties. You call the constructor twice and pass in your desired arguments.

Upvotes: 2

vsfDawg
vsfDawg

Reputation: 1527

You don't have to collect all of the data before you start building the first event.

Scanner input=new Scanner(System.in);
DinnerEvent[] dinnerEvents = new DinnerEvent[EVENTS_QUANTITY];
for(int index=0; index<dinnerEvents.length; ++index) {
  System.out.print("Enter event number, number of guests, and ID of entree, two sides, and a dessert.");
  dinnerEvents[x] = new DinnerEvent(
       input.next(),
       input.nextInt(),
       input.nextInt(),
       input.nextInt(),
       input.nextInt(),
       input.nextInt());
}

Upvotes: 0

code_mechanic
code_mechanic

Reputation: 1148

Assuming the scanner values as the parameters of constructor, you can do something like this

DinnerEvent[] events = new DinnerEvent[EVENTS_QUANTITY];
for(int x=0; x<EVENTS_QUANTITY; ++x)
      {
         String n;
         int g, e, s1, s2, d;
         Scanner input=new Scanner(System.in);
         System.out.print("Enter event number, number of guests, and ID of entree, two sides, and a dessert.");
         n=input.nextLine();
         g=input.nextInt();
         e=input.nextInt();
         s1=input.nextInt();
         s2=input.nextInt();
         d=input.nextInt();

         events[x] = new DinnerEvent(n, g, e, s1, s2, d);
      }

Upvotes: 0

Related Questions