Overloaded static methods that calculate the area of various shapes, duplicate method warningWhy is the Java main method static?Why can't static methods be abstract in JavaHow do I make the method return type generic?Why can't I define a static method in a Java interface?Static methods in Python?Java area classWhy doesn't Java allow overriding of static methods?Java: when to use static methodsReflection on a static overloaded method using an out parameterFind the largest area of various Shape objects from an ArrayList

What Marvel character has this 'W' symbol?

How can flights operated by the same company have such different prices when marketed by another?

How to innovate in OR

Scam? Checks via Email

Why don't short runways use ramps for takeoff?

How to litter train a cat if both my husband and I work away from home all day?

Should I put my name first or last in the team members list?

How did Biff return to 2015 from 1955 without a lightning strike?

Stationing Callouts using VBScript Labeling in ArcMap?

Move arrows along a contour

A conjectural trigonometric identity

Why does Latex make a small adjustment when I change section color

In the Schrödinger equation, can I have a Hamiltonian without a kinetic term?

What do the novel titles of The Expanse series refer to?

Just how much information should you share with a former client?

Would people understand me speaking German all over Europe?

What is the term for completing a route uncleanly?

UX writing: When to use "we"?

Introduction to the Sicilian

How to find bus maps for Paris outside the périphérique?

PCB design using code instead of clicking a mouse?

What is the range of a Drunken Monk's Redirect attack?

Applying for mortgage when living together but only one will be on the mortgage

When did J.K. Rowling decide to make Ron and Hermione a couple?



Overloaded static methods that calculate the area of various shapes, duplicate method warning


Why is the Java main method static?Why can't static methods be abstract in JavaHow do I make the method return type generic?Why can't I define a static method in a Java interface?Static methods in Python?Java area classWhy doesn't Java allow overriding of static methods?Java: when to use static methodsReflection on a static overloaded method using an out parameterFind the largest area of various Shape objects from an ArrayList






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am reading Starting out with Java and a challenge presented in the book is to: "Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: circles, rectangles and cylinders." The area of a circle only requires only one parameter and there was no issue here so I left that method out. But the area of a rectangle and the area of a cylinder both require two parameters:



public class Area
public static double area(double w, double l)

//Area of rectangle
return l*w;

public static double area(double r, double h)

//Area of a cylinder
return Math.PI * (r*r) *h;




With the above code I get the warning "duplicate method area(double, double) in type Area." I know that if I simply change the type of one of the parameters I won't have this issue, but is this the only way it can be done? I can't have two overloaded static methods with the same parameter list?










share|improve this question


























  • No you can't have, if you pass value like this area(2.0, 2.0) which method out of 2 compiler should infer? So it won't work out.

    – Pradeep Simha
    Mar 26 at 22:25












  • No, you can not overload methods with the same parameter. You can create area(double w, double l) and area(float r, float h) but you have to be very attentive/careful when using those method to pass proper data.

    – Boken
    Mar 26 at 22:32











  • Why the downvote? It seems like a reasonable question to me. The exercise seems a little artificial (see my remarks on naming in my answer) but faced with that exercise, the question looks sound.

    – another-dave
    Mar 26 at 22:50

















0















I am reading Starting out with Java and a challenge presented in the book is to: "Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: circles, rectangles and cylinders." The area of a circle only requires only one parameter and there was no issue here so I left that method out. But the area of a rectangle and the area of a cylinder both require two parameters:



public class Area
public static double area(double w, double l)

//Area of rectangle
return l*w;

public static double area(double r, double h)

//Area of a cylinder
return Math.PI * (r*r) *h;




With the above code I get the warning "duplicate method area(double, double) in type Area." I know that if I simply change the type of one of the parameters I won't have this issue, but is this the only way it can be done? I can't have two overloaded static methods with the same parameter list?










