Family relations

  • 3
  • Locked
A programmer can create a man and a woman with a couple of deft movements of his or her fingers. Easy-peasy: we'll write the appropriate classes and create objects. Let's work on a married couple: we'll create Man and Woman objects, and then save a reference to the Woman in man.wife, and a reference to the Man in woman.husband. You see? You don't even need a marriage license.
You can't complete this task, because you're not signed in.
Comments (68)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Natasha Ghoorah
Level 3 , United Kingdom
8 July, 11:00
Got there by trial & error, 13 attempts. I am a total newbie and I am not sure I am actually learning rather than guessing. Can anyone else related? Or is the guessing part of the learning process?
White Wash Scrum Master at capgemini
6 September, 13:26
I kinda feel the same, but since java is quite unforgiving. Just getting to the correct answer already seems to me that we are doing something right.
Level 3 , Manchester, United States
11 April, 16:53
What exactly is meant by "saving a reference to an object" after an object is defined? Having a hard time wrapping my head around the concept. Any help is appreciated.
Level 6 , Detroit, United States
4 January, 03:24
woman.husband = man;
man.wife = woman;
Can someone help me understand why if I write the same as throws a NPE ?
man = woman.husband;
woman = man.wife;
Level 41
4 January, 09:16
woman.husband = man;
man = woman.husband;
are not the same!
Level 3 , San Antonio, United States
12 October 2020, 16:05
Create 2 objects Man and Woman store "woman" reference in man.wife then store "man" reference in woman.husband
Level 5 , Slough, United Kingdom
5 October 2020, 13:58
It was difficult, in case you have also struggled, please check below hints: => The first 2 tasks, you need to create 2 objects. One of the object's should be named as a Woman and the second one is called Man. A general rule how to create an object: ClassName objectname = new ClassName(); => The last 2 tasks, just think about very carefully about the hint that we have been given: woman.husband = man Good luck everyone!
Level 6 , Germany
9 June 2020, 09:45
Here are some explanations that may be of help: 1. A variable can be assigned to another variable, the same thing applies to an object reference. Meaning, an object reference can be assigned to another object reference. What is an object reference? Dog maxy = new Dog(); Here, maxy is an object reference. Now, maxy is already 'a variable that is referenced to a Dog object'. Since a variable can be assigned to another variable, the following is valid: Dog maxy = new Dog(); kikipaka = maxy; // here kikipaka is an object reference that refers to the sama object as maxy does. Be careful, if somehow your code is written like this: ********* kikipaka = maxy; Dog maxy = new Dog(); ********* The code won't compile because maxy the object reference has not yet been made. 2. The confusing '.' operator Please see the explanation about the cat. The code is as follow: ********** Person person; person = new Person(); person = null; Cat cat = new Cat(); cat.owner = new Person(); = "William"; ********************** It's confusing because we assume that the '.' operator serves only to call something. But in fact, naming your variable with a dot '.' is valid. Meaning, naming your variable like String x.max = "Hello World"; is valid although you're not calling anything. Here you can see that the cat.owner does not serve to CALL, rather it is the NAME of the variable. Thus, a new object reference is made by writing *cat.owner = new Person();* It could also be written as *catOwner = new Person();* Try to change *woman.husband* to *womanHusband* and *man.wife* to *manWife*. Maybe you can understand better. p.s: I still have not yet understood the public Woman wife; and the public Man husband; I'm sure something I don't know is still lurking there, but I'll save it for later
Level 6 , Germany
9 June 2020, 10:56
UPDATE So there has been a fundamental mistake here. I said that the naming something with a '.' is valid although you're not calling something. Well, it seems that it's a mistake. At least in this case, it is a mistake. So man.wife is actually an object reference calling something from the inside of the Man class. Think about it as man.age is actually calling the age variable and we can assign the man.age variable to a number like 55 or something else. Here, man.wife is calling the 'wife' part. I don't know if it's the correct, but... oh well. Notice the there is command Public Woman wife; inside the Man class. Do not forget that you can make an object reference variable like this: Woman woman = new Woman(); or Woman woman; woman = new Woman; In this case, it is written Woman wife; That means that the wife is an object reference variable. Now back to the man.wife. The man is an object reference variable that refers to the Man class. Now, man references itself to something inside the Man class and it happens to be that man refers to just another object reference variable called wife. So, in short, man.wife is an object reference variable that refers to another object reference variable named wife. Please note, that the man.wife does not have any value, because it is not assigned to any object. Thus, it is null. Therefore, when we assign man.wife = woman; we have made so, that the object reference variable called wife refers to the same object the object reference variable callen woman does. Hope this helps
Level 3 , Hamburg, Germany
10 September 2020, 23:54
Thanks alot for sharing your thoughts. It helped me to unwrap and actually understand the meaning behind the man.wife = woman part.
Brandon Leirer
Level 7 , Keller, United States
8 May 2020, 17:10
Just my 2 cents, this task could use more/better explanation. This was the first one where I had zero clue what I was attempting to do.
Level 7 , Lübeck, Deutschland
23 July 2020, 08:24
I had that problem in the previous exercise but this I understand now really well
Schantl Johann
Level 2 , Wuerzburg, Germany
28 February 2020, 13:24
Dear All, Could you have a look at my Code. It is not working? I don't know why. Thanks Johann
Level 41 , Solar System
1 March 2020, 08:13
Please use the Help option within the task to ask for help with your code, otherwise no one can see it and no one can help you with it.
Halit Erdogdi
Level 4 , Douglasville, United States
22 February 2020, 19:03
Why is this resulting in an error?
Level 41 , Solar System
23 February 2020, 11:28
Please use the Help option within the task to ask for help with your code, otherwise no one can see it and no one can help you with it.
ivan Preudhomme
Level 2 , Den Haag, Netherlands
22 February 2020, 17:53
Yes capitalisation errors was problem