Right now I have 3 classes. Train, wagon and passengers. In Train class
public class Train
{
private int seatNum = 30 + (int) (Math.random() * ((40 - 30) + 1)); //Random number of seats
private int wagonNum = 5 + (int) (Math.random() * ((10 - 5) + 1));//Random number of wagons
public Train()
{
PassWagon[] wagons = new PassWagon[wagonNum];//here I created an array with random wagon numbers.
for (int i = 0; i < wagonNum; i++)
{
wagons[i] = new PassWagon(seatNum); // I added PassWagon objects to that array. I need to access "seatnum" in the PassWagon class.
}
}
}
and
public class PassWagon
{
private int wagonseats ;
Passenger[] rowA = new Passenger[wagonseats]; //I created 2 "Passenger" arrays. Problem is, wagonseats variable has nothing. "seatNum" variable from other class gets initiated at Constructor.
Passenger[] rowB = new Passenger[wagonseats];
public PassWagon(int seat)
{
this.wagonseats = seat;
for (int i = 0; i < seats; i++)
{
rowA[i] = new Passenger(); //I get an out of bounds error here because this array has nothing in it.
rowB[i] = new Passenger();
}
}
I want to get the "seatNum" variable and use this to create rowA and rowB arrays. I tried couple of things:
I generated random number in PassWagon class, but this time every wagon object will have different number of seats. I want all wagons to have same amount seats.
I created rowA and rowB arrays inside the constructor, this time I couldn't accesed rowA and rowB arrays outside the constructor.
Does anybody have any ideas?