share|improve this question


























  • No you can't have, if you pass value like this area(2.0, 2.0) which method out of 2 compiler should infer? So it won't work out.

    – Pradeep Simha
    Mar 26 at 22:25












  • No, you can not overload methods with the same parameter. You can create area(double w, double l) and area(float r, float h) but you have to be very attentive/careful when using those method to pass proper data.

    – Boken
    Mar 26 at 22:32











  • Why the downvote? It seems like a reasonable question to me. The exercise seems a little artificial (see my remarks on naming in my answer) but faced with that exercise, the question looks sound.

    – another-dave
    Mar 26 at 22:50













0












0








0








I am reading Starting out with Java and a challenge presented in the book is to: "Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: circles, rectangles and cylinders." The area of a circle only requires only one parameter and there was no issue here so I left that method out. But the area of a rectangle and the area of a cylinder both require two parameters:



public class Area
public static double area(double w, double l)

//Area of rectangle
return l*w;

public static double area(double r, double h)

//Area of a cylinder
return Math.PI * (r*r) *h;




With the above code I get the warning "duplicate method area(double, double) in type Area." I know that if I simply change the type of one of the parameters I won't have this issue, but is this the only way it can be done? I can't have two overloaded static methods with the same parameter list?










share|improve this question
















I am reading Starting out with Java and a challenge presented in the book is to: "Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: circles, rectangles and cylinders." The area of a circle only requires only one parameter and there was no issue here so I left that method out. But the area of a rectangle and the area of a cylinder both require two parameters:



public class Area
public static double area(double w, double l)

//Area of rectangle
return l*w;

public static double area(double r, double h)

//Area of a cylinder
return Math.PI * (r*r) *h;




With the above code I get the warning "duplicate method area(double, double) in type Area." I know that if I simply change the type of one of the parameters I won't have this issue, but is this the only way it can be done? I can't have two overloaded static methods with the same parameter list?







java static static-methods






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 23:31







justin

















asked Mar 26 at 22:24









justinjustin

16210 bronze badges




16210 bronze badges















  • No you can't have, if you pass value like this area(2.0, 2.0) which method out of 2 compiler should infer? So it won't work out.

    – Pradeep Simha
    Mar 26 at 22:25












  • No, you can not overload methods with the same parameter. You can create area(double w, double l) and area(float r, float h) but you have to be very attentive/careful when using those method to pass proper data.

    – Boken
    Mar 26 at 22:32











  • Why the downvote? It seems like a reasonable question to me. The exercise seems a little artificial (see my remarks on naming in my answer) but faced with that exercise, the question looks sound.

    – another-dave
    Mar 26 at 22:50

















  • No you can't have, if you pass value like this area(2.0, 2.0) which method out of 2 compiler should infer? So it won't work out.

    – Pradeep Simha
    Mar 26 at 22:25












  • No, you can not overload methods with the same parameter. You can create area(double w, double l) and area(float r, float h) but you have to be very attentive/careful when using those method to pass proper data.

    – Boken
    Mar 26 at 22:32











  • Why the downvote? It seems like a reasonable question to me. The exercise seems a little artificial (see my remarks on naming in my answer) but faced with that exercise, the question looks sound.

    – another-dave
    Mar 26 at 22:50
















No you can't have, if you pass value like this area(2.0, 2.0) which method out of 2 compiler should infer? So it won't work out.

– Pradeep Simha
Mar 26 at 22:25






No you can't have, if you pass value like this area(2.0, 2.0) which method out of 2 compiler should infer? So it won't work out.

– Pradeep Simha
Mar 26 at 22:25














No, you can not overload methods with the same parameter. You can create area(double w, double l) and area(float r, float h) but you have to be very attentive/careful when using those method to pass proper data.

– Boken
Mar 26 at 22:32





No, you can not overload methods with the same parameter. You can create area(double w, double l) and area(float r, float h) but you have to be very attentive/careful when using those method to pass proper data.

– Boken
Mar 26 at 22:32













Why the downvote? It seems like a reasonable question to me. The exercise seems a little artificial (see my remarks on naming in my answer) but faced with that exercise, the question looks sound.

– another-dave
Mar 26 at 22:50





