0

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?

1 Answer 1

1

You should create the rowA and rowB variables as fields, outside of the constructor, but initialize them inside the constructor, once you have the parameter seat (which is actually the number of seats to judge from the other class).

public class PassWagon {
    private int wagonseats ;

    Passenger[] rowA; // Just create, don't initialize yet
    Passenger[] rowB;

    public PassWagon(int seat) {
        this.wagonseats = seat;
        rowA = new Passenger[wagonseats];
        rowB = new Passenger[wagonseats];

        for (int i = 0; i < wagonseats; i++) {
            rowA[i] = new Passenger();
            rowB[i] = new Passenger();
        }   
    }
}

Your problem was that when you were initializing them before the constructor, wagonseats has just been created so it contained 0. So you were creating an array with zero members. You can't change it after you create it, so you should only create it when you have the correct number.

By the way, if there are two rows, I suppose wagonseats should actually be seat/2, shouldn't it? Or at least, the size of the arrays and the limit of the loop should be seat/2.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.