14 Replies Latest reply: Oct 25, 2012 10:06 AM by 968909 RSS

    Java Method Calling - Passing Values

    968909
      Below is the code that I wrote, and it runs. But how do I pass the two variables, length and width to my methods so that they will calcuate.
      It prompts to input length and width, but comes back with area and perimeter as 0.0.

      I have bolded where I think I need to pass the varaibles to.

      thank you in advance.

      import java.util.Scanner;

      public class Square {

           public static void main(String [] args){
                Scanner myScanner = new Scanner(System.in);
                
                //double length=0;
                //double width=0;
           
                System.out.print("Enter the length: ");
                double length = myScanner.nextDouble();
                
                System.out.print("Enter the width: ");
                double width = myScanner.nextDouble();
                
                
                area space = new area();
           double aTotalArea = space.totalArea();
                System.out.println("The area is: " + aTotalArea);
                          
                perimeter boundary = new perimeter();
           double aTotalPerimeter = boundary.totalPerimeter();
                System.out.println("The perimeter is: " + aTotalPerimeter);
           }
      }

      public class area {
           
           double length;
           double width;
           
           *double totalArea () {*
           double zone = length * width;
           return zone;
           
           }
      }
      public class perimeter {
           
           double length;
           double width;
           
           *double totalPerimeter(){*
           double border = (length * width) + (length * width);
           return border;
           }
      }

      Edited by: SunshineJava on Oct 18, 2012 10:25 AM
        • 1. Re: Java Method Calling - Passing Values
          abillconsl
          You first need to add the parameters to the method signature:
          double totalArea(double length, double width)
          Then you put the arguments into the call:
          double d = TotalArea(side, side);  //since all sides of a square are the same, you don't need separate length and width in the caller.
          BTW, if totalPerimiter is supposed to get the perimeter of a rectangle, the calculation is incorrect. It should be (2*length)+(2*width). Of course for a square it could just be 4*side, but no doubt you want to make your totalArea and totalPerimiter methods capable of handling rectangles other than squares alone.

          Edited by: abillconsl on Oct 18, 2012 2:26 PM
          • 2. Re: Java Method Calling - Passing Values
            968909
            Thank you -

            I updated my method:

            public class perimeter {
                 
                 double width;
                 double length;
                 
                 
                 double totalPerimeter(double length, double width){
                 double border = (length*2) + (width*2);
                 return border;
                 }
            }
            Can you help me with where does "double d = TotalArea(side, side);" go. You said in the call.
            I was looking at double aTotalArea = space.totalArea(); as the call so I am confused.

            please and thank you
            • 3. Re: Java Method Calling - Passing Values
              abillconsl
              Same place - I just took liberties with the names, and I forgot that object reference - sorry.

              Edited by: abillconsl on Oct 19, 2012 10:12 AM
              • 4. Re: Java Method Calling - Passing Values
                968909
                public class perimeter {
                double width;
                double length;
                double totalPerimeter(double length, double width){
                double border = (length*2) + (width*2);
                return border;
                }
                }

                Can you help me with where does "double d = TotalArea(side, side);" go. You said in the call.
                I was looking at double aTotalArea = space.totalArea(); as the call so I am confused.

                When you said same place - same place as what? I have tried to add your suggestion but am unable to make it work.

                Any help would be appreciated.

                please and thank you

                Edited by: SunshineJava on Oct 24, 2012 4:54 PM
                • 5. Re: Java Method Calling - Passing Values
                  965123
                            
                            area space = new area();
                       double aTotalArea = space.totalArea();
                            System.out.println("The area is: " + aTotalArea);
                       
                  Let's change it to appear like this:
                  area space = new area();
                  space.width = width;
                  space.length = length;
                  double aTotalArea = space.totalArea();
                  System.out.println("The area is: " + aTotalArea);
                  You can do the same when you use the perimeter class as well.
                  • 6. Re: Java Method Calling - Passing Values
                    970581
                    area space = new area();
                    double aTotalArea = space.totalArea();
                    System.out.println("The area is: " + aTotalArea);


                    In the above code, you have created an instance of a class area, gave it a name as 'space'. Using that instance, you have made a call to the method totalArea(). This will make a call to the totalArea() method of the area class.

                    double totalArea () {
                    double zone = length * width;
                    return zone;
                    }

                    Now you are accepting the values for length and width using the Square class, so you need to pass the values of these variables to the totalArea() method of the area class. That's the reason why you need to change you method of the area class as

                    double totalArea (double length, double width) {
                    double zone = length * width;
                    return zone;
                    }

                    In this way, you are passing the values to the method. (same is with the perimeter class)
                    • 7. Re: Java Method Calling - Passing Values
                      TPD-Opitz
                      Kamal Wickramanayake wrote:
                      Let's change it to appear like this:
                      area space = new area();
                      space.width = width;
                      space.length = length;
                      ...
                      This is a bad advice!

                      Why?
                      It violates both information hiding and responsibility of concerns.
                      This way the caller knows how the <tt>area</tt> stores its data, and even worse it can change it!

                      There where two alternatives to solve this. Adding parameters to <tt>totalArea()</tt> has already been sugested.

                      In this particular situation I would prefer a parametrized constructor because an area should not change:
                      class Area { // stick to java naming conventions!
                        privat final double _length;
                        privat final double _width;
                      
                        public Area(double length, double width){
                           _length=length;
                           _width=width;
                        }
                      ...
                      }
                      bye
                      TPD
                      • 8. Re: Java Method Calling - Passing Values
                        Kayaman
                        TPD Opitz-Consulting com wrote:
                        class Area { // stick to java naming conventions!
                        privat final double _length;
                        privat final double _width;
                        I don't recall the "almost hungarian wart" being part of java naming conventions either.
                        • 9. Re: Java Method Calling - Passing Values
                          TPD-Opitz
                          Kayaman wrote:
                          TPD Opitz-Consulting com wrote:
                          class Area { // stick to java naming conventions!
                          privat final double _length;
                          privat final double _width;
                          I don't recall the "almost hungarian wart" being part of java naming conventions either.
                          So what is your prefered way to distinguish member variables from method parameters or method local variables?

                          bye
                          TPD
                          • 10. Re: Java Method Calling - Passing Values
                            965123
                            TPD Opitz-Consulting com wrote:
                            Kamal Wickramanayake wrote:
                            Let's change it to appear like this:
                            area space = new area();
                            space.width = width;
                            space.length = length;
                            ...
                            This is a bad advice!

                            Why?
                            It violates both information hiding and responsibility of concerns.
                            This way the caller knows how the <tt>area</tt> stores its data, and even worse it can change it!
                            Sometimes, it's best not to comment.
                            • 11. Re: Java Method Calling - Passing Values
                              TPD-Opitz
                              Kamal Wickramanayake wrote:
                              Sometimes, it's best not to comment.
                              you're telling me this why?

                              bye
                              TPD
                              • 12. Re: Java Method Calling - Passing Values
                                gimbal2
                                TPD Opitz-Consulting com wrote:
                                So what is your prefered way to distinguish member variables from method parameters or method local variables?
                                EDIT: you know what, never mind. This thread is looking kind of volatile right now, don't want to add coals to the fire.
                                • 13. Re: Java Method Calling - Passing Values
                                  abillconsl
                                  SunshineJava wrote:
                                  When you said same place - same place as what? I have tried to add your suggestion but am unable to make it work.

                                  Any help would be appreciated.

                                  please and thank you
                                  I meant, the same place in your code that you are currently calling it. Simply replace the no argument call with the one that has the arguments.
                                  • 14. Re: Java Method Calling - Passing Values
                                    968909
                                    Thank you for all the great ideas and suggestions. I used some, ignored others but was finally able to get it to work. Its such a great feeling when it runs how you want it to!!

                                    Have a great day!!

                                    FInal Product:

                                    import java.util.Scanner;

                                    public class square_new {

                                         public static void main(String [] args){
                                              Scanner myScanner = new Scanner(System.in);

                                              System.out.print("Enter the length: ");
                                              double length = myScanner.nextDouble();

                                              System.out.print("Enter the width: ");
                                              double width = myScanner.nextDouble();

                                              area space = new area();
                                              double aTotalArea = space.totalArea(length, width);
                                              System.out.println("The area is: " + aTotalArea);


                                              perimeter boundary = new perimeter();
                                              double aTotalPerimeter = boundary.totalPerimeter(length, width);
                                              System.out.println("The perimeter is: " + aTotalPerimeter);
                                         }
                                    }

                                         public class area {

                                              double totalArea(double length, double width) {
                                                   double zone = length * width;
                                                   return zone;
                                              }
                                         }

                                         public class perimeter {

                                              double totalPerimeter(double length, double width){
                                                   double border = (2 * length) + (2 * width);
                                                   return border;
                                              }
                                              
                                         }