Let us consider that you have the following list of friends:
Amigo[] agenda = {
new Amigo("João", "20/01/1983"),
new Amigo("Marcia", "12/08/1990"),
new Amigo("Lucas", "02/12/1989"),
new Amigo("Tereza", "30/08/1985")
}
You want to get the birthday list of the month 08, then call the method:
String aniversariantes = objeto.aniversariarNoMes(8);
Considering the initial implementation of the method:
public static String aniversariarNoMes(int mes) {
String saida = "";
if (mes < 01 || mes > 12) {
return "Mês invalido.";
} else {
for (Amigo amigo : agenda) {
if (mes == amigo.getMesNas()) {
saida = amigo.getNome() + "\n";
return saida;
}
return saida+=amigo.getNome()+"\n";
}
}
return null;
}
Follow the steps that will be performed (table test):
- The method
aniversariarNoMes
is called with the value mes = 8
;
- A string
saida
empty;
- Checks whether the month reported is valid (between 1 and 12 inclusive);
- The value is valid, then the
else
;
- Scroll through the list of friends defined in
agenda
;
- The value of
amigo
will be Amigo("João", "20/01/1983")
;
- Check whether the anniversary month of
amigo
is 8;
- False, does not run the
if
;
- Returns the value of
saida
, concatenated with the name of amigo
;
As it was returned, the method is finalized and therefore the returned value would be João\n
. Besides having returned before the hour, returned an unexpected value, since John makes birthday in the month 01. With this you already know that this return
it makes no sense to stay there. Removing it, it:
public static String aniversariarNoMes(int mes) {
String saida = "";
if (mes < 01 || mes > 12) {
return "Mês invalido.";
} else {
for (Amigo amigo : agenda) {
if (mes == amigo.getMesNas()) {
saida = amigo.getNome() + "\n";
return saida;
}
}
}
return null;
}
Redoing the table test:
- The method
aniversariarNoMes
is called with the value mes = 8
;
- A string
saida
empty;
- Checks whether the month reported is valid (between 1 and 12 inclusive);
- The value is valid, then the
else
;
- Scroll through the list of friends defined in
agenda
;
- The value of
amigo
will be Amigo("João", "20/01/1983")
;
- Check whether the anniversary month of
amigo
is 8;
- False, does not run the
if
;
- In the next iteration of
for
, amigo
vale Amigo("Marcia", "12/08/1990")
;
- Check whether the anniversary month of
amigo
is 8;
- True, then runs the
if
;
- Updates the value of
saida
to the name of amigo
;
- Returns the value of
amigo
;
As returned, again the method is finished. In this case, the return would be Marcia\n
. Indeed this record makes birthday in month 8, but is not the only one, so we can not finalize the method at this time. This shows us that again this return
is wrong. Removing it, gets:
public static String aniversariarNoMes(int mes) {
String saida = "";
if (mes < 01 || mes > 12) {
return "Mês invalido.";
} else {
for (Amigo amigo : agenda) {
if (mes == amigo.getMesNas()) {
saida = amigo.getNome() + "\n";
}
}
}
return null;
}
Redoing the table test:
- The method
aniversariarNoMes
is called with the value mes = 8
;
- A string
saida
empty;
- Checks whether the month reported is valid (between 1 and 12 inclusive);
- The value is valid, then the
else
;
- Scroll through the list of friends defined in
agenda
;
- The value of
amigo
will be Amigo("João", "20/01/1983")
;
- Check whether the anniversary month of
amigo
is 8;
- False, does not run the
if
;
- In the next iteration of
for
, amigo
vale Amigo("Marcia", "12/08/1990")
;
- Check whether the anniversary month of
amigo
is 8;
- True, then runs the
if
;
- Updates the value of
saida
to the name of amigo
;
- In the next iteration of
for
, amigo
vale Amigo("Lucas", "02/12/1989")
;
- Check whether the anniversary month of
amigo
is 8;
- False, does not run the
if
;
- In the next iteration of
for
, amigo
vale Amigo("Tereza", "30/08/1985")
;
- Check whether the anniversary month of
amigo
is 8;
- True, then runs the
if
;
- Updates the value of
saida
to the name of amigo
;
- The list is finalized, so the
for
;
- Returns
null
;
We already realize that now it’s always returning null
, then there must be another return
in the code that returns the correct value to us. As we have seen within the for
does not work, just put out of it:
public static String aniversariarNoMes(int mes) {
String saida = "";
if (mes < 01 || mes > 12) {
return "Mês invalido.";
} else {
for (Amigo amigo : agenda) {
if (mes == amigo.getMesNas()) {
saida = amigo.getNome() + "\n";
}
}
return saida;
}
return null;
}
But if you analyze item 5.14 of the last table test, you will see that the value of saida
ends up valid Tereza\n
. This is not what we want, but the list of names of the birthday kids. That is, within the if
, we can not just make the simple assignment to the value of saida
, but to concatenate to the current value. To do this:
public static String aniversariarNoMes(int mes) {
String saida = "";
if (mes < 01 || mes > 12) {
return "Mês invalido.";
} else {
for (Amigo amigo : agenda) {
if (mes == amigo.getMesNas()) {
saida += amigo.getNome() + "\n";
}
}
return saida;
}
return null;
}
This way, the code must return Marcia\nTereza\n
, which is expected, or null
if there are no birthday kids. I leave the table test of this version as an activity for you. Do it and see if it really matches what the code does.
Please, when you paste code into Stackoverflow, enter 4 characters before each line of code. This way, the code is readable.
– Nexus
I’m new here, so I don’t know very well. But I’ll pay attention, thank you.
– Davi Fonseca
But if the month of birth of
amigo
is what you want, you update the value ofsaida
and immediately returns it, leaving the method. Wouldn’t the correct one be to concatenate the value and only in the end return? By the way, if you’re new to the community, start by doing the [tour].– Woss
@The correct thing is that I’m new to programming and I’m looking for help. This is why I joined the community.
– Davi Fonseca
And we’re here to help. You’ve done the table test to understand what your code does?
– Woss
I didn’t @Andersoncarloswoss
– Davi Fonseca
If using Calendar to catch the month, it does not count from 1 to 12 but from 0 to 11. In addition, your code is confused, things are missing there to understand better, as this class Friend and as you call this method.
– user28595
@Andersoncarloswoss can post all the code?
– Davi Fonseca
I do not think strictly necessary, because the logic error is clear already in the code posted. If you take the table test, it might be clear to you as well. Can you do it? Now, here we have excellent Java programmers. Maybe if you post the full code, you might get hints for better implementations if that’s the case.
– Woss
How does the table test?
– Davi Fonseca