Casale
Casale

Reputation: 39

passing parameters defined in main method to instance methods?

I wrote this program which creates a sphere, cylinder and cone object and calculates volume and surface area but I have some questions.

1) I declared variables r and h inside the main method and then passed them as arguments to the instance methods. Why am I able to do that if the variables are local and declared inside main. They are not static.

2) Is there a benefit to declaring the variables inside main and passing them as arguments or would it be better to declare them as instance variables and initialize them via a constructor.

/*All these classes can be created in a separate file*/
   import java.util.Scanner;


//Creating sphere class
class Sphere
{
//Method to find volume of sphere. Defined inside the Sphere class
double Volume(double r)
{
  return (4.0/3.0) * Math.PI * Math.pow(r,3);  
}


 //Method to find surface of sphere. Defined inside the sphere class
double Surface (double r)
{   
  return 4.0 * Math.PI * Math.pow(r,2);     
 }

 }


//Creating cylinder class
class Cylinder
{
//Methods for cylinder objects
double Volume(double r, double h)

{
       return  Math.PI * Math.pow(r,2) * h;
}

double Surface(double r,double h)

{
        return (2 * Math.PI * r * h) + (2* Math.pow(r,2) * Math.PI);    
}        
}


//Creating cone class
class Cone
{   
     //Methods for cone objects
double Volume(double r, double h)

{
     return  Math.PI * Math.pow(r,2) * (h/3.0);
}


double Surface(double r,double h)    
{
    return Math.PI * r * (r + (Math.sqrt((h*h) + (r*r))));
}    
 }


 //Analogous to our tester class. We could also write this class in a 
separate file along with the main method
public class geometryOOP 
   {

public static void main(String[] args) 
{ 

//Creating Scanner object named in
Scanner in = new Scanner(System.in);
//Prompt
System.out.println("Enter height: ");
//Storing input into h
double h = in.nextDouble();
//Prompt
System.out.println("Enter radius: ");
//Storing input into r
double r = in.nextDouble();






//Creating 3 objects one from each of our classes
Sphere firstsphere = new Sphere();
Cylinder firstcylinder = new Cylinder();
Cone firstcone = new Cone();


//Calling instance methods using dot notation and printing results
System.out.println("The volume of the sphere is : " + Double.toString(firstsphere.Volume(r)));
System.out.println("The surface of the sphere is : " + Double.toString(firstsphere.Surface(r)));
System.out.println("The volume of the cylinder is : " + Double.toString(firstcylinder.Volume(r,h)));
System.out.println("The surface of the cylinder is : " + Double.toString(firstcylinder.Surface(r,h)));
System.out.println("The volume of the cone is : " + Double.toString(firstcone.Volume(r,h)));
System.out.println("The surface of the cone is : " + Double.toString(firstcone.Surface(r,h)));
}


 }

Upvotes: 0

Views: 111

Answers (1)

Ian Mc
Ian Mc

Reputation: 5829

Your current Shape classes can be improved. They contain no state. A class with no state may as well have static methods, where the caller does not need to create instances of the class to invoke those methods.

To demonstrate improved approaches, below are three different Sphere classes.

  1. Sphere1 is similar to your class. To use it, you must create an instance of it by new Sphere1() and it's volume method requires a parameter. This class can be improved (see below Sphere2 and Sphere3).
  2. Sphere2 is a more typical class with state (the radius) and a method which uses the state. Now the volume method does not require a parameter. The state is established using the constructor.
  3. Sphere3 is a stateless class. Here the volume method is static. To use it, there is no requirement to create an instance of the class.

In summary, if your classes have state, then create a constructor and establish the state when you create the object. Then the method calls do not require a state parameter.
If you prefer to have stateless classes, then use static methods; there is no value to creating an instance in this case (using new).

    public class ShapeMain {

    public static void main(String[] args) {

        // Your code uses this approach
        Sphere1 s1 = new Sphere1();
        s1.volume(2.0);

        // An improvement to above
        Sphere2 s2 = new Sphere2(2.0);
        s2.volume();

        // An better alternative if you want stateless classes
        Sphere3.volume(2.0);

    }

    //  Models your class; has NO state 
    static class Sphere1 {
        double volume(double r) {
            return (4.0 / 3.0) * Math.PI * Math.pow(r, 3);
        }
    }

    //  A class with state (radius) 
    static class Sphere2 {
        private final double r;
        public Sphere2(double r) {
            this.r = r;
        }
        double volume() {
            return (4.0 / 3.0) * Math.PI * Math.pow(r, 3);
        }
    }

    //  A class with no state; use a static method to calculate Sphere volume
    static class Sphere3 {
        static double volume(double r) {
            return (4.0 / 3.0) * Math.PI * Math.pow(r, 3);
        }
    }

    }

Upvotes: 1

Related Questions