Results 1 to 15 of 15
Like Tree2Likes
  • 1 Post By superhaNds
  • 1 Post By JosAH

Thread: Problem returning in a method

  1. #1
    therealvasile is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Default Problem returning in a method

    I need help understanding how to fix this to return the perimeter.

    Java Code:
    public class Triangle 
    {  
        public Triangle(double side1, double side2, double side3)
        {
            if (side1 <= 0 || side2 <= 0 || side3 <= 0)
            {
                System.exit(0);
            }
        }
        
        public double getPerimeter (double side1, double side2, double side3 )
        {
           double Perimeter = side1 + side2 + side3;
           return Perimeter;
        }
        
        public static void main(String[] args) 
        {
            Triangle t = new Triangle(3,4,5);
            t.getPerimeter(3,4,5);
            
        }
    }
    Last edited by therealvasile; 07-12-2013 at 12:57 AM.

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Problem returning in a method

    what is the problem?

  3. #3
    therealvasile is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Default Re: Problem returning in a method

    Quote Originally Posted by superhaNds View Post
    what is the problem?
    It doesnt return anything

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Problem returning in a method

    First, you need to create instance fields of side1, side2, and side3 and then initialize them in your constructor.
    These fields will then be used by the getPerimeter method.

    The getPerimeter method returns a double so you need to assign it to a double value like this:

    double perimeter = getPerimeter(3,4,5);

    Then do something with it. Perhaps print it out.

    I recommend you buy a book on Java to assist in learning the language.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Problem returning in a method

    It does, but you do not use the value. Calling the get method on its own doesn't accomplish anything.
    You can print it: e.g System.out.print(t.getPerimeter); or assign it to another variable double p = t.getPerimeter(); or anything.

  6. #6
    therealvasile is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Default Re: Problem returning in a method

    Quote Originally Posted by jim829 View Post
    First, you need to create instance fields of side1, side2, and side3 and then initialize them in your constructor.
    These fields will then be used by the getPerimeter method.

    The getPerimeter method returns a double so you need to assign it to a double value like this:

    double perimeter = getPerimeter(3,4,5);

    Then do something with it. Perhaps print it out.

    I recommend you buy a book on Java to assist in learning the language.

    Regards,
    Jim
    i have the cay big java book but it isnt always so clear

  7. #7
    therealvasile is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Default Re: Problem returning in a method

    Quote Originally Posted by superhaNds View Post
    It does, but you do not use the value. Calling the get method on its own doesn't accomplish anything.
    You can print it: e.g System.out.print(t.getPerimeter); or assign it to another variable double p = t.getPerimeter(); or anything.
    does return not print it?

  8. #8
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Problem returning in a method

    returning a value doesn't mean printing it.
    therealvasile likes this.

  9. #9
    therealvasile is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Default Re: Problem returning in a method

    Quote Originally Posted by superhaNds View Post
    returning a value doesn't mean printing it.
    so does return have a purpose ?
    Last edited by therealvasile; 07-12-2013 at 01:10 AM.

  10. #10
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Problem returning in a method

    Absolutely, it is vital. When you go deeper into classes, objects you will see that the basic methods of a class are get methods that return their data fields and set methods that mutate the fields.
    The values are important and are used in many different ways. Passed on as arguments to other methods to return other values, displayed in GUI's, printing is just for testing mostly/ This is not much but you should learn the basics.

    Consider a student and a university class, university has a method to check a student ID to confirm that he/she is a student and you need the
    student ID to pass as an argument. The ID is a data field of student and using the getID method of student, you pass the student's ID to the University's method.
    Last edited by superhaNds; 07-12-2013 at 01:21 AM.

  11. #11
    therealvasile is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Default Re: Problem returning in a method

    is this correct then?

    Java Code:
    public class Triangle 
    {
        double side1;
        double side2;
        double side3;
        
        public Triangle(double s1, double s2, double s3)
        {
            side1 = s1;
            side2 = s2;
            side3 = s3;
            if ( side1 <= 0 || side2 <= 0 || side3 <= 0 )
            {
                System.exit(0);
            }
        }
        
        public double getPerimeter ()
        {
           double Perimeter = side1 + side2 + side3;
           return Perimeter;
        }
        
        
        
        public static void main(String[] args) 
        {
            double s1 = 3;
            double s2 = 4;
            double s3 = 5;
            Triangle t = new Triangle(s1,s2,s3);
            double Perim= t.getPerimeter();
            System.out.println(Perim);
        }
    }

  12. #12
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Problem returning in a method

    getPerimeter() is fine.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Problem returning in a method

    Yes, that is it exactly. One more thing you may not be aware of. You did not have to change the names of your constructor parameters. When you want to differentiate between a local variable or method/constructor parameter you can employ the 'this' keyword.

    Java Code:
    int side1; // instance fields
    int side2;
    int side3;
    
    public Triangle(int side1, int side2, int side3) {
       this.side1 = side1;
       this.side2 = side2;
       this.side3 = side3;
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Problem returning in a method

    Also note that by convention, variable names start with a lowercase letter.
    Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problem returning in a method

    Quote Originally Posted by therealvasile View Post
    Java Code:
        public Triangle(double side1, double side2, double side3)
        {
            if (side1 <= 0 || side2 <= 0 || side3 <= 0)
            {
                System.exit(0);
            }
        }
    That test is not sufficient; suppose you pass side lengths 1, 10, 2; those three sides can never form a triangle; use the triangle inequality instead: for three sides a, b and c, the sides have to be non-negative and a+b >= c, a+c >= b and b+c >= a

    kind regards,

    Jos
    khairul likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Method not returning a value
    By mathwiz7 in forum New To Java
    Replies: 1
    Last Post: 11-28-2012, 04:07 PM
  2. Replies: 1
    Last Post: 12-15-2011, 04:02 AM
  3. Replies: 4
    Last Post: 03-29-2011, 07:34 AM
  4. My method keeps returning 0
    By ToolJob in forum New To Java
    Replies: 11
    Last Post: 03-27-2011, 05:22 PM
  5. Returning Value from a method
    By Mirix in forum New To Java
    Replies: 12
    Last Post: 06-01-2010, 09:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •