Results 1 to 5 of 5
  1. #1
    Legoland is offline Member
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    12
    Rep Power
    0

    Default Program for calculating area and hypotenus in a triangel

    Hi!
    I try to make a program on this conditions:
    Make a program where I can put in the values for the 2 cathetus in an right angled triangel the the program will calculate the hypotenusa and area.
    I have managed the hypotenusa but are stuck on the area I get Area 0.0.
    I also have probelms with the last showMeessageDialog I want the Area to be on a new line there.
    The program looks like this:
    import javax.swing.*;
    public class Pythagoras2 {
    public static void main (String[] arg) {
    String s;
    s = JOptionPane.showInputDialog ("First side?");
    double a = Double.parseDouble(s);
    s = JOptionPane.showInputDialog ("Second side?");
    double b = Double.parseDouble(s);
    double c = Math.sqrt(a*a + b*b);
    double area = (a*b%2);
    JOptionPane.showMessageDialog(null,
    "Hypotenusans length: " + c + "Area:" + area);

    System.exit(0);

    }
    }
    ????? Im lost
    Legoland

  2. #2
    ali
    ali is offline Member
    Join Date
    Apr 2007
    Posts
    7
    Rep Power
    0

    Default Corrections

    Hi,

    Try running the following code, I think it would solve all your problems

    import javax.swing.*;
    public class Pythagoras2 {
    public static void main (String[] arg) {
    String s;
    s = JOptionPane.showInputDialog ("First side?");
    double a = Double.parseDouble(s);
    s = JOptionPane.showInputDialog ("Second side?");
    double b = Double.parseDouble(s);
    double c = Math.sqrt(a*a + b*b);
    double area = (a*b/2);
    JOptionPane.showMessageDialog(null,
    "Hypotenusans length: " + c + "\nArea:" + area);

    System.exit(0);

    }
    }

    The first problem with your code was that you were trying to find the modulus (remainder) where you are supposed to divide.
    You need to put in escape sequence characters to change the lines in the Message dialogs the same way that you need them when displaying the output on console.

    Hope this solves the problem,

    Regards
    Ali

  3. #3
    Legoland is offline Member
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    12
    Rep Power
    0

    Default Thank you Ali!

    Thanks for your help now it works fine and I understand what I missed out on.
    I was totally stucked and I have noone to ask here at home.
    Have a nice weekend

  4. #4
    ali
    ali is offline Member
    Join Date
    Apr 2007
    Posts
    7
    Rep Power
    0

    Smile

    Dont mention it, I hope that you have an enjoyable weekend too.

    Regards,
    Ali

  5. #5
    g25451 is offline Member
    Join Date
    Aug 2008
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Calculating trigonometric functions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:56 PM
  2. Calculating hyperbolic functions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:55 PM
  3. Calculating the DB connection time
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:55 AM
  4. Calculating sin of a double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:13 PM
  5. Print Area
    By Riftwalker in forum Advanced Java
    Replies: 0
    Last Post: 11-28-2007, 07:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •