Urgent help required

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • koonda
    New Member
    • Oct 2006
    • 34

    #1

    Urgent help required

    Hi all,

    I have an assignment due next week. I don't realy know simple two way association which this program requires. Here is the requirement.


    This exercise is about simple two-way associations. An example of a simple two-way association is the ownership relation between a Person and a Car. To ensure consistency, the following three rules should be obeyed.
    Each car is owned by exactly one person, that is, no car is without an owner, and no car is owned by more than one person.
    Each Person can own at most one car.
    If a person owns a car, that car is owned by that person, and vice versa.


    It is common for simple two-way associations that one side X must be associated to the other side Y, while Y might be associated to an X.

    Write the two classes Person and Car, such that each has a public (declaring it private will make the exercise much harder) reference field that can refer to one of the other kind, that is, class Person as a field Car car, and class Car has a field Person owner. Hint. Use the constructor of Car to make sure that a car is never created without a owner, this should take care of rule 1 & 2 above.
    Add a method void newOwner(Person p) to the car class. It should make p the new owner of this car. Make sure rule 1, 2 & 3 are respected by the method. You may assume that the person p is not the owner of an other car.


    I have no clue how to implement this. Could you guys give me some Idea how to design these two classes according to the requirements.

    I appreciate your help.

    Looking forward to your reply.

    Koonda
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by koonda
    Hi all,

    I have an assignment due next week. I don't realy know simple two way association which this program requires. Here is the requirement.


    This exercise is about simple two-way associations. An example of a simple two-way association is the ownership relation between a Person and a Car. To ensure consistency, the following three rules should be obeyed.
    Each car is owned by exactly one person, that is, no car is without an owner, and no car is owned by more than one person.
    Each Person can own at most one car.
    If a person owns a car, that car is owned by that person, and vice versa.


    It is common for simple two-way associations that one side X must be associated to the other side Y, while Y might be associated to an X.

    Write the two classes Person and Car, such that each has a public (declaring it private will make the exercise much harder) reference field that can refer to one of the other kind, that is, class Person as a field Car car, and class Car has a field Person owner. Hint. Use the constructor of Car to make sure that a car is never created without a owner, this should take care of rule 1 & 2 above.
    Add a method void newOwner(Person p) to the car class. It should make p the new owner of this car. Make sure rule 1, 2 & 3 are respected by the method. You may assume that the person p is not the owner of an other car.


    I have no clue how to implement this. Could you guys give me some Idea how to design these two classes according to the requirements.

    I appreciate your help.

    Looking forward to your reply.

    Koonda
    I would recommend designing a UML representation of the two classes. That will give you a good base and reference guide before you sit down and program them.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by koonda
      Hi all,

      I have an assignment due next week. I don't realy know simple two way association which this program requires. Here is the requirement.


      This exercise is about simple two-way associations. An example of a simple two-way association is the ownership relation between a Person and a Car. To ensure consistency, the following three rules should be obeyed.
      Each car is owned by exactly one person, that is, no car is without an owner, and no car is owned by more than one person.
      Each Person can own at most one car.
      If a person owns a car, that car is owned by that person, and vice versa.


      It is common for simple two-way associations that one side X must be associated to the other side Y, while Y might be associated to an X.

      Write the two classes Person and Car, such that each has a public (declaring it private will make the exercise much harder) reference field that can refer to one of the other kind, that is, class Person as a field Car car, and class Car has a field Person owner. Hint. Use the constructor of Car to make sure that a car is never created without a owner, this should take care of rule 1 & 2 above.
      Add a method void newOwner(Person p) to the car class. It should make p the new owner of this car. Make sure rule 1, 2 & 3 are respected by the method. You may assume that the person p is not the owner of an other car.


      I have no clue how to implement this. Could you guys give me some Idea how to design these two classes according to the requirements.

      I appreciate your help.

      Looking forward to your reply.

      Koonda
      But you haven't done anything yet. What do you think should be done? The rules here require you to have done some work for you to get any help on your assignment.

      Comment

      • koonda
        New Member
        • Oct 2006
        • 34

        #4
        Here is the code but I don't know how to implement it further.

        public class Person
        {

        public Car car;

        //constructor

        public Person()
        {

        }

        } //end class


        public class Car
        {
        piblic Person owner;

        //constructor

        public Car()
        {

        owner = new Person()
        }

        public void newOwner(Person p)
        {

        p = owner;

        }

        } //end car

        }


        I don't know how to implement it further. Looking forward to your reply

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by koonda
          Here is the code but I don't know how to implement it further.

          public class Person
          {

          public Car car;

          //constructor

          public Person()
          {

          }

          } //end class


          public class Car
          {
          piblic Person owner;

          //constructor

          public Car()
          {

          owner = new Person()
          }

          public void newOwner(Person p)
          {

          p = owner;

          }

          } //end car

          }


          I don't know how to implement it further. Looking forward to your reply
          If you didn't understand the assignment well, maybe you should go back to your teacher and ask about it more.

          Comment

          • koonda
            New Member
            • Oct 2006
            • 34

            #6
            Thanks a lot anyways, It is working a little bit and I will try to Implement it soon.

            I still have some question on some other Java Assignments and I hope you will be able to help me.

            Thanks a lot.

            Koonda

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by koonda
              Thanks a lot anyways, It is working a little bit and I will try to Implement it soon.

              I still have some question on some other Java Assignments and I hope you will be able to help me.

              Thanks a lot.

              Koonda
              We are willing to help with assignments if you are willing to do most of the work yourself. If you don't know how to begin, then we simply refer you back to the teacher.

              Comment

              Working...