# Need Help with code ...beginner

• February 18th, 2012, 07:35 PM
Envious1
Need Help with code ...beginner
Hello, I have tried everything and my code won't do what it needs to can someone explain this to me D:!?

Write a Java program that computes the area of a scalene triangle. Use the input dialog box to get the sides a, b, and c of the scalene triangle and compute the area using the following equation;
Area = (s (s – a) (s - b) (s – c) )1/2 where s=(a+b+c)/2
Your program must check that none of the length of the sides (a,b,c) is <= 0 and s > a and s > b and s > c. The output must be displayed in the message dialog box. (hint: you may use math class methods if needed).

and heres what I have....

import javax.swing.JOptionPane;

public class Assignment3
{
public static void main(String[] args)
{
double sideA;
double sideB;
double sideC;
double s;
double Area;
String input;

input =
JOptionPane.showInputDialog("Enter side A"); //input box for side A
sideA = Double.parseDouble (input);

input =
JOptionPane.showInputDialog("Enter side B"); //input box for side B
sideB = Double.parseDouble (input);

input =
JOptionPane.showInputDialog("Enter side C"); //input box for side C
sideC = Double.parseDouble (input);

s = (sideA + sideB + sideC) / 2; //finds what s is = to

JOptionPane.showMessageDialog (null, "s is equal to " + s);

Area = Math.pow (s*(s - sideA)*(s - sideB)*(s - sideC),1/2); //finds the area

JOptionPane.showMessageDialog (null,"Area is equal to " + Area);

//checks to see the each side is less then or equal to 0

if (sideA <= 0)

JOptionPane.showMessageDialog (null, " A is less then 0");

if (sideB <= 0)

JOptionPane.showMessageDialog (null, " B is less then 0");

if (sideC <= 0)

JOptionPane.showMessageDialog (null, "C is less then 0");

System.exit(0);
}
}
• February 18th, 2012, 07:57 PM
snowguy13
Re: Need Help with code ...beginner
Are you getting errors (post them!), or do you not know how to continue? Please be specific in the description of your problem.
• February 18th, 2012, 10:04 PM
Envious1
Re: Need Help with code ...beginner
I think I got it right...we'll see when i get my grade back XD

import javax.swing.JOptionPane;

public class Assignment3
{
public static void main(String[] args)
{
double sideA;
double sideB;
double sideC;
double s;
double Area;
String input;

input =
JOptionPane.showInputDialog("Enter side A"); //input box for side A
sideA = Double.parseDouble (input);

input =
JOptionPane.showInputDialog("Enter side B"); //input box for side B
sideB = Double.parseDouble (input);

input =
JOptionPane.showInputDialog("Enter side C"); //input box for side C
sideC = Double.parseDouble (input);

//calculates s
s = (sideA + sideB + sideC) / 2; //finds what s is = to

JOptionPane.showMessageDialog (null, "s is equal to " + s);

//checks to see that each side is less then s
if (s > sideA && s > sideB && s > sideC)
{
JOptionPane.showMessageDialog (null, " All sides are less then s");
}
else
{
JOptionPane.showMessageDialog (null, " All sides are not less then s");
}

//calculates area
Area = Math.sqrt (s*(s - sideA)*(s - sideB)*(s - sideC)); //finds the area

JOptionPane.showMessageDialog (null,"Area is equal to " + Area);

//checks to see that each side is less then or equal to 0
if (sideA <= 0 && sideB <= 0 && sideC <= 0)
{
JOptionPane.showMessageDialog (null, " All sides are less then 0");
}
else
{
JOptionPane.showMessageDialog (null, " All sides are not less then 0");
}

System.exit(0);
}
}
• February 19th, 2012, 07:52 AM
snowguy13
Re: Need Help with code ...beginner
Don't you want to check if ANY of the sides are less than zero BEFORE you calculate the area? :P