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 must be signed in to leave a comment
15 March, 17:09
Este ejercicio es ridículo. Todo irá bien hasta que llegas a la parte de "woman.husband / man.wife". Es simplemente contraproducente que te pidan hacer este tipo de cosas cuando no has adquirido todavía una base. No aporta nada para el usuario que está aprendiendo, no te ayuda a entender mejor lo que estás haciendo y tampoco tiene ninguna utilidad si no es con una explicación posterior.
27 January, 18:58
I think writing "Man" and "man" is confusing.
19 October 2021, 10:53
Woman Class is a property of Man just like property of age and height in the class Man. The value (wife) serve as link to other class.
19 October 2021, 10:50
8 July 2021, 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 2021, 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.
11 April 2021, 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.
30 December 2022, 07:55
When you creates in the variable man the object you don't save the object itself but a reference to the object Reference is a memory adress in the hardware layer.
4 January 2021, 03:24
Can someone help me understand why if I write the same as throws a NPE ?
4 January 2021, 09:16
and are not the same!
12 October 2020, 16:05
Create 2 objects Man and Woman store "woman" reference in man.wife then store "man" reference in woman.husband
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!
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(); cat.owner.name = "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
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
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.