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 12 of 12

Thread: Help with programming homework

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with programming homework

    I have this program that I need help with. Basically its a program where a user is prompted to enter the length of all three sides of a triangle and the program calculates the area by herons formula and can tell if the triangle is equilateral or Pythagorean. I am having trouble entering a formula to where all three enter sides cant possibly be a triangle. Here is my Program. I need help where the '?' is stated.

    import java.util.Scanner;
    public class Triangle {
    public static void main(String[] args){

    double a;
    double b;
    double c;
    double s;
    double x;
    double area;

    Scanner input = new Scanner(System.in);
    System.out.println("Welcome to the Triangle Calculation Program");
    System.out.print ("Enter length of side one => ");
    a = input.nextDouble();
    System.out.print ("Enter length of side two => ");
    b = input.nextDouble();
    System.out.print ("Enter length of side three => ");
    c = input.nextDouble();
    System.out.println("Your triangle has an area of " + getArea(a,b,c));


    if ( (a == b) && (b == c) ) {
    System.out.println("This was a Equilateral triangle");
    }
    else if ( ( a*a + b*b == c*c ) || (b*b + c*c == a*a) || (c*c + a*a == b*b )) {
    System.out.println("This was a Pythagorean triangle");

    }
    else if ( ( ? ) ) {
    System.out.println("Sorry- not a valid triangle");
    }


    }
    public static double getArea(double a, double b, double c) {
    double s = (a + b + c)/2.0;
    double x = ((s) * (s-a) * (s-b) * (s-c));
    double area = Math.sqrt(x);
    return area;
    }

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with programming homework

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    I need help where the '?' is stated.
    Can you explain what your problem is?
    What decision does that if need to make? What values of what variables should be tested?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with programming homework

    Where the "?" is, I need to input a formula to where earlier in the program, if I enter 3 sides that cant possibly make a triangle, the 3 inputs will be put in that formula and it will print "Sorry- not a valid triangle". For an example if ( s*(s-a)*(s-b)*(s-c), which is double x, is negative, a triangle cant be formed and will say "Sorry- not a valid triangle". But how would I input that above formula negatively for it to compile and work with the program?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with programming homework

    how would I input that above formula negatively
    A negative value is less than 0. Test it by: if((put formula here) < 0)
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    JT123 (February 24th, 2014)

  6. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with programming homework

    It states that variable s in the formula might not have been initialized. But I thought I did that?

    --- Update ---

    Nevermind, I fixed it. It works now but another problem has come up.

    if ( ( s * s-a * s-b * s-c < 0 ) ) {
    System.out.println("Sorry- not a valid triangle");
    }
    It works, but I do not get the message "Sorry- not a valid triangle". I get "Your Triangle has an area of NaN". How does that show up if thats not in my program and how do I get rid of it to say "Sorry- not a valid triangle"?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with programming homework

    It states that variable s in the formula might not have been initialized.
    The compiler is usually correct. Double Check it.
    Where is the variable s given a value and where is the error?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with programming homework

    Nevermind, I fixed it. It works now but another problem has come up.

    if ( ( s * s-a * s-b * s-c < 0 ) ) {
    System.out.println("Sorry- not a valid triangle");
    }
    It works, but I do not get the message "Sorry- not a valid triangle". I get "Your Triangle has an area of NaN". How does that show up if thats not in my program and how do I get rid of it to say "Sorry- not a valid triangle"?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with programming homework

    I get "Your Triangle has an area of NaN"
    Can you copy the full contents of the console window that shows what you entered and what the program printed out?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with programming homework

    This is what the console window says:
    Welcome to the Triangle Calculation Program!
    Enter length of side one => 2
    Enter length of side two => 5
    Enter length of side three => 9
    Your Triangle has an area of NaN

    Obviously 2,5,9 doesnt make a triangle. And Its not suppose to say "Your triangle has an area of NaN" Its suppose to say "Sorry- not a valid triangle"

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with programming homework

    Your triangle has an area of NaN"
    Check the equations that compute the area to see why they return a NaN value.

    Also the chain of if/else if statements needs an ending else to print a message if none of the preceding statements were true.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with programming homework

    The area is correct, I transferred the formula straight from the handout for my homework. I dont need it to print the area, if its not a triangle, I cant have an area of the inputs. I just need it to say "Sorry-not a valid triangle". Instead it tries to tell me an area of a triangle that isnt a triangle with a formula that is right.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with programming homework

    Please post your new code and be sure to wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    (Note: I rarely try to read unformatted code)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java Programming (Object Oriented Programming) Assignment
    By azmilPhenom in forum Object Oriented Programming
    Replies: 3
    Last Post: December 21st, 2013, 07:26 AM
  2. Homework Help With Simple Java Programming
    By xxkronix in forum Java Theory & Questions
    Replies: 4
    Last Post: December 2nd, 2013, 06:29 PM
  3. JAVA PROGRAMMING HOMEWORK HELP!
    By crxxriot in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 23rd, 2013, 02:36 PM
  4. Java Programming Homework Help!!
    By sonicsoccer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 20th, 2013, 02:57 AM
  5. Replies: 0
    Last Post: December 12th, 2011, 03:17 PM