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

Thread: Passing a boolean value through a method?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Passing a boolean value through a method?

    this is the exercise that I am currently working on:
    Write a test program that reads three sides for a triangle and computes the area if the input is valid. Otherwise, it displays that the input is invalid.

    package Chapter05;
    import java.util.Scanner;
    public class MyTriangle {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
     
            System.out.print("Enter three points for a triangle: ");
            double side1 = input.nextDouble();
            double side2 = input.nextDouble();
            double side3 = input.nextDouble();
            area(side1,side2,side3);
        }
     
        public static boolean isValid(double side1, double side2, double side3) {
            if(side1 + side2 > side3 
               && side1 + side3 > side2
               && side2 + side3 > side1) {
               return true;
            }
            else if(side1 + side2 < side3 
                    || side1 + side3 < side2
                    || side2 + side3 < side1) {
                System.out.println("Invalid");
            }
            return false;
        }
     
        public static double area(double side1, double side2, double side3) {
            double area = 0;
            if(isValid(side1,side2,side3)) {
            double S = (side1 + side2 + side3)/2;
            area = Math.sqrt(S * (S - side1)* (S - side2) * (S - side3));
     
            }
     
            return area;
        }   
    }

    How do I pass the value in the method isValid to run the method Area? When ever I run my code and put in the values for Side1,Side2, and Side3 my program just says Build Successful


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Passing a boolean value through a method?

    Questions? Problems?

  3. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    Mr.JavaNoob (November 29th, 2013)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Passing a boolean value through a method?

    You use an 'if' statement:
    if ( resultsOfFirstMethod )
    {
        area();
    }

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Mr.JavaNoob (November 29th, 2013)

  6. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Passing a boolean value through a method?

    Quote Originally Posted by GregBrannon View Post
    You use an 'if' statement:
    if ( resultsOfFirstMethod )
    {
        area();
    }

    Sorry, I don't quite understand

  7. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Passing a boolean value through a method?

    Expanding it for you:
        if ( isValid( side1, side2, side3 ) )
        {
            area(side1,side2,side3);
        }

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    Mr.JavaNoob (November 29th, 2013)

  9. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Passing a boolean value through a method?

    Thanks Greg I got it. looks like I was forgetting System.out.println(area); Thanks a lot.

  10. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Passing a boolean value through a method?

    Ahhh. Glad to help. You're welcome.

Similar Threads

  1. Boolean method help!
    By Scorks in forum Object Oriented Programming
    Replies: 3
    Last Post: April 2nd, 2013, 12:33 PM
  2. [SOLVED] Passing 2D array to method
    By drgroove777 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 24th, 2012, 02:08 AM
  3. passing boolean value issue
    By tr_tech in forum Member Introductions
    Replies: 1
    Last Post: June 17th, 2011, 09:13 AM
  4. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  5. passing string into method
    By tabutcher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 08:43 AM