Good afternoon everyone. In between work and JavaRush, I bought and started reading “ Learning Java ” by Kathy Sierra and Bert Bates. Today I did another homework assignment in the textbook. I couldn't solve the problem " class Triangle " p. 95. In IntelliJ IDEA the answer was not what I needed. I spent two hours, tried all possible options, nothing worked. He spat and went for the answers. And there is my very first decision. As a result, if you enter this code into IntelliJ IDEA, the answer will not be the one written in the textbook. The correct solution from the answers looks like this:
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;
}
}
The answer, according to the authors of the textbook, should be:
треугольник 0, зона = 4.0
треугольник 1, зона = 10.0
треугольник 2, зона = 18.0
треугольник 3, зона = 28.0
y = 4, зона t5 = 343.0
In reality the answer looks like this:
треугольник 0, зона = 3.0
треугольник 1, зона = 4.0
треугольник 2, зона = 6.0
треугольник 3, зона = 7.0
y = 4, зона t5 = 343.0
I only had two possible answers:
-
Technical incompleteness and typo.
-
The book was written using Java 5.0, I solve the problem in Java 8.0
GO TO FULL VERSION