Vincent Nelissen
Vincent Nelissen

Reputation: 41

Searching for an answer why the answer in the book is not equal to the answer I receive from my program

import java.util.Scanner;
public class Volumeofatriangle {
    public static void main(String[] args){
        //Create a scanner
        Scanner input = new Scanner(System.in);
    
        //Receive the length of sides of a triangle.
        System.out.print("Enter the length of your triangle: ");
        double lengthOfSides = input.nextDouble();
            
        //area of triangle.
        double areaOfTriangle = lengthOfSides * lengthOfSides * Math.sqrt(3) / 4;
            
        //volume of triangle.
        double volumeOfTriangle = areaOfTriangle * lengthOfSides;
            
        System.out.println("The triangle has an area of " + areaOfTriangle +
            "and a volume of " + volumeOfTriangle);
    }
}

At the moment I fill in 3,5 as triangle the area I will get the following answers:

The triangle has an area of 5.304405598179686and a volume of 18.565419593628903

The exercise in the book is:

(Compute the volume of a triangle) Write a program that reads in the length of sides of an equilateral triangle and computes the area and volume using the following formulas:

area = square root 3/4 (length of sides)till the power of 2. volume = area * length.

With this information I made the program above. However the answer in the book to a 3.5 equiliteral triangle:

The area is 3.89 The volume of the triangular prism is 19.48

I tried writing the program several times but every time I don't get the answer from the book.

Did I make a mistake or is the answer in the book wrong ?

Upvotes: 1

Views: 1374

Answers (2)

Kemal Dursun
Kemal Dursun

Reputation: 11

Actually answer is true, but the descriptions are not clear. The book is writing for the question "Enter length of the sides and height of the Equilateral triangle:", it means you should enter length and height(2 different value).

According to the book, it must be 3 and 5. (Just point is not necessary, between 3 and 5 at the book).

Already normally you should plus with "height" to find The volume of the Triangular prism. So the formula is: volume = area * height(writing length instead of height, confusing)

So results are, area = 3.89 and volume = 19.48(like the book, just more digits after the point at my result)

I made the code like that:


        Scanner input = new Scanner(System.in); 

        double area;
        System.out.println("Enter length of sides and height of the Equilateral triangle: ");
        double lengthOfSides = input.nextDouble();
        double height = input.nextDouble();

        area = (Math.sqrt(3)/4)*Math.pow(lengthOfSides, 2);
        double volume = area * height;

        System.out.println("The area is " + area);
        System.out.print("The volume of the Triangular prism is " + volume);

(I am talking about Introduction to Java, Daniel Liang, Eleventh Edition, Page 91)

Upvotes: 1

EFM
EFM

Reputation: 46

The answer in the book is wrong. If you do it by hand, your program's results are the correct ones: 3.5^2*sqrt(3)/4 = 5.3.

Upvotes: 3

Related Questions