14 Replies - 407 Views - Last Post: 09 June 2020 - 10:05 AM Rate Topic: -----

#1 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 124
  • Joined: 28-November 13

Parents and childs

Posted 09 June 2020 - 06:30 AM

A parent can be a child
He / she has 0 .. * children
A child has two parents (first instance none)
The user of the program must be able to manage a list of people, considered as a collection of parents and / or children, by applying the functionalities {addPerson, removePerson, findByName, findFamilyByName, getPerson}

can any one guide me through this ? ps: i'm not asking for you to solve it and i know you don't do it here .
what i know so far is i should have 3 classes a class Person and two classes Child and Parent that extends Person
i'm confused where to put the functions addPerson removePerson ...
as supposed addPerson should add a person child or Parent to an arraylist of Persons .
The arrayList of Persons here should be a variable in my class Person ?

Is This A Good Question/Topic? 0
  • +

Replies To: Parents and childs

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: Parents and childs

Posted 09 June 2020 - 07:05 AM

Not sure where add and remove of a Person works with parents and childern. Parents would have addChild but Childern would not have addParent except for the case of adoption or perhaps divorce.
Clubs and teams could have add and remove person methods.
Was This Post Helpful? 0
  • +
  • -

#3 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 124
  • Joined: 28-November 13

Re: Parents and childs

Posted 09 June 2020 - 07:26 AM

View PostNormR, on 09 June 2020 - 07:05 AM, said:

Not sure where add and remove of a Person works with parents and childern.

well as said a user should manage multiple people as i understand it he must create person and add them to an arraylist but what got me confused is why do i need an addperson function when i can just use the arraylist add function to add a person to the arraylist.

View PostNormR, on 09 June 2020 - 07:05 AM, said:

Parents would have addChild but Childern would not have addParent except for the case of adoption or perhaps divorce.

addChild should add a child to an arraylist of childs that is a variable in the parent class right ?

ok so how can i control that everychild would have 2 parents if i dopn't make an addParent fucntion in the child class
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: Parents and childs

Posted 09 June 2020 - 07:34 AM

Quote

how can i control

It is up to you to decide what the code is going to model. Can a child's parents be changed? How?
Can they only be set in the child class's constructor?

This post has been edited by NormR: 09 June 2020 - 07:36 AM

Was This Post Helpful? 0
  • +
  • -

#5 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 124
  • Joined: 28-November 13

Re: Parents and childs

Posted 09 June 2020 - 07:38 AM

View PostNormR, on 09 June 2020 - 07:34 AM, said:

Quote

how can i control
Can a child's parents be changed? How?
Can they only be set in the child class's constructor?


well probably no a child parent couldnt be changed . can they only be set in the constructor well as said in the question A child has two parents (first instance none) first instance none i guess he means that when first created a child have no parents you then have to add them later
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: Parents and childs

Posted 09 June 2020 - 07:49 AM

Almost by definition, a child must have two birth parents. I would set them in the constructor.
Later changes to legal parents can be made as needed. A child rarely has more than 2 parents, but anything is legally possible.

This post has been edited by NormR: 09 June 2020 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -

#7 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 124
  • Joined: 28-November 13

Re: Parents and childs

Posted 09 June 2020 - 07:59 AM

well its an exercise for my uni i can change it the way are want or else i would have made it as i wish
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: Parents and childs

Posted 09 June 2020 - 08:46 AM

More thoughts:
Every person is a child
Not every person is a parent
Was This Post Helpful? 0
  • +
  • -

#9 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 124
  • Joined: 28-November 13

Re: Parents and childs

Posted 09 June 2020 - 08:59 AM

i asked about the addperson method well they told me it should be a function that adds a person to a collection no matter what it is arraylist list set deque ... any idea on how to do this
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15724
  • View blog
  • Posts: 62,994
  • Joined: 12-June 08

Re: Parents and childs

Posted 09 June 2020 - 09:03 AM

Is having two classes mandatory? I would assume you can just have one class, 'Person', and implement a tree of sorts. The relationship of parent/child is implied by the data structure and not concrete classes.
Was This Post Helpful? 0
  • +
  • -

#11 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: Parents and childs

Posted 09 June 2020 - 09:21 AM

Quote

function that adds a person to a collection no matter what it is arraylist list set deque ... any idea on how to do this

Teams and clubs are examples of something that contains persons. How the person objects are saved would be an implementation detail inside the class. The person objects could be saved in the classes you listed: arraylist list set deque ...
Was This Post Helpful? 0
  • +
  • -

#12 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 124
  • Joined: 28-November 13

Re: Parents and childs

Posted 09 June 2020 - 09:25 AM

yeah but what i meant about the function add person is that it should work no matter what collection you give it as parameter i have no idea how to do this if you can guide me to a tut or help me understand how it's done that would be great
ex: addPerson(ArrayList a) or addPerson(set s)
both should add to the collection
Was This Post Helpful? 0
  • +
  • -

#13 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: Parents and childs

Posted 09 June 2020 - 09:43 AM

Quote

the function add person is that it should work no matter what collection you give it as parameter

The method addPerson would be defined for all the classes that can have a list of Persons like a team or passengers on a bus.
The java collection class that was used would be an implementation detail for that class. The code would NOT work directly with the collection class. It would work with the classes you define like team or bus.

This post has been edited by NormR: 09 June 2020 - 09:45 AM

Was This Post Helpful? 0
  • +
  • -

#14 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 124
  • Joined: 28-November 13

Re: Parents and childs

Posted 09 June 2020 - 09:59 AM

can you by any chance give an example ?
Was This Post Helpful? 0
  • +
  • -

#15 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: Parents and childs

Posted 09 June 2020 - 10:05 AM

Here's some almost code
class Team {
  private List<Person> personsOnTeam = new ArrayList  // Declare list of persons on team
  public void addPerson(Person p) {
      personsOnTeam.add(p)
  }
} 

This post has been edited by NormR: 09 June 2020 - 10:06 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1