java cyclic graphs

Hi,

I was reading below link
2.2.3. Cyclic graphs
Imagine we have the following simple class structure:
public class Flight {
  private String number;
  private List<Passenger> passengers;
   
  // getters and setters omitted
}

public class Passenger {
  private String name;
  private Flight flight;
   
  // getters and setters omitted
}
This is a cyclic graph: the Flight refers to the Passenger, which refers to the Flight again. Cyclic graphs like these are quite common in Java. If we took a naive approach to converting this to XML, we will end up with something like:
<flight number="KL1117">
  <passengers>
    <passenger>
      <name>Arjen Poutsma</name>
      <flight number="KL1117">
        <passengers>
          <passenger>
            <name>Arjen Poutsma</name>
            <flight number="KL1117">
              <passengers>
                <passenger>
                   <name>Arjen Poutsma</name>
                   ...

i wonder why java allows cyclic graph as above. What is the purpose or use of above cyclic graph and practical uses of it. 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.

Dante GagliardiDeveloperCommented:
Cyclicity is property of graphs, not of the Java language. The Java language could not prevent cyclic graphs even if it wanted to.

As to why cyclic graphs are important, it really depends on what you want to do.

For example, if you have a list of 5 values that you want to assign evenly to 20 objects, you could make a linked list of the 5 values and make it loop so that calling "next" on the last node of the list advances back to the first node.

Then, you take your list of 20 objects, and for each one assign to it the value of the current node in the linked list, then call "next" on the linked list.

Object 1 will have Value 1. Object 2 will have Value 2. Object 3 will have Value 3. Object 4 will have Value 4. Object 5 will have Value 5. The next object, Object 6, will have Value 1, because the call to "next" started the cycle over.

Cyclic graphs are also very important in number theory.
zzynxSr. Software engineerCommented:
>> What is the purpose or use of above cyclic graph and practical uses of it.
It looks quite handy to me that for a certain flight, you want to know all passengers, while given a passenger you want to know what flight (s)he belongs to.
I suppose you can see the practical use of that in the real world.
Same for customers and orders: given a customer, you want to know all order. Given an order, you want to know what customer ordered it.
gudii9Author Commented:
But how we refer one object like Passenger within other Object Flight. That looks bit complex to me esp. when you create instance of each object how the run-time know which exact Object is being created or referred. Any simple, detailed links on this usage. Please advise
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!

Dante GagliardiDeveloperCommented:
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


Then use these two classes like so:

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

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

Open in new window


Now, the Flight object has a reference to the Passenger object, and the Passenger object has a reference to the Flight object.
gudii9Author Commented:
flight.addPassenger(passenger);
passenger.setFlight(flight);

why setter is used with passenger where as add method is used with flight object(can we use setter with flight object as well)
Please advise
Dante GagliardiDeveloperCommented:
A flight can contain multiple passengers, and so we add a passenger to the flight.

A passenger can (hopefully!) only be on one flight at a time, so we set the flight on the passenger.

It is only a naming convention I use. You can feel free to modify the names of the methods as you please.
zzynxSr. Software engineerCommented:
why setter is used with passenger where as add method is used with flight object(can we use setter with flight object as well)
You really add a passenger to a flight. (A flight is a bunch of passengers by nature)
A passenger belongs to a flight. So that can be seen as an attribute of a passenger: the flight (s)he belongs to. For setting an attribute, we use a set method.
gudii9Author Commented:
flight.addPassenger(passenger);
passenger.setFlight(flight);

To me both passenger and flight seems to be attributes. I think that is same as parameter

What is attribute?
Do we normally use setters only with setters and getters not with methods like addPassenger(passenger);

May be i open new fresh question as this is different topic

Please advise
zzynxSr. Software engineerCommented:
To me both passenger and flight seems to be attributes
What's the existance of a flight without passengers?
A passenger is a member of a flight. A flight(number) is an attribute of a passenger.
A flight IS a list of passengers. It also has attributes like eg. a flight number.
gudii9Author Commented:
A flight(number) is an attribute of a passenger.
A flight IS a list of passengers. It also has attributes like eg. a flight number.


public class Variables {

    //Constant
    public final static String MyVariable = "that was a lot for a constant";

    //Value
    final String dontChangeMeBro = "my god that is still long for a val";

    //field
    protected String flipMe = "wee!!!";

    //Property
    private String ifYouThoughtTheConstantWasVerboseHaHa;

    //Still the propery
    public String getIfYouThoughtTheConstantWasVerboseHaHa() {
        return ifYouThoughtTheConstantWasVerboseHaHa;
    }
    //And now the setter
    public void setIfYouThoughtTheConstantWasVerboseHaHa(String ifYouThoughtTheConstantWasVerboseHaHa) {
        this.ifYouThoughtTheConstantWasVerboseHaHa = ifYouThoughtTheConstantWasVerboseHaHa;
    }

}

http://stackoverflow.com/questions/10115588/what-is-the-difference-between-field-variable-attribute-and-property-in-java
based on above link when you say attribute you mean property right?
Dante GagliardiDeveloperCommented:
Yes, attribute and property mean the same thing here.
gudii9Author Commented:
Member also same as attribure or property. Please advise
Dante GagliardiDeveloperCommented:
A Member Variable can be any of those things listed in the link you provided.

The term "Member Variable" is used to refer to any variable that is a "member" of the class, meaning it exists as long as the object exists. It is meant to contrast against a "local variable" which is a variable that is only defined in a method. At this point, however, we are moving on to another question. If your original question has been answered, please accept and answer for it and ask a new question about the difference between member and local variables.

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:
is method as below is also member?

getIfYouThoughtTheConstantWasVerboseHaHa()
please advise.

I have opened one other question to discuss about local variables
Dante GagliardiDeveloperCommented:
Technically, yes, a method is a member of a class. However, the word "member" is almost always used to refer to a "member variable". In this case, you have a method that returns the value of a member variable.

Again, please accept an answer to this question if your original question has been answered. By going off topic, it becomes harder for other people with similar questions to find the answer they need.
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.