Area of a triangle (using 2 extra methods) error help
EDITED..
Oke, I managed to sort everything out, except, it does not calculate the area in area() method:
Code :
[COLOR="Red"]public static double area(double side1, double side2, double side3, double areaTri)[/COLOR]
{
double s = (side1 + side2 + side3)/2;
areaTri = Math.sqrt(s*(s - side1)*(s - side2)*(s - side3));
//Returns to main() and should print out the last statement with the area
return areaTri;
}
Updated Code:
Code :
//Calculate the area of a triangle
import java.util.Scanner;
import static java.lang.Math.*;
[COLOR="Lime"]public class MyTriangle[/COLOR]
{
[COLOR="Red"]public static void main(String[] args)[/COLOR]
{
double areaTri=0;
Scanner input = new Scanner(System.in);
System.out.print("Enter the size of the first side: ");
double side1 = input.nextDouble();
System.out.print("Enter the size of the second side: ");
double side2 = input.nextDouble();
System.out.print("Enter the size of the third side: ");
double side3 = input.nextDouble();
isValid(side1,side2, side3, areaTri);
System.out.println("");
System.out.println("The area of the triangle is "+ areaTri);
}
//Check if the sum of side1 and side2 is greater than side3 and if not returns false
//If true calls on method area()
[COLOR="Red"]public static boolean isValid(double side1, double side2, double side3, double areaTri)[/COLOR]
{
if(side1 * side2 > side3)
{
area(side1,side2,side3, areaTri);
return true;
}
else
{
System.out.println("The input is invalid!");
return false;
}
}
//calculates area of the triangle assuming conditions are met at method isValid()
[COLOR="red"]public static double area(double side1, double side2, double side3, double areaTri)[/COLOR]
{
double s = (side1 + side2 + side3)/2;
areaTri = Math.sqrt(s*(s - side1)*(s - side2)*(s - side3));
return areaTri;
}
}
Re: Area of a triangle (using 2 extra methods) error help
Quote:
, it does not calculate the area in area() method:
What is it supposed to do in that method?
Do you have a question?
If you need help debugging your code, I suggest that you separate out all the subexpressions in the sqrt() method call and print them out to see if they are computing to the values you expect.
Re: Area of a triangle (using 2 extra methods) error help
I managed to fix it in the main():
From:
Code :
System.out.println("The area of the triangle is "+ areaTri);
To:
Code :
System.out.println("The area of the triangle is "+ area(side1,side2,side3,areaTri));
This solved the problem and lead to another. How do I round it off to 1 decimal place. Right now the output would look something like this:
0.7261843774138906
Re: Area of a triangle (using 2 extra methods) error help
Quote:
How do I round it off to 1 decimal place
A couple of ways
The printf() has formatting strings
The DecimalFormat class will format a number.
Re: Area of a triangle (using 2 extra methods) error help
I decided to use printf() since I'm more familiar with it, but it doesnt seem to work the same:
In C this would normally work:
Code :
printf("The area of the triangle is %0.1f", area(side1,side2,side3,areaTot));
I get a format specifier error in java when running:
Code :
System.out.printf("The area of the triangle is %0.1f", area(side1,side2,side3,areaTot));
Re: Area of a triangle (using 2 extra methods) error help
Have you looked up the error message to see what it means? It could tell you what the problem is.
Re: Area of a triangle (using 2 extra methods) error help
I know what it means...the format specifier "%0.1f" doesnt work, my question is how could I interprate this into java to show a single decimal place.
Re: Area of a triangle (using 2 extra methods) error help
Try changing it. See the Formatter class for descriptions.
Re: Area of a triangle (using 2 extra methods) error help
Oke, I figured it out with the help of your link.
My problem was the 0 in %0.1f. It should look like this: %.1f
Thank you!
Re: Area of a triangle (using 2 extra methods) error help
You're welcome. Most of it is in the doc.