• October 13th, 2009, 04:56 PM
raidcomputer
I need help creating this program and i am completely new to methods i only know the basic syntax for a method. i want to design a algorithm for my process first and from there hopefully come up with some code...

Write a program that reads in three real numbers representing the sides of a triangle. The program determines whether the numbers represent a valid triangle, and if so computes the area of the triangle. If the input does not represent a valid triangle an appropriate message should be displayed to the user. Solve this problem by creating a MyTriangle class which, in addition to the main method, contains the following two methods:
Code :

``` // Returns true if the sum of any two sides is greater than the third side.   public static boolean isValid(double side1, double side2, double side3)       // Returns the area of the triangle.   public static double area(double side1, double side2, double side3)```

The area can be found by taking the square root of s(s – side1)(s – side2)(s – side3),

where s = (side1 + side2 + side3)/2

Include the following two sets of numbers among your test cases:

3.0 4.0 5.0

3.2 4.4 7.8

Heres what i have, still new to java methods so not sure where to start.....
Code :

```import java.util.Scanner;   public class AreaOfTriangle{ public static void main(String [] args){   Scanner input = new Scanner(System.in);   System.out.println("Enter a number representing the side of a triangle ") double side1 = input.nextDouble();   System.out.println("Enter the second side of the triangle ") double side2 = input.nextDouble();   System.out.println("Enter final side of triangle ") double side3 = input.nextDouble(); } }```
• October 14th, 2009, 04:23 AM
wolfgar
well , to determine if u can create a triangle to begin with u need to check that 2 of the sides added together don't equal less than the largest side

this could be a simple if chain
Code :

```  public static boolean isValid(double side1, double side2, double side3){ boolean returnable = false;   if ( side1+ side2 <= side3){ if (side2 + side3 <= side 1){ if (side 1 + side3 <= side2){ returnable = false; }else{ returnable = true; } else { returnable = true; } else { returnable = true; } } return returnable; }```
untested but the logic should work
if statement 1 = true ,then u cant make a triangle , check statement 2
if statement 2 = true ,then u cant make a triangle , check statement 3
if statement 1 = true ,then u cant make a triangle at all , set the return value to false and return it
else if any of the above = false then a triangle can be made , set return value to true and return it

for the other method u will need to import the java.lang.StrictMath to use
Code :

```public static double area(double side1, double side2, double side3){ double s = sqrt( (side1 + side2 + side3) / 2 ) ; s = s * (s - side 1) * ( s - side2 ) * (s - side3 ); return s; }```
i used the formula u gave for it