0

I have an exercise which is asking us about basics concept apparently. But I can't find any information or at least I'm not sure about what I'm suppose to search. So I have a small class and I need to comment 3 different constructors. It look like this :

class Person{
      private String name;
      private ArrayList<String> pseudos;
      private String email;

//For me this one isn't a problem, it initialize a name & a mail with the two parameters and I believe the tricky part is this Array. I'm not sure but as far as I know it's okay to initialize the Array whithout getting him through parameter... Maybe I'm wrong.
public Person(String aName, String aEmail){
       name = aName;
       pseudos = new ArrayList<String>();
       email = aMail;
}

//It seems to be the same explanation than the first one.
public Person(){
       pseudos = newArrayList<String>();
}

//Apart from the uppercase I thinks this one is good, nothing is wrong.
public Person(String aName, String aMail, ArrayList<String> manyPseudos){
       name = aName;
       pseudos = new ArrayList<String>();
       if(pseudos != null)
       {
            Pseudos.addAll(manyPseudos); //The uppercase here should be the problem
       }
       mail = aMail;
  }
}

Of course I tried to figured it out through my lesson, but I didn't find anything so i supposed it's an exercise based on logic ... But I lacked of this kind of logic and I really want to at least trully understand this part since it's pretty basics and I will have to manipulate it a lot.

Again, thanks for your guidance and your time.

Alex
  • 21
  • 5
  • 4
    in the code you pasted none of the constructors are even in the class body of the Person class. – OH GOD SPIDERS Mar 04 '21 at 16:09
  • 2
    Think about which properties of a person should be mandatory and which optional. Then write constructors that leave out each optional property. – k314159 Mar 04 '21 at 16:11
  • @OHGODSPIDERS This is a good point to make the code complete, but I think given the context, we can assume the surrounding class since none of the questions are about the compiler errors that would arise without it. – Code-Apprentice Mar 04 '21 at 16:12
  • Do you really mean you "need to **comment** 3 different constructors"? Or **create** them? Are the constructors already written for you as part of the exercise and you need to add comments? – k314159 Mar 04 '21 at 16:14
  • @OHGODSPIDERS You're right, this mistake is one me. Code-Apprentice I think the name is the only one which is mandatory. But, I'm not sure of the impact. So if i use only the mandatory parameter how I handle the optional one ? – Alex Mar 04 '21 at 16:17
  • 1
    @k314159 Sorry for my english, yes the 3 constructor were already in this code we're just supposed to comment them, it give us no more details so I don't know if the comment will be about how it work or if we're supposed to find errors in it. – Alex Mar 04 '21 at 16:19
  • 1
    You can comment that there is no need to check `if (pseudos != null)` because you've just assigned it to a new object on the previous line, so it can never be null. – k314159 Mar 04 '21 at 16:21
  • @k314159 Oh I didn't new that, thanks ! I'll keep that in mind – Alex Mar 04 '21 at 16:28
  • Are you supposed to write the documentation of the constructors, that is, javadoc? These normally is done by comments starting with `/**` instead of `//` or `/*` and use some specific format/tags. See [Javadoc Guide](https://docs.oracle.com/en/java/javase/15/javadoc/index.html) or [How to Write Doc Comments for the Javadoc Tool](https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html) –  Mar 04 '21 at 16:31
  • No it's easier than that, we use visual paradigm in order to learn UML, sometimes we get this king of question as well in just a Note item. EDIT : But ! thanks for these doc, didn't knew it. – Alex Mar 04 '21 at 16:34

1 Answers1

2

I believe the tricky part is this Array. I'm not sure but as far as I know it's okay to initialize the Array whithout getting him through parameter... Maybe I'm wrong.

First, pseudos is an ArrayList, not an array. These are two different things.

Second, pseudos is a variable just like any other. This means you can initialize it in all the same ways you initialize any variable: from the value of another variable or directly from an expression. Here we initialize pseudos by creating a new ArrayList directly.

Pseudos.addAll(manyPseudos); //The uppercase here should be the problem

You have a good eye here. Psuedos at the beginning should be pseudos instead. Since Java is case-sensitive, these are two different things. We generally use the convention that class names start with uppercase and variables and functions start with lowercase. In theory you can have a class named Psuedos and a variable named pseudos, but you have to keep them separate in your mind.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Okay get it, so it's truly possible to just initialize inside a constructor without any parameters. And since an ArrayList (which I admit, cause me a lot of trouble to differentiate it from a simple array) is the same as other variable we also could initialise aName (for instance) without parameters ? For your second part thanks ! I'm relieave at least I get this one and thanks for your explanation as well. – Alex Mar 04 '21 at 16:23
  • @Alex "we also could initialise aName (for instance) without parameters" Exactly. The idea here is to decide what each field in the class represents and when you know what value it is supposed to have. In the case of a name, the `Person` class doesn't know what that should be, so it makes sense to take a parameter to initialize it. On the other hand, a list of pseudonyms must be initialized as an ArrayList object. But notice that we don't add anything to that list because `Person` doesn't know what those pseudonyms are. It just knows it needs a list to store them. – Code-Apprentice Mar 04 '21 at 16:44
  • @Alex I assume that the class also has an `addPseudonym()` function to actually add them. – Code-Apprentice Mar 04 '21 at 16:45