Hello,
I've been trying to figure out one part of my code for around 3 hours.
I've been rewriting it different ways, only running what the logic error is and not the other parts and I still can't seem to get what I want.
I think that if I can figure out this one part of the program that it will help me with the whole program as I'll know what I did wrong.
I'll tell you what I'm having problems at in the program at the bottom of the code.
I'm a beginner so i'm sure it's not a very difficult problem to solve.
This is the program directions:
/* Write a program that prompts the user to enter three points (x1, *y1), (x2, y2), (x3, y3) of a triangle and displays its area. The *formula for computing the area of a triangle is *S = (side1 + side2 + side3)/2; *Area = √(s(s-side1)(s-side2)(s-side3)) */
I'm trying to be consistent with this problem by only using certain numbers.
If you run the program it will ask you these numbers in the order I listed them.
The number's I am using are:
x1 = 3
y1 = 4
x2 = 5
y2 = 6
x3 = 7
y3 = 4
(i hope the code is easy to read i tried to make it so)
Here is the code:
To avoid typing too much in the code, I'll tell you exactly what my problem is.import javax.swing.JOptionPane; public class AreaOfATriangle { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub /* Write a program that prompts the user to enter three points (x1, *y1), (x2, y2), (x3, y3) of a triangle and displays its area. The *formula for computing the area of a triangle is *S = (side1 + side2 + side3)/2; *Area = √(s(s-side1)(s-side2)(s-side3)) */ String x1String = JOptionPane.showInputDialog("Enter a point for x1 of (P(x and y)) for a triangle: "); double x1 = Double.parseDouble(x1String); String y1String = JOptionPane.showInputDialog("Enter a point for y1 of (P(x and y)) for a triangle: "); double y1 = Double.parseDouble(y1String); String x2String = JOptionPane.showInputDialog("Enter a point for x2 of (P(x and y)) for a triangle: "); double x2 = Double.parseDouble(x2String); String y2String = JOptionPane.showInputDialog("Enter a point for y2 of (P(x and y)) for a triangle: "); double y2 = Double.parseDouble(y2String); String x3String = JOptionPane.showInputDialog("Enter a point for x3 of (P(x and y)) for a triangle: "); double x3 = Double.parseDouble(x3String); String y3String = JOptionPane.showInputDialog("Enter a point for y3 of (P(x and y)) for a triangle: "); double y3 = Double.parseDouble(y3String); double side1 = Math.pow(Math.pow((x2-x1),2) + Math.pow((y2-y1),2),(0.5));/*this should be 4 but for some reason it's giving me 2.82364723723 (i'm calculating the side of a triangle here) *i'm using the distance formula to find the side, the program didn't give this to me, I don't know why */ double side2 = Math.pow(Math.pow((x3-x2),2) + Math.pow((y3-y2),2),(0.5));//this is another side of a triangle i'm calculating double side3 = Math.pow(Math.pow((x1-x3),2) + Math.pow((y1-y3),2),(0.5));//another side to calculate double s = (side1 + side2 + side3) / 2;//s means semiperimeter (calculating) double area = Math.pow((s * (s - side1) * (s - side2) * (s - side3)), (0.5));//calculates the area String output = "The area of the triangle is: " + area + ".";//creating a result JOptionPane.showMessageDialog(null, output, "Result", JOptionPane.WARNING_MESSAGE);//displaying the result } }
I'm trying to get an area, but I figured out that in my code there is a logic error in the following piece of code:
I noticed that it was giving me 2.8 instead of 4.0. I couldn't figure this out, why is it doing this? What's wrong in the code and how do i make it 4.0?
Also, in Math.pow in the piece of code I just mentioned, why can't I use (1/2) instead of (0.5)? Does it have something to do with the datatype i'm using?
When i run my code with everything included it says there are errors in the project, but it doesn't say where. When i run it anyway (on eclipse), the area is give as 3.999999999982 when it
should be 11 (I calculated the area using the numbers I gave and it gave 11).
I assuming that once i correct then it will fix the errors in the program and it will run.
Thank you for taking the time to look at this.


LinkBack URL
About LinkBacks
Reply With Quote