First name, Last name, JAVA POO age

Asked

Viewed 774 times

-2

I have to do this: Computationally model the problem of getting the full name, age, the age in months from the name, surname and year of birth of a person. I did so but it’s not working, someone can help me?

public class Pessoa

{
   public String nome;
   public String sobrenome;
   public int AnoDeNascimento;

   public String setNome (String newNome)
   {
       this.nome = newNome;
   }

   public String getNome () 
   {
       return nome;
   }

   public String Sobrenome (String newSobrenome)
   {
       this.sobrenome = newSobrenome;
   }

   public String getSobrenome ()
   {
       return sobrenome;
   }

   public int setAnoDeNascimento (int anonas)
   {
       this.AnoDeNascimento = anonas;
   }

   public String getNomeCompleto () 
   {
       return nome + sobrenome; 
   }
}
  • 3

    What "is not working"?

  • The bluej keeps complaining about public String of the methods setNome, and setAnoDeNascimento

  • 1

    Complaining is very general, you need to detail this.

  • 2

    Because the signature of the two methods require return but you return nothing. Methods setters normally return nothing, are void;

  • Got it, thank you very much helped me a lot

  • I think someone already told you that you’re not supposed to use public attributes, right?

  • @I could post a reply to our friend saying this, but as you saw first, I think you’d better go through with it.

  • @Victorstafusa if you want to answer with something more elaborate, can answer, I’m terrible at elaborating theoretical or canonical answers, as this question asks.

  • @Articuno Feito.

Show 4 more comments

1 answer

0

Look at these two methods:

  public String setNome (String newNome)
  {
      this.nome = newNome;
  }

And:

  public String Sobrenome (String newSobrenome)
  {
      this.sobrenome = newSobrenome;
  }

Both declare return String, but they have no kind of instruction return. The compiler will not like this and will complain that the return. Bluej will show you this bug he gets from the compiler.

The return of both should be void. In addition, the method for defining seobrenome should be called setSobrenome, and not just Sobrenome.

Your code doesn’t follow well either conventions of the Java language (even if we consider that what actually happens is that there are some unofficial conventions competing with each other). In particular, the field AnoDeNascimento should be called anoDeNascimento, starting with lower case letter.

In addition, the parameter name in the method setAnoDeNascimento could be newAnoDeNascimento, instead of anonas. The reason is that abbreviating words by eating letters of the name is a bad programming practice and tends to make programs very difficult to understand. In fact, to avoid mixing words in English with words in Portuguese in the same name, we could call novoAnoDeNascimento. The other parameters of the setters would be novoNome and novoSobrenome. Mixing Portuguese and English by prefixing get and set, Hence it is better to be tolerated because many tools seek methods with prefixes get and set to identify the properties of your objects, and by changing these prefixes, these tools will not work with your class (which is another reason to call your method to define the last name of setSobrenome).

Another detail is that in no widely used code formatting convention, space is used between the method name and the ( of the following parameters. The blank line between the class declaration and the { also not something that is in accordance with any code formatting convention.

Note also that in your method getNomeCompleto(), it is a good idea to add a space between the name and the surname.

Finally, you may have seen in several places that maintaining public attributes/fields is considered a bad programming practice, and the reason you’re using getters and setters has directly to do with it. Therefore, they must be private.

By correcting these details, your code looks like this:

public class Pessoa
{
    private String nome;
    private String sobrenome;
    private int anoDeNascimento;

    public void setNome(String novoNome)
    {
        this.nome = novoNome;
    }

    public String getNome() 
    {
        return nome;
    }

    public void setSobrenome(String novoSobrenome)
    {
        this.sobrenome = novoSobrenome;
    }

    public String getSobrenome()
    {
        return sobrenome;
    }

    public int setAnoDeNascimento(int novoAnoDeNascimento)
    {
        this.anoDeNascimento = novoAnoDeNascimento;
    }

    public String getNomeCompleto() 
    {
        return nome + " " + sobrenome;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.