Logic error in Math.pow(a,b) using JOptionPane
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:
Code Java:
/* 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:
Code Java:
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
}
}
To avoid typing too much in the code, I'll tell you exactly what my problem is.
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:
Code Java:
double side1 = Math.pow(Math.pow((x2-x1),2) + Math.pow((y2-y1),2),(0.5));
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
Code Java:
double side1 = Math.pow(Math.pow((x2-x1),2) + Math.pow((y2-y1),2),(0.5));
then it will fix the errors in the program and it will run.
Thank you for taking the time to look at this.
Re: Logic error in Math.pow(a,b) using JOptionPane
Quote:
says there are errors in the project,
Are those compiler errors or execution errors?
Can you use the javac compiler to get good compiler error messages?
Re: Logic error in Math.pow(a,b) using JOptionPane
Quote:
Originally Posted by
Norm
Are those compiler errors or execution errors?
Can you use the javac compiler to get good compiler error messages?
It doesn't have any errors marked in the program when i have typed it all in, but when i compile and try to run it, it says:
Errors in Workspace:
Errors exist in required project(s):
AreaOfATriangle
Proceed with launch?
and then I proceed with executing it and it runs the program fine, although my errors in that code (i think they are called logic errors) are still there.
I was running this program in Eclipse when it said this, I tried using JGrasp and it compiles and runs without giving me an error though.
Can I use the javac compiler to get good compiler error messages? I'm not sure what you mean by this, Eclipse marks errors when i'm typing the code, but there aren't any errors it's marking in the program.
Re: Logic error in Math.pow(a,b) using JOptionPane
If you can do the computation manually and get the correct answer, you need to compare your results at each step with the programs results for that step. When your results and the program's results are different, you need to break the compound statements down into simple, single steps and compare your manual results with what the program produces. At some point you should find the error.
Re: Logic error in Math.pow(a,b) using JOptionPane
Quote:
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?
Yes, those are integer literals and Java will use integer math. 1/2 = 0 in integer land.
Are you sure your hand-calculated answer is correct?
Try graphing your triangle and see if you can estimate approximate lengths and area.
Side note:
There's a sqrt function available in Java. Personally I prefer this over pow(x,0.5).
Re: Logic error in Math.pow(a,b) using JOptionPane
I tried doing what Norm said,
I'm using:
x1 = 2, x2 = 2
y1 = 4, y2 = 4
Code Java:
double side1 = Math.pow((x2-x1),2);//this equals 4.0
then I added another Math.pow()
Code Java:
double side1 = Math.pow((x2-x1),2) + Math.pow((y2-y1),2);//this equals 8.0 ((4-2)^2) + ((4-2)^2) = 8.0
Then I added a Math.pow (also did Math.sqrt) to this.
Code Java:
double side1 = Math.pow(Math.pow((x2-x1),2) + Math.pow((y2-y1),2),(0.5));//this equals 2.8284
I thought the above would equal 4.0? Isn't it (4+4)^0.5 ?
I also tried the Math.sqrt and got the same answer: 2.8284
I did the hand calculations to this part of the program and its just 4.0 as well.
I'm assuming it has to do with me not correctly putting a Math.pow inside another Math.pow, however, I've never done anything like this. I've only used a single Math.pow one at a time.
Re: Logic error in Math.pow(a,b) using JOptionPane
No, sqrt(8) != 4. sqrt(8) ~= 2.8284
You can verify this easily because 4*4 = 16, and likewise 2.8284*2.8284 ~= 8
Re: Logic error in Math.pow(a,b) using JOptionPane
Thank you, Wow i feel like an idiot. My code was actually correct I just thought when I was typing in integer numbers into the program that for some reason the Area couldn't be a decimal number and that sqrt(8) != 4.
I appreciate everyone taking the time to reply to my problem. I thought my problem was more complicated than it actually was.