Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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
Avatar of Dante Gagliardi
Dante Gagliardi
Flag of United States of America image

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.
SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

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
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.
Avatar of gudii9

ASKER

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
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.
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.
Avatar of gudii9

ASKER

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
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.
Avatar of gudii9

ASKER

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?
Yes, attribute and property mean the same thing here.
Avatar of gudii9

ASKER

Member also same as attribure or property. Please advise
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

is method as below is also member?

getIfYouThoughtTheConstantWasVerboseHaHa()
please advise.

I have opened one other question to discuss about local variables
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.