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

# Thread: Help with programming homework

1. ## 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;
}

}  Reply With Quote

3. ## Re: Help with programming homework

[code=java]
[/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?  Reply With Quote

4. ## 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?  Reply With Quote

5. ## 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)  Reply With Quote

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

JT123 (February 24th, 2014)

7. ## 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"?  Reply With Quote

8. ## 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?  Reply With Quote

9. ## 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"?  Reply With Quote

10. ## 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?

[code=java]
[/code]
to get highlighting and preserve formatting.  Reply With Quote

11. ## 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"  Reply With Quote

12. ## 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.  Reply With Quote

13. ## 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.  Reply With Quote

14. ## Re: Help with programming homework  Reply With Quote