inherit
27035
0
Jan 9, 2024 11:43:57 GMT -8
Neo
1,809
July 2004
neodan
|
Post by Neo on Oct 24, 2006 14:07:40 GMT -8
Directions: Write an application that reads the lengts of the sides of a triangle from the user. Compare the area of the triangle using herons forumula. In which s is half of the perimeter of the triangle and a, b, c a are the lengths of the tree sides. Print the area to three decimal places.
I'm stuck on this part, anybody with the knowledge can help?
import cs1.Keyboard; import java.text.DecimalFormat;
public class HeronsForumula {
//******************************************************************** // Compares the area of the triangle using Heron's formula. //********************************************************************
public static void main (String[] args) {
int a, b, c; double area, s;
System.out.print ("Enter the first side "); a = Keyboard.readInt();
System.out.print ("Enter the second side "); b = Keyboard.readInt();
System.out.print ("Enter the third side "); c = Keyboard.readInt();
System.out.print ("Enter the Surface Area ");
s= (double) (a + b + c)/2;
area = (double) s * (s-a) * (s-b) * (s - c);
System.out.println ("The area is " + area);
}
}
|
|
inherit
54941
0
Mar 24, 2011 16:00:30 GMT -8
Mithras
2,603
August 2005
mithras
|
Post by Mithras on Oct 24, 2006 16:35:38 GMT -8
What's the problem?
|
|
inherit
20776
0
Aug 25, 2020 9:37:35 GMT -8
*Dan!
=)
1,282
February 2004
hangout2
|
Post by *Dan! on Oct 24, 2006 18:43:07 GMT -8
You need to create a new decimal object. I cant remember off the top of my head because it has been awhile sense i did decimal formating in java. Also why do you do " s= (double) (a + b + c)/2;" when you already defined s as a double? Also you never did read the keyboard for the surface area. Oh and you might want to make variables a,b and c double because what if the value of a line is 4.5. Java will not round that up or keep it in decimal format with the int variable type. It will change it back down to 4. Keep every variable set to double for this program .
|
|
inherit
27035
0
Jan 9, 2024 11:43:57 GMT -8
Neo
1,809
July 2004
neodan
|
Post by Neo on Oct 24, 2006 18:48:23 GMT -8
Thanks.
I will do that, and also what about the bolded part. Is that coded correctly?
|
|
inherit
54941
0
Mar 24, 2011 16:00:30 GMT -8
Mithras
2,603
August 2005
mithras
|
Post by Mithras on Oct 25, 2006 12:20:23 GMT -8
I usually do:
area = (double)(s * (s-a) * (s-b) * (s - c));
but I think it will work either way.
|
|