Reputation: 107
Right so i have got this class called PlaneSeat,
the constructor in another class called PlaneSeat is
public PlaneSeat (int seat_id){
this.seatId = seat_id;
}
1) I wish to create 12 instances of this class with each PlaneSeat having a seatID of 1-12
Should i do this: (I dont know what this does)
private int PlaneSeat;
PlaneSeat [] seats = new PlaneSeat[12];
or (I dont know what this does either)
private int PlaneSeat[] = { 1,2,3,4,5,6,7,8,9,10,11,12};
Which one is better and what does what?
2) Also, if i have another class where the main is found and i wish to access the seat ID of each seat on the plane, how should i do it?
jet1.getSeatID // doesnt work where jet1 is a instance of a plane
Upvotes: 1
Views: 14710
Reputation: 1538
2) To access seatID, you need an accessor (normally called getSeatID()) in the PlaneSeat class.
public int getSeatID () {
return seatID;
}
1) private int PlaneSeat; PlaneSeat [] seats = new PlaneSeat[12]; You don't need to declare private int PlaneSeat, which doesn't actually make sense. Should be private PlaneSeat seat; or something... PlaneSeat[] seats = new PlaneSeat[12]; creates a new array of PlaneSeat objects with a size of 12.
private int PlaneSeat[] = { 1,2,3,4,5,6,7,8,9,10,11,12};
Again, this should be private PlaneSeat[] seats;
To create your seats, you would first declare your seat array
PlanetSeat[] seats = new PlaneSeat[12];
Then you can use a loop to fill the seats:
for (int i = 1; i <= 12; i++) {
seats[i-1] = new PlaneSeat(i);
}
Upvotes: 1