Why the downvote? It seems like a reasonable question to me. The exercise seems a little artificial (see my remarks on naming in my answer) but faced with that exercise, the question looks sound.

– another-dave
Mar 26 at 22:50












4 Answers
4






active

oldest

votes


















3














Both of those methods:



  • are called 'area'

  • take two arguments of type 'double'

so they are indistinguishable. The names you choose for the formal arguments do not factor into the decision, because (aprt from the trivial "because that's how the language works") those names are not written in the source code at the point where a call to area() is made.



Given the problem statement, you've got little choice but to change the type of at least one of the arguments to one of the two overloads. Are integer sizes allowed? Float/double makes me a little nervous: it's too easy to make a mistake.




I understand that this is an exercise in overloading methods in Java, so you've got to follow the problem statement.



However, as a general issue: given a class named Area with a bunch of static area-calculators, IMO it really would be more understandable to name each such area-calculating method to say what it really does. So for example areaOfCircle and areaOfRectangle. Doing different calculations depending on the types of the arguments does not seem to be understandable at a glance the way good names will be.






share|improve this answer


































    1














    The area of the circle could be changed to radius as a double, and pi as a float. That way the compiler will recognize a difference in the circle's method. The idea of the lesson is that , as long as you change the parameters in your methods, you can have multiple methods of the same name which is the point of overloading a method.






    share|improve this answer
































      0














      You can make something like this:



      public class Area 

      // Rectangle
      public static double calculateArea(int width, int length)
      return width * length;


      // Cylinder
      public static double calculateArea(double radius, int height)
      return Math.PI * Math.pow(radius, 2) * height;


      // Circle
      public static double calculateArea(double radius)
      return Math.PI * Math.pow(radius, 2);




      And then call it using:



      Area.calculateArea(2.0); // Call 'Circle' method
      Area.calculateArea(2.0, 3); // Call 'Cylinder' method
      Area.calculateArea(3, 5); // Call 'Rectangle' method





      share|improve this answer
































        0














        Here's a contrived answer that satisfies the requirements for 3 overloaded 'area' methods without having to use float or int where we might prefer double.



        First the Area class. For each shape, we need 3 things: a (inner) class that holds the values that define the shape; a function that delivers an object of that class; and (crucially) an area() method that accepts a single argument, an object of the inner class. It is this last item that satisfies the "overloaded method" requirement.



        class Area {

        static class Circle
        double radius;
        Circle(double r) radius = r;


        static Circle circle(double r)
        return new Circle(r);


        static double area(Circle c)
        return PI * c.radius * c.radius;


        static class Rectangle
        double length, width;
        Rectangle(double l, double w) length = l; width = w;


        static Rectangle rectangle(double l, double w)
        return new Rectangle(l, w);


        static double area(Rectangle r)
        return r.length * r.width;


        static class Cylinder
        double radius, height;
        Cylinder(double r, double h) radius = r; height = h


        static Cylinder cylinder(douvle r, double h)
        return new Cylinder(r, h);


        static double area(Cylinder c)
        return 2 * PI * c.radius * c.height + // side
        PI * c.radius * c.radius * 2; // ends
        // see note!

        ]


        Now, how to use them? These examples show how:



        a1 = Area.area(Area.circle(1));
        a2 = Area.area(Area.rectangle(2,3));
        a3 = Area.area(Area.cylinder(4,5));


        The functions like "Circle circle(...) ... " exist so I don't have to write "new" to create a Circle, etc.



        Pretty ugly, huh?



        Note: your formula for the "area" of a cylinder is actually computing the volume. What exactly do you mean by area of a 3-D solid? If you mean the surface area, then it's the two end circles plus the wrapped-around rectangle that makes the 'side'. Each of the former has area 'pi r^2'; the rectangle has sides '2 pi r' and 'h', thus area '2 pi r h'.






        share|improve this answer





























          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367026%2foverloaded-static-methods-that-calculate-the-area-of-various-shapes-duplicate-m%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Both of those methods:



          • are called 'area'

          • take two arguments of type 'double'

          so they are indistinguishable. The names you choose for the formal arguments do not factor into the decision, because (aprt from the trivial "because that's how the language works") those names are not written in the source code at the point where a call to area() is made.



          Given the problem statement, you've got little choice but to change the type of at least one of the arguments to one of the two overloads. Are integer sizes allowed? Float/double makes me a little nervous: it's too easy to make a mistake.




          I understand that this is an exercise in overloading methods in Java, so you've got to follow the problem statement.



          However, as a general issue: given a class named Area with a bunch of static area-calculators, IMO it really would be more understandable to name each such area-calculating method to say what it really does. So for example areaOfCircle and areaOfRectangle. Doing different calculations depending on the types of the arguments does not seem to be understandable at a glance the way good names will be.






          share|improve this answer































            3














            Both of those methods:



            • are called 'area'

            • take two arguments of type 'double'

            so they are indistinguishable. The names you choose for the formal arguments do not factor into the decision, because (aprt from the trivial "because that's how the language works") those names are not written in the source code at the point where a call to area() is made.



            Given the problem statement, you've got little choice but to change the type of at least one of the arguments to one of the two overloads. Are integer sizes allowed? Float/double makes me a little nervous: it's too easy to make a mistake.




            I understand that this is an exercise in overloading methods in Java, so you've got to follow the problem statement.



            However, as a general issue: given a class named Area with a bunch of static area-calculators, IMO it really would be more understandable to name each such area-calculating method to say what it really does. So for example areaOfCircle and areaOfRectangle. Doing different calculations depending on the types of the arguments does not seem to be understandable at a glance the way good names will be.






            share|improve this answer





























              3












              3








              3







              Both of those methods:



              • are called 'area'

              • take two arguments of type 'double'

              so they are indistinguishable. The names you choose for the formal arguments do not factor into the decision, because (aprt from the trivial "because that's how the language works") those names are not written in the source code at the point where a call to area() is made.



              Given the problem statement, you've got little choice but to change the type of at least one of the arguments to one of the two overloads. Are integer sizes allowed? Float/double makes me a little nervous: it's too easy to make a mistake.




              I understand that this is an exercise in overloading methods in Java, so you've got to follow the problem statement.



              However, as a general issue: given a class named Area with a bunch of static area-calculators, IMO it really would be more understandable to name each such area-calculating method to say what it really does. So for example areaOfCircle and areaOfRectangle. Doing different calculations depending on the types of the arguments does not seem to be understandable at a glance the way good names will be.






              share|improve this answer















              Both of those methods:



              • are called 'area'

              • take two arguments of type 'double'

              so they are indistinguishable. The names you choose for the formal arguments do not factor into the decision, because (aprt from the trivial "because that's how the language works") those names are not written in the source code at the point where a call to area() is made.



              Given the problem statement, you've got little choice but to change the type of at least one of the arguments to one of the two overloads. Are integer sizes allowed? Float/double makes me a little nervous: it's too easy to make a mistake.




              I understand that this is an exercise in overloading methods in Java, so you've got to follow the problem statement.



              However, as a general issue: given a class named Area with a bunch of static area-calculators, IMO it really would be more understandable to name each such area-calculating method to say what it really does. So for example areaOfCircle and areaOfRectangle. Doing different calculations depending on the types of the arguments does not seem to be understandable at a glance the way good names will be.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 26 at 22:41

























              answered Mar 26 at 22:34









              another-daveanother-dave

              1,7342 gold badges2 silver badges11 bronze badges




              1,7342 gold badges2 silver badges11 bronze badges


























                  1














                  The area of the circle could be changed to radius as a double, and pi as a float. That way the compiler will recognize a difference in the circle's method. The idea of the lesson is that , as long as you change the parameters in your methods, you can have multiple methods of the same name which is the point of overloading a method.






                  share|improve this answer





























                    1














                    The area of the circle could be changed to radius as a double, and pi as a float. That way the compiler will recognize a difference in the circle's method. The idea of the lesson is that , as long as you change the parameters in your methods, you can have multiple methods of the same name which is the point of overloading a method.






                    share|improve this answer



























                      1












                      1








                      1







                      The area of the circle could be changed to radius as a double, and pi as a float. That way the compiler will recognize a difference in the circle's method. The idea of the lesson is that , as long as you change the parameters in your methods, you can have multiple methods of the same name which is the point of overloading a method.






                      share|improve this answer













                      The area of the circle could be changed to radius as a double, and pi as a float. That way the compiler will recognize a difference in the circle's method. The idea of the lesson is that , as long as you change the parameters in your methods, you can have multiple methods of the same name which is the point of overloading a method.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 26 at 22:33









                      MGTMGT

                      13710 bronze badges




                      13710 bronze badges
























                          0














                          You can make something like this:



                          public class Area 

                          // Rectangle
                          public static double calculateArea(int width, int length)
                          return width * length;


                          // Cylinder
                          public static double calculateArea(double radius, int height)
                          return Math.PI * Math.pow(radius, 2) * height;


                          // Circle
                          public static double calculateArea(double radius)
                          return Math.PI * Math.pow(radius, 2);




                          And then call it using:



                          Area.calculateArea(2.0); // Call 'Circle' method
                          Area.calculateArea(2.0, 3); // Call 'Cylinder' method
                          Area.calculateArea(3, 5); // Call 'Rectangle' method





                          share|improve this answer





























                            0














                            You can make something like this:



                            public class Area 

                            // Rectangle
                            public static double calculateArea(int width, int length)
                            return width * length;


                            // Cylinder
                            public static double calculateArea(double radius, int height)
                            return Math.PI * Math.pow(radius, 2) * height;


                            // Circle
                            public static double calculateArea(double radius)
                            return Math.PI * Math.pow(radius, 2);




                            And then call it using:



                            Area.calculateArea(2.0); // Call 'Circle' method
                            Area.calculateArea(2.0, 3); // Call 'Cylinder' method
                            Area.calculateArea(3, 5); // Call 'Rectangle' method





                            share|improve this answer



























                              0












                              0








                              0







                              You can make something like this:



                              public class Area 

                              // Rectangle
                              public static double calculateArea(int width, int length)
                              return width * length;


                              // Cylinder
                              public static double calculateArea(double radius, int height)
                              return Math.PI * Math.pow(radius, 2) * height;


                              // Circle
                              public static double calculateArea(double radius)
                              return Math.PI * Math.pow(radius, 2);




                              And then call it using:



                              Area.calculateArea(2.0); // Call 'Circle' method
                              Area.calculateArea(2.0, 3); // Call 'Cylinder' method
                              Area.calculateArea(3, 5); // Call 'Rectangle' method





                              share|improve this answer













                              You can make something like this:



                              public class Area 

                              // Rectangle
                              public static double calculateArea(int width, int length)
                              return width * length;


                              // Cylinder
                              public static double calculateArea(double radius, int height)
                              return Math.PI * Math.pow(radius, 2) * height;


                              // Circle
                              public static double calculateArea(double radius)
                              return Math.PI * Math.pow(radius, 2);




                              And then call it using:



                              Area.calculateArea(2.0); // Call 'Circle' method
                              Area.calculateArea(2.0, 3); // Call 'Cylinder' method
                              Area.calculateArea(3, 5); // Call 'Rectangle' method






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 27 at 1:47









                              Yuri ChervonyiYuri Chervonyi

                              363 bronze badges




                              363 bronze badges
























                                  0














                                  Here's a contrived answer that satisfies the requirements for 3 overloaded 'area' methods without having to use float or int where we might prefer double.



                                  First the Area class. For each shape, we need 3 things: a (inner) class that holds the values that define the shape; a function that delivers an object of that class; and (crucially) an area() method that accepts a single argument, an object of the inner class. It is this last item that satisfies the "overloaded method" requirement.



                                  class Area {

                                  static class Circle
                                  double radius;
                                  Circle(double r) radius = r;


                                  static Circle circle(double r)
                                  return new Circle(r);


                                  static double area(Circle c)
                                  return PI * c.radius * c.radius;


                                  static class Rectangle
                                  double length, width;
                                  Rectangle(double l, double w) length = l; width = w;


                                  static Rectangle rectangle(double l, double w)
                                  return new Rectangle(l, w);


                                  static double area(Rectangle r)
                                  return r.length * r.width;


                                  static class Cylinder
                                  double radius, height;
                                  Cylinder(double r, double h) radius = r; height = h


                                  static Cylinder cylinder(douvle r, double h)
                                  return new Cylinder(r, h);


                                  static double area(Cylinder c)
                                  return 2 * PI * c.radius * c.height + // side
                                  PI * c.radius * c.radius * 2; // ends
                                  // see note!

                                  ]


                                  Now, how to use them? These examples show how:



                                  a1 = Area.area(Area.circle(1));
                                  a2 = Area.area(Area.rectangle(2,3));
                                  a3 = Area.area(Area.cylinder(4,5));


                                  The functions like "Circle circle(...) ... " exist so I don't have to write "new" to create a Circle, etc.



                                  Pretty ugly, huh?



                                  Note: your formula for the "area" of a cylinder is actually computing the volume. What exactly do you mean by area of a 3-D solid? If you mean the surface area, then it's the two end circles plus the wrapped-around rectangle that makes the 'side'. Each of the former has area 'pi r^2'; the rectangle has sides '2 pi r' and 'h', thus area '2 pi r h'.






                                  share|improve this answer































                                    0














                                    Here's a contrived answer that satisfies the requirements for 3 overloaded 'area' methods without having to use float or int where we might prefer double.



                                    First the Area class. For each shape, we need 3 things: a (inner) class that holds the values that define the shape; a function that delivers an object of that class; and (crucially) an area() method that accepts a single argument, an object of the inner class. It is this last item that satisfies the "overloaded method" requirement.



                                    class Area {

                                    static class Circle
                                    double radius;
                                    Circle(double r) radius = r;


                                    static Circle circle(double r)
                                    return new Circle(r);


                                    static double area(Circle c)
                                    return PI * c.radius * c.radius;


                                    static class Rectangle
                                    double length, width;
                                    Rectangle(double l, double w) length = l; width = w;


                                    static Rectangle rectangle(double l, double w)
                                    return new Rectangle(l, w);


                                    static double area(Rectangle r)
                                    return r.length * r.width;


                                    static class Cylinder
                                    double radius, height;
                                    Cylinder(double r, double h) radius = r; height = h


                                    static Cylinder cylinder(douvle r, double h)
                                    return new Cylinder(r, h);


                                    static double area(Cylinder c)
                                    return 2 * PI * c.radius * c.height + // side
                                    PI * c.radius * c.radius * 2; // ends
                                    // see note!

                                    ]


                                    Now, how to use them? These examples show how:



                                    a1 = Area.area(Area.circle(1));
                                    a2 = Area.area(Area.rectangle(2,3));
                                    a3 = Area.area(Area.cylinder(4,5));


                                    The functions like "Circle circle(...) ... " exist so I don't have to write "new" to create a Circle, etc.



                                    Pretty ugly, huh?



                                    Note: your formula for the "area" of a cylinder is actually computing the volume. What exactly do you mean by area of a 3-D solid? If you mean the surface area, then it's the two end circles plus the wrapped-around rectangle that makes the 'side'. Each of the former has area 'pi r^2'; the rectangle has sides '2 pi r' and 'h', thus area '2 pi r h'.






                                    share|improve this answer





























                                      0












                                      0








                                      0







                                      Here's a contrived answer that satisfies the requirements for 3 overloaded 'area' methods without having to use float or int where we might prefer double.



                                      First the Area class. For each shape, we need 3 things: a (inner) class that holds the values that define the shape; a function that delivers an object of that class; and (crucially) an area() method that accepts a single argument, an object of the inner class. It is this last item that satisfies the "overloaded method" requirement.



                                      class Area {

                                      static class Circle
                                      double radius;
                                      Circle(double r) radius = r;


                                      static Circle circle(double r)
                                      return new Circle(r);


                                      static double area(Circle c)
                                      return PI * c.radius * c.radius;


                                      static class Rectangle
                                      double length, width;
                                      Rectangle(double l, double w) length = l; width = w;


                                      static Rectangle rectangle(double l, double w)
                                      return new Rectangle(l, w);


                                      static double area(Rectangle r)
                                      return r.length * r.width;


                                      static class Cylinder
                                      double radius, height;
                                      Cylinder(double r, double h) radius = r; height = h


                                      static Cylinder cylinder(douvle r, double h)
                                      return new Cylinder(r, h);


                                      static double area(Cylinder c)
                                      return 2 * PI * c.radius * c.height + // side
                                      PI * c.radius * c.radius * 2; // ends
                                      // see note!

                                      ]


                                      Now, how to use them? These examples show how:



                                      a1 = Area.area(Area.circle(1));
                                      a2 = Area.area(Area.rectangle(2,3));
                                      a3 = Area.area(Area.cylinder(4,5));


                                      The functions like "Circle circle(...) ... " exist so I don't have to write "new" to create a Circle, etc.



                                      Pretty ugly, huh?



                                      Note: your formula for the "area" of a cylinder is actually computing the volume. What exactly do you mean by area of a 3-D solid? If you mean the surface area, then it's the two end circles plus the wrapped-around rectangle that makes the 'side'. Each of the former has area 'pi r^2'; the rectangle has sides '2 pi r' and 'h', thus area '2 pi r h'.






                                      share|improve this answer















                                      Here's a contrived answer that satisfies the requirements for 3 overloaded 'area' methods without having to use float or int where we might prefer double.



                                      First the Area class. For each shape, we need 3 things: a (inner) class that holds the values that define the shape; a function that delivers an object of that class; and (crucially) an area() method that accepts a single argument, an object of the inner class. It is this last item that satisfies the "overloaded method" requirement.



                                      class Area {

                                      static class Circle
                                      double radius;
                                      Circle(double r) radius = r;


                                      static Circle circle(double r)
                                      return new Circle(r);


                                      static double area(Circle c)
                                      return PI * c.radius * c.radius;


                                      static class Rectangle
                                      double length, width;
                                      Rectangle(double l, double w) length = l; width = w;


                                      static Rectangle rectangle(double l, double w)
                                      return new Rectangle(l, w);


                                      static double area(Rectangle r)
                                      return r.length * r.width;


                                      static class Cylinder
                                      double radius, height;
                                      Cylinder(double r, double h) radius = r; height = h


                                      static Cylinder cylinder(douvle r, double h)
                                      return new Cylinder(r, h);


                                      static double area(Cylinder c)
                                      return 2 * PI * c.radius * c.height + // side
                                      PI * c.radius * c.radius * 2; // ends
                                      // see note!

                                      ]


                                      Now, how to use them? These examples show how:



                                      a1 = Area.area(Area.circle(1));
                                      a2 = Area.area(Area.rectangle(2,3));
                                      a3 = Area.area(Area.cylinder(4,5));


                                      The functions like "Circle circle(...) ... " exist so I don't have to write "new" to create a Circle, etc.



                                      Pretty ugly, huh?



                                      Note: your formula for the "area" of a cylinder is actually computing the volume. What exactly do you mean by area of a 3-D solid? If you mean the surface area, then it's the two end circles plus the wrapped-around rectangle that makes the 'side'. Each of the former has area 'pi r^2'; the rectangle has sides '2 pi r' and 'h', thus area '2 pi r h'.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Mar 28 at 1:10

























                                      answered Mar 28 at 1:04









                                      another-daveanother-dave

                                      1,7342 gold badges2 silver badges11 bronze badges




                                      1,7342 gold badges2 silver badges11 bronze badges






























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367026%2foverloaded-static-methods-that-calculate-the-area-of-various-shapes-duplicate-m%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript