First name, Last name, Age JAVA POO

-2

I have to do this: computationally the problem of getting the full name, age, age in months from the name, surname and year of birth of a person. I did it but it's not working, can someone 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; 
   }
}
    
asked by anonymous 09.10.2017 / 03:52

1 answer

0

See these two methods:

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

E:

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

Both declare return String , but have no statement of type return . The compiler will not like this and will complain that return is missing. BlueJ will show you this error that it takes from the compiler.

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

Your code also does not go well with Java language conventions (even if you consider that what happens in The truth is that there are some unofficial conventions competing with each other). In particular, the AnoDeNascimento field should be called anoDeNascimento , starting with a lowercase letter.

In addition, the parameter name in setAnoDeNascimento method could be newAnoDeNascimento , instead of anonas . The reason is that abbreviating words by eating letters from 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 setter parameters would be novoNome and novoSobrenome . As for mixing Portuguese and English by putting the prefixes get and set , this is better tolerated because many tools look for methods with the prefixes get and set to identify the properties of their objects, and When changing these prefixes, these tools will not work with your class (which is another reason to call your method to set the% surname to%).

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

Note also that in your { method, it is a good idea to add a space between the first and last name.

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

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;
    }
}
    
09.10.2017 / 15:41