Let's say 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 for 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 aniversariarNoMes
method is called with the value mes = 8
;
A string string is defined as% empty,
Checks whether the entered month is valid (between 1 and 12, inclusive);
The value is valid, then the saida
is executed;
Scrolls the list of friends defined in else
;
The value of agenda
will be amigo
;
Checks that the% birthday month of% is 8;
False, Amigo("João", "20/01/1983")
is not executed;
Returns the value of amigo
, concatenated with the name of if
;
As returned, the method is terminated and therefore the returned value would be saida
. In addition to having returned before the time, it returned an unexpected value, since João celebrates his birthday in month 01. By this you already know that this amigo
does not make sense to be there. Removing it, it stays:
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;
}
Remaining the table test:
The João\n
method is called with the value return
;
A string string is defined as% empty,
Checks whether the entered month is valid (between 1 and 12, inclusive);
The value is valid, then the aniversariarNoMes
is executed;
Scrolls the list of friends defined in mes = 8
;
The value of saida
will be else
;
Checks that the% birthday month of% is 8;
False, agenda
is not executed;
At the next iteration of amigo
, Amigo("João", "20/01/1983")
is amigo
;
Checks that the% birthday month of% is 8;
True, then if
is executed;
Updates the value of for
to the name of amigo
;
Returns the value of Amigo("Marcia", "12/08/1990")
;
As returned, the method is terminated again. In this case, the return would be amigo
. In fact this record is birthday in month 8, but it is not the only one, so we can not finalize the method at this time. This shows us that again this if
is wrong. Removing it, it stays:
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;
}
Remaining the table test:
The saida
method is called with the value amigo
;
A string string is defined as% empty,
Checks whether the entered month is valid (between 1 and 12, inclusive);
The value is valid, then the amigo
is executed;
Scrolls the list of friends defined in Marcia\n
;
The value of return
will be aniversariarNoMes
;
Checks that the% birthday month of% is 8;
False, mes = 8
is not executed;
At the next iteration of saida
, else
is agenda
;
Checks that the% birthday month of% is 8;
True, then amigo
is executed;
Updates the value of Amigo("João", "20/01/1983")
to the name of amigo
;
At the next iteration of if
, for
is amigo
;
Checks that the% birthday month of% is 8;
False, Amigo("Marcia", "12/08/1990")
is not executed;
At the next iteration of amigo
, if
is saida
;
Checks that the% birthday month of% is 8;
True, then amigo
is executed;
Updates the value of for
to the name of amigo
;
The list is terminated, then the Amigo("Lucas", "02/12/1989")
is terminated;
Returns amigo
;
We have already noticed that now it is always returning if
, so there must be another for
in the code that returns the correct value. As we have seen that inside amigo
does not work, just put it outside:
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 look at item 5.14 of the last table test, you will see that the value of Amigo("Tereza", "30/08/1985")
ends with amigo
. This is not what we want, but the list of names of the anniversaries. That is, within the if
, we can not only make the simple assignment to the value of saida
, but rather concatenate 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;
}
In this way, the code should return amigo
, which is expected, or for
if there are no birthday parties. 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.