Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Logic error in Math.pow(a,b) using JOptionPane

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    /* 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:

    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:
    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
    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Logic error in Math.pow(a,b) using JOptionPane

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Logic error in Math.pow(a,b) using JOptionPane

    Quote Originally Posted by Norm View Post
    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Logic error in Math.pow(a,b) using JOptionPane

    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).

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    double side1 = Math.pow((x2-x1),2);//this equals 4.0

    then I added another Math.pow()

    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.

    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.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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

  8. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Error message when using JOptionPane
    By runkerr in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 5th, 2013, 12:17 PM
  2. Not sure what the Logic Error is? [help]
    By Mitsuwa in forum Object Oriented Programming
    Replies: 2
    Last Post: January 27th, 2013, 11:55 PM
  3. Can you help me find my logic error
    By michael305rodri in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 5th, 2012, 01:51 AM
  4. Math.pow
    By britton33 in forum What's Wrong With My Code?
    Replies: 30
    Last Post: July 1st, 2012, 04:42 PM
  5. Lotto Problem logic error
    By ippo in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 10th, 2012, 10:18 AM