Results 1 to 10 of 10
  1. #1
    phixion is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default Am I understanding this correctly?

    My logic/questions are in the comments. Thanks in advance.

    Java Code:
    import java.util.Scanner; 
    
    public class BoxTester
    {
        public static void main (String[]args)
        {
            Scanner keyboard = new Scanner(System.in);
            
            double height = 0;
            double width = 0;
            double depth = 0;
            
            while(true)
            {
                System.out.print("Please enter the height, width, depth ie 4 5 6: ");
                
                height = keyboard.nextDouble();
                width = keyboard.nextDouble();
                depth = keyboard.nextDouble();
                
                /**The code below tells the BoxTester program that box is going to be a Box object. 
                 * = new Box calls the Constructor of the Box class and states that there is a new
                 * instance of itself. 
                 * 
                 * box can now be used as a reference to anything we wish to do with this object.
                 * 
                 * Box() accepts the values of the user input ie (height = keyboard.nextDouble()), etc. and
                 * places them in the order specified ie Box(height, width, depth) <-- parameters?.
                 * 
                 * It then passes those variables and their values (arguments?) to .GetSurfaceArea and .GetVolume.
                 */
                
                Box box = new Box(height, width, depth);
                
                System.out.println("Surface Area: " + box.GetSurfaceArea()); //calls the .GetSurfaceArea method from the Box class
                System.out.println("Volume: " + box.GetVolume()); //calls the .GetVolume method from the Box class
            }
        }
    }

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    I don't see any questions.

    -Gary-

  3. #3
    phixion is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    Am I right in assuming that Box(height, width, depth) are parameters? (the height, width, depth part)

    Also, when the user inputs their values for height, width, and depth, is the information passed called arguments?

    Is my explanation in the comments correct?
    Last edited by phixion; 01-27-2011 at 01:40 AM.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    OK, I see your questions now. Your understanding is pretty good. You haven't posted the code for the Box class, but my guess is that the Box() constructor will store the passed values of height, width and depth (yes, those are parameters) in a set of instance variables within the class. Then the GetSurfaceArea() and GetVolume() methods (better style would be to name them getSurfaceArea() and getVolume()) would read the values from those instance variables.

    The terms parameter and argument are used pretty interchangeably, although technically, a method takes parameters, and the Java program takes arguments at the command line. Very few hard-core pedants would call you out on that.

    -Gary-

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    The "things" inside brackets are called parameters or alternatively arguments. In the method signature where you also include the parameter type they are called formal paramaters. In the method call where you enter the parameter only (because the complier already knows the type) they are called actual parameters.

  6. #6
    phixion is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    Here is the Box class. I have a few more questions. (Sorry, ha. I'm just trying to get a firm understanding of this stuff before I move forward).

    Are instance variables essentially attributes of that particular object?

    Below, I'm not completely understanding what is taking place from the methods Box() to SetCube. I'm not 100% sure why I'm constantly declaring the same variables.

    This will by my last post for the week, ha.

    Java Code:
    
    public class Box
    {
        // instance variables
        private double height;
        private double width;
        private double depth;
    
        /**
         * Constructor for objects of class Box
         */
        public Box()
        {
            // initialise instance variables
            height = 0;
            width = 0;
            depth = 0;
        }
        
        public Box(double h, double w, double d)
        {
            height = h;
            width = w;
            depth = d;
        }
        
        public Box(double edgeLength)
        {
            height = edgeLength;
            width = edgeLength;
            depth = edgeLength;
        }
    
        public void SetBox (double h, double w, double d)
        {
            // Modifying method
            height = h;
            width = w;
            depth = d;
        }
        
        public void SetCube(double edgeLength)
        {
            //Modifying method
            height = edgeLength;
            width = edgeLength;
            depth = edgeLength;
        }
        
        public double GetSurfaceArea()
        {
            return (2*height*width) + (2*height*depth) + (2*depth*width);
            
            //double surfacearea;
            //surfacearea = (2*height*width) + (2*height*depth) + (2*depth*width);
            //return surfacearea;
        }
        
        public double GetVolume()
        {
            return height*width*depth;
            
            //double volume;
            //volume = height*width*depth;
            //return volume;
        }
    }

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Quote Originally Posted by phixion View Post
    Are instance variables essentially attributes of that particular object?
    Just like you have a name and I have a different name. So a Person object would have an instance variable called name and each Person object can have a different value stored in that variable.
    Below, I'm not completely understanding what is taking place from the methods Box() to SetCube.
    All those "methods" called Box and not methods but constructors. There are several constructors that allow the user to create a Box object depending upon what data they have. If they only have one value then all dimensions will get the same value. If they have 3 values then you can set height, width and depth separately.
    I'm not 100% sure why I'm constantly declaring the same variables.
    You are not. You are simply assigning the values passed as parameters to the instance variables. Further all those setXXX methods merely mimic the constructors. They allow you tio change values (dimensions) of the Box once it has been created. Just like a Person can change their name.

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    See if this is clearer:
    Java Code:
    public class Box
    {
        // instance variables
        private double height;
        private double width;
        private double depth;
    
        /**
         * Constructor for objects of class Box
         */
        public Box()
        {
            // initialise instance variables
    [COLOR="Blue"]        // setting them all to zero is arbitrary, but to
            // some extent you need to be arbitrary in a
            // default constructor
    [/COLOR]        [COLOR="Blue"]this.[/COLOR]height = 0;
            [COLOR="Blue"]this.[/COLOR]width = 0;
            [COLOR="Blue"]this.[/COLOR]depth = 0;
        }
        
        public Box(double h[COLOR="Blue"]eight[/COLOR], double wi[COLOR="Blue"]dth[/COLOR], double d[COLOR="Blue"]epth[/COLOR])
        {
            [COLOR="Blue"]// setting the instance variables to the values passed as parameters[/COLOR]
            [COLOR="Blue"]this.[/COLOR]height = h[COLOR="Blue"]eight[/COLOR];
            [COLOR="Blue"]this.[/COLOR]width = w[COLOR="Blue"]idth[/COLOR];
            [COLOR="Blue"]this.[/COLOR]depth = d[COLOR="Blue"]epth[/COLOR];
        }
        
        public Box(double edgeLength)
        {
    [COLOR="Blue"]        // making a cubical Box
            // note that if there is no ambiguity, we do not need to specify [I]this.[/I][/COLOR]
            height = edgeLength;
            width = edgeLength;
            depth = edgeLength;
        }
    
        public void [COLOR="Blue"]s[/COLOR]etBox (double h[COLOR="Blue"]eight[/COLOR], double w[COLOR="Blue"]idth[/COLOR], double d[COLOR="Blue"]epth[/COLOR])
        {
            // Modifying method
    [COLOR="Blue"]        // sets instance variables of an already-existing Box to
            // the values passed. Note that it might make more sense
            // to have separate setHeight(), setWidth() and setDepth()
            // methods
    [/COLOR]        [COLOR="Blue"]this.[/COLOR]height = h[COLOR="Blue"]eight[/COLOR];
            [COLOR="Blue"]this.[/COLOR]width = w[COLOR="Blue"]idth[/COLOR];
            [COLOR="Blue"]this.[/COLOR]depth = d[COLOR="Blue"]epth[/COLOR];
        }
        
        public void [COLOR="Blue"]s[/COLOR]etCube(double edgeLength)
        {
            //Modifying method
            height = edgeLength;
            width = edgeLength;
            depth = edgeLength;
        }
        
        public double [COLOR="Blue"]g[/COLOR]etSurfaceArea()
        {
            return (2 * height * width) + (2 * height * depth) + (2 * depth * width);
        }
        
        public double [COLOR="Blue"]g[/COLOR]etVolume()
        {
            return height * width * depth;
        }
    }
    Some developers prefer the approach you started with -- different names for parameters and instance variables. I don't. I think there is generally one good name for a particular piece of data, and we ought to use that one good name instead of making up a slightly worse one to use half the time. Instance variables ought to have the good name because they're used throughout your code, and parameters ought to have the good name, because they're part of the published API that users of your class will need to use. Using the same name everywhere does mean that you have to use the this keyword, but I don't consider that a disadvantage. If anything it makes the code that much clearer.

    Method names should start with a lower-case letter. It makes it easier to distinguish them from constructors.

    -Gary-

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Quote Originally Posted by gcalvin View Post
    different names for parameters and instance variables.
    I'm in the different names camp for 2 reasons.

    1) It saves me having to type this. all over the place. ;)
    2) It avoids problems when people forget to type this. in their code.

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Junky View Post
    I'm in the different names camp for 2 reasons.

    1) It saves me having to type this. all over the place. ;)
    2) It avoids problems when people forget to type this. in their code.
    Well, I respect your opinion, but you're wrong. :p

    -Gary-

Similar Threads

  1. I can't get this to run correctly
    By LostinJavaLand in forum New To Java
    Replies: 4
    Last Post: 07-15-2010, 06:49 AM
  2. Did I do this THREAD correctly?
    By TimHuey in forum New To Java
    Replies: 4
    Last Post: 04-24-2010, 05:54 AM
  3. need help in understanding collection
    By ShinTec in forum Advanced Java
    Replies: 2
    Last Post: 04-24-2010, 02:49 AM
  4. Help on understanding a program
    By newbie225 in forum New To Java
    Replies: 1
    Last Post: 11-10-2009, 12:53 AM
  5. how to scale correctly ?
    By h9h in forum Java 2D
    Replies: 10
    Last Post: 10-29-2009, 07:06 AM

Posting Permissions

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