attribute vs parameter and setter vs add method

Hi,

In below code

class Passenger
{
   Flight m_Flight;
   public void setFlight(Flight flight)
   {
      m_Flight = flight;
   }
}

class Flight
{
   List<Passenger> m_Passengers = new ArrayList<>();
   public void addPassenger(Passenger passenger)
   {
      m_Passengers.add(passenger);
   }
}

Open in new window



Passenger passenger = new Passenger();
Flight flight = new Flight();

flight.addPassenger(passenger);
passenger.setFlight(flight);

Open in new window

what is differences and similarities between
attribute vs parameter and setter vs add method.

When to use set method and when to use add method. Please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
I have no idea what you are saying.

There are two pieces of code doing two different things.
adding a passenger and setting a flight are two different things.
zzynxSr. Software engineerCommented:
attribute vs parameter
It's not attribute vs. parameter, but attribute vs. member.

A Flight has Passengers as members. You add members to a list of members.
A passenger has a Flight as attribute. You set an attribute on an 'attribute owner' (being a Passenger here)

Remark: In real life I would expect the Passenger class to have a flightNumber attribute instead of the Flight object itself.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
A Flight has Passengers as members. You add members to a list of members.
A passenger has a Flight as attribute. You set an attribute on an 'attribute owner' (being a Passenger here)

can you please elaborate. I was not able to folow completely.

I did not understand difference between member vs attribute vs parameter now.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

zzynxSr. Software engineerCommented:
A flight is a list of passengers. A passenger is a member of the list/flight.
When adding a passenger to that flight, you use a method where the passenger is the parameter of the method.
A passenger belongs to a certain flight. A passenger has a flight(number) as an attribute.
If you set a passenger's flight(number) you pass the flight(number) as parameter to the setter method.
In fact by calling that setter, you set the flight(number) attribute of that passenger.
gudii9Author Commented:
A flight is a list of passengers.

based on the code you mentioned it?
or based on common knowledge?

I see

class Flight
{
   List<Passenger> m_Passengers = new ArrayList<>();
   public void addPassenger(Passenger passenger)
   {
      m_Passengers.add(passenger);
   }
}


Flight class has ArrayList of Passenger type,That is what you mean when you say

flight is a list of passengers

but flight could be more than list of passengers right.

Here only one additional method called

addPassenger

aprart from  list of passengers
gudii9Author Commented:
A passenger is a member of the list/flight.

List is Passenger ArrayList you are talking about right?

But flight is separate object right
gudii9Author Commented:
A flight is a list of passengers.

above you  mean like below??
A flight has a list of passengers.


A passenger is a member of the list/flight.
above you mean like below??
A passenger is a member of the list
or you mean like below ??
A passenger is a non static member instance variable  belonging to flight object
gudii9Author Commented:
When adding a passenger to that flight, you use a method where the passenger is the parameter of the method.

Do you mean Based on below code i thought we are adding passenger to ArrayList which in turn is under flight class. Is my understanding correct?

class Flight
{
   List<Passenger> m_Passengers = new ArrayList<>();
   public void addPassenger(Passenger passenger)
   {
      m_Passengers.add(passenger);
gudii9Author Commented:
zzynxSr. Software engineerCommented:
Which of the questions do you want me to answer? ;-)
gurpsbassiCommented:
I normally use the work 'attribute' to refer to instance variables of a class when drawing object models.
gudii9Author Commented:
Which of the questions do you want me to answer? ;-)
all of them if you can please
zzynxSr. Software engineerCommented:
based on the code you mentioned it?
or based on common knowledge?
Both

Flight class has ArrayList of Passenger type,That is what you mean when you say
flight is a list of passengers
Indeed.

but flight could be more than list of passengers right.
Correct

A flight is a list of passengers.
above you  mean like below??
A flight has a list of passengers.
Yes.

Do you mean Based on below code
Yes.

i thought we are adding passenger to ArrayList which in turn is under flight class. Is my understanding correct?
That's the internal details of the Flight class. But as a user of the Flight class you're not interested whether that's an ArrayList or something else. You just (want to) add a passenger to a flight
gurpsbassiCommented:
I think @zzynx has summed it up.
@gudii9 have you understood?
gudii9Author Commented:
A Flight has Passengers as members. You add members to a list of members.
A passenger has a Flight as attribute. You set an attribute on an 'attribute owner' (being a Passenger here)
why we have passenger as member in Flight whereas flight as attibute in Passenger.

Why cannot we have both as member or both as attribute
zzynxSr. Software engineerCommented:
Two reactions:
"Member" and "attribute" are just words, terminology.
In fact - as I already indicated - I would expect a passenger to have a flight number as atttribute.
Eg. A ball has attributes 'color' and 'diameter'. A passenger has the attribute 'flight number'. That attribute expresses what flight (s)he is/will be/was on.

Why cannot we have both as member
We can. I can think of a scenario where that would make sense: Flight could be a member of a Passenger's list of "Taken Flights Ever". (The list of "Taken Flights Ever" could be considered as an attribute of the Passenger)
gudii9Author Commented:
"Member" and "attribute" are just words, terminology.

do they mean same thing or different thing?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.