1

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
4
  • Java Arrays Commented Mar 9, 2014 at 17:34
  • None of your examples even compile. Start by reading the Java Tutorials to learn the basic syntax of Java. Commented Mar 9, 2014 at 17:37
  • To Keppil, well the big file has a lot more methods and 3 classes so i thought I'd just include the necessary code. Sorry Commented Mar 9, 2014 at 18:10
  • @user2999509 Please click on the grey arrow next to an answer to mark your question as answered if that is the case. That way, other people will see that the question no longer requires an answer. Commented Mar 9, 2014 at 20:25

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

PlaneSeat [] seats = new PlaneSeat[12]; for (int i = 1; i <= 12; i++) { seats[i-1] = new PlaneSeat(i); } - This causes a "illegal start of type" error, where do u suggest i put it? Thanks!
It should be put inside a method. Actually I never had an "illegal start of type" error in Java...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.