Results 1 to 5 of 5

Thread: basic doubt

  1. #1
    Senior Member
    Join Date
    Dec 2001
    Posts
    134

    basic doubt

    Hello guys i have some very basic doubt in java class, i was just playing with the very simple and basic.
    what i was trying to do it to create the object of a class "Boxers" inside itself and then using the object, i was try to call the method of the class. but it was not working.

    Can anyone et me know where am i going wrong, or what am i not following. any sort of information will be appreciated. the following is the code.
    -----------------------------------------------------------------------------------------------------------------

    class Boxers
    {
    public static void main(String args[])
    {
    double width=0.0;
    double height=0.0;
    double depth=0.0;



    Boxers box1=new Boxers();
    Boxers box2=new Boxers();

    box1.width=27;
    box1.height=23;
    box1.depth=43;

    box2.width=21;
    box2.height=24;
    box2.depth=32;

    box1.volume();
    box2.volume();

    void volume()
    {

    System.out.println("the volume of the box is: "+(width*height*depth));

    }

    }
    }
    -------------------------------------------------------------------------------------------------------------------
    Thanks regrads
    Harbir
    U get What U pay for.

  2. #2
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    You didn't provide an access level for your method (public, private, protected or package). I added 'private,' since nothing outside the class accesses it for now.

    Furthermore, you should exclude the characteristics of your object from the static main method (put the attributes and the method outside the main-method).

    This should work for you:

    Code:
    class Boxers
    {
        double width    = 0.0;
        double height   = 0.0;
        double depth    = 0.0;
        
        private void volume()
        {
            System.out.println("the volume of the box is: "+(width*height*depth));
        }
        
        public static void main(String args[])
        {
            Boxers box1 = new Boxers();
            Boxers box2 = new Boxers();
    
            box1.width  = 27;
            box1.height = 23;
            box1.depth  = 43;
    
            box2.width  = 21;
            box2.height = 24;
            box2.depth  = 32;
    
            box1.volume();
            box2.volume();
        }
    }
    I wish to express my gratitude to the people of Italy. Thank you for inventing pizza.

  3. #3
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    Your code looked a little senseless in places. I rewrote it a little, to add some organisation. Have a look at these suggestions:

    Why are you creating doubles for your object variables? If you are using only non-broken values in your calculations, Integers (int) will do.

    Manipulationg object variables directly is dangerous. You should create functions to do this for you, so that you can always keep track of how your variables are changed. You shouldn't leave your object variables open to the public. I declared them 'private' and added a constructor to set them.

    Instead of using a 'void' function to print the volume, I changed that function so that it returns the volume as a number. This way, you can use the same class again, even if you want to do something else with the volume besides printing it (use it in another calculation, for example).

    Hope this helps a little.

    Code:
    class Boxers
    {
        private int width    = 0;
        private int height   = 0;
        private int depth    = 0;
    
        public Boxers(int aWidth, int aHeight, int depth)
        {
            /* These three lines all do the same thing:
             * assign the specified attribute to the object variable
             */
            
            // attribute    = object variable  
            this.width      = aWidth;
            height          = aHeight;
            this.depth      = depth;
        }
        
        private int getVolume()
        {
            return (width * height * depth);
        }
        
        public static void main(String args[])
        {
            Boxers box1 = new Boxers(27,23,43);
            Boxers box2 = new Boxers(21,24,32);
    
            System.out.println("the volume of the box is: " + box1.getVolume());
            System.out.println("the volume of the box is: " + box2.getVolume());
        }
    }
    I wish to express my gratitude to the people of Italy. Thank you for inventing pizza.

  4. #4
    Senior Member
    Join Date
    Dec 2001
    Posts
    134

    I followed

    Thanx Buddy, u reallu have veen a good and a quality help, i really appreciate it.
    Regards
    Harbir
    U get What U pay for.

  5. #5
    Junior Member
    Join Date
    Oct 2004
    Posts
    8

    Cool

    Hey i think the only mistake in ur code was tat u didnt declared the attributes as the class members rather it was a part of the main's local variables....



    u could use the code like this::


    class Boxers
    {

    double width=0.0;
    double height=0.0;
    double depth=0.0;



    public static void main(String args[])
    {

    Boxers box1=new Boxers();
    Boxers box2=new Boxers();

    box1.width=27;
    box1.height=23;
    box1.depth=43;

    box2.width=21;
    box2.height=24;
    box2.depth=32;

    box1.volume();
    box2.volume();

    void volume()
    {

    System.out.println("the volume of the box is: "+(width*height*depth));

    }

    }
    }
    Knowledge needs to be acquired

Posting Permissions

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