JavaRush /Java Blog /Random EN /Error in Learning Java by Kathy Sierra and Bert Bates
lookinbody
Level 11
Москва

Error in Learning Java by Kathy Sierra and Bert Bates

Published in the Random EN group
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. Error in textbookThe 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:
  1. Technical incompleteness and typo.

  2. The book was written using Java 5.0, I solve the problem in Java 8.0

Please tell me how right I am in my guesses.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION