# Help with programming homework

• February 24th, 2014, 06:47 PM
JT123
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;
}

}
• February 24th, 2014, 06:59 PM
Norm
Re: Help with programming homework
[code=java]
[/code]
to get highlighting and preserve formatting.

Quote:

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?
• February 24th, 2014, 07:10 PM
JT123
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?
• February 24th, 2014, 07:17 PM
Norm
Re: Help with programming homework
Quote:

how would I input that above formula negatively
A negative value is less than 0. Test it by: if((put formula here) < 0)
• February 24th, 2014, 07:28 PM
JT123
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"?
• February 24th, 2014, 07:29 PM
Norm
Re: Help with programming homework
Quote:

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?
• February 24th, 2014, 07:30 PM
JT123
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"?
• February 24th, 2014, 07:31 PM
Norm
Re: Help with programming homework
Quote:

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.
• February 24th, 2014, 07:48 PM
JT123
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"
• February 24th, 2014, 07:53 PM
Norm
Re: Help with programming homework
Quote:

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.
• February 24th, 2014, 08:03 PM
JT123
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.
• February 24th, 2014, 08:24 PM
Norm
Re: Help with programming homework