Boa tarde a todos. Entre o trabalho e o JavaRush, comprei e comecei a ler “ Aprendendo Java ”, de Kathy Sierra e Bert Bates. Hoje fiz outra tarefa de casa no livro didático. Não consegui resolver o problema " class Triangle " página 95. No IntelliJ IDEA a resposta não era o que eu precisava. Passei duas horas, tentei todas as opções possíveis, nada funcionou. Ele cuspiu e foi em busca das respostas. E aí está minha primeira decisão. Como resultado, se você inserir este código no IntelliJ IDEA, a resposta não será a que está escrita no livro. A solução correta das respostas é assim:
public class Triangle
{
double area;
int height;
int lenght;
public static void main(String[] args)
{
int x = 0;
Triangle [] ta = new Triangle[4];
while (x < 4)
{
ta[x] = new Triangle();
ta[x].height = (x + 1) * 2;
ta[x].lenght = x + 4;
ta[x].setArea();
System.out.print("треугольник " +x+ ", зона");
System.out.println(" = " + ta[x].area);
x = x + 1;
}
int y = x;
x = 27;
Triangle t5 = ta[2];
ta[2].area = 343;
System.out.print("y = " + y);
System.out.println(", зона t5 = " + t5.area);
}
void setArea()
{
area = (height + lenght) / 2;
}
}
A resposta, segundo os autores do livro didático, deveria ser:
треугольник 0, зона = 4.0
треугольник 1, зона = 10.0
треугольник 2, зона = 18.0
треугольник 3, зона = 28.0
y = 4, зона t5 = 343.0
Na realidade a resposta é assim:
треугольник 0, зона = 3.0
треугольник 1, зона = 4.0
треугольник 2, зона = 6.0
треугольник 3, зона = 7.0
y = 4, зона t5 = 343.0
Eu só tinha duas respostas possíveis:
-
Incompletude técnica e erro de digitação.
-
O livro foi escrito em Java 5.0, resolvo o problema em Java 8.0
GO TO FULL VERSION