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

Thread: Novice with java methods, please help!

  1. #1
    Banned
    Join Date
    Sep 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Novice with java methods, please help!

    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:
      // 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.....
    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();
         }
    }
    Last edited by helloworld922; October 13th, 2009 at 08:08 PM.


  2. #2
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Novice with java methods, please help!

    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
     
    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
    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
    Programming: the art that fights back

Similar Threads

  1. numerical conversion methods..
    By chronoz13 in forum Java SE APIs
    Replies: 12
    Last Post: September 27th, 2009, 05:29 AM