0
I am very new to the java programming language and I would really like some help understanding what the following code is doing. I have a pretty decent understanding of what is going on within the Main class. I am confused about the output of the code. Can you briefly describe the actual output of code? This is not homework just self study and help me to do another such problem. The exercise can be found here: [1]: http://techgurulab.com/course/java-tutorials/. Thanks!

class box {
    int width;
    int length;
    int volume;

    void volume(int height, int length, int width) {
        volume = width * height * length;
    }
}

class Prameterized_method {
    public static void main(String args[]) {
        box obj = new box();

        obj.height = 1;
        obj.length = 5;
        obj.width = 5;

        obj.volume(3, 2, 1);
        System.out.println(obj.volume);

    }
}
6
  • So what's the output of the code? I mean exactly what are you seeing? Commented Oct 1, 2014 at 21:12
  • 2
    Your title has nothing to do with your question. Can you rephrase it? Commented Oct 1, 2014 at 21:13
  • 1
    And there is no variable with name height in box. And also in java it is customary that class names starts with upper case, i.e. Box instead of box Commented Oct 1, 2014 at 21:15
  • 5
    Just as a side note, that code is horribly bad for several reasons. Probably the worst is the volume method (that takes parameters, with names that are identical to existing instance variables, and doesn't return anything but sets an instance variable)??? Did you copy that example from some tutorial? If so, do yourself a big favor and find another tutorial. Commented Oct 1, 2014 at 21:17
  • And where did you find that code. There is no constructor in Box. volume function does not use instance variables which are set without a constructor Commented Oct 1, 2014 at 21:17

4 Answers 4

3

If your question is whether it will print 25 (1*5*5) or 6(1*2*3), then it will print 6. In the code in your method:

void volume(int height, int length, int width) {
    volume = width * height * length;
}

volume represents this.volume, whereas height, length and width are local to the function. To access the instance variable, you would have to explicitly type this.volume.

Hence this.volume will be the return value of volume(3,2,1) and does not have any relation to this.height, this.length or this.width.

Sign up to request clarification or add additional context in comments.

Comments

0

Since there is not access modifier specified it is package private. Means you can access it from outside and don't need any getters for them.

box obj = new box(); // Creates an object of type box
obj.volume(3, 2, 1); // Call the method to set the volume of the box with the parameters.
System.out.println(obj.volume); // Prints the volume 

The are all connect by the obj object that we created in the first step.

Comments

0

obj.height does not exist it is not defined as a field in the class

Comments

0

The sample of code shown above does as following in following sequence.

The program startes in the

public static void main(String args[]){
    ...
}

Block. Then the code inside is executed. The box obj = new box(); line creates a new object. The object is of type box which is defined above. This means that the object has the fields and methods as defined in the box section class box { ... }

The next few lines of code

obj.height = 1;
obj.length = 5; 
obj.width = 5;
obj.volume(3, 2, 1); 
System.out.println(obj.volume);

When obj.height we are setting the height of the box to 1 When obj.length is called we are setting the length of the box to 5 When obj.width is called we are setting the width of the box.

Please not that the box class does not have a height field, you need to add that

class box{
    int width;
    int heigh;
    int length;
    int volume;

    ...
}

Then we have a method call. This is the obj.volume(3,2,1) call. This takes a box with dimensions 3x2x1 and calculates the volume by multiplying all the values. This is done a bit inefficiently as we are not using the fields we set earlier. You could modify the method as this:

void volume() {
    volume = width * height * length;
}

This method will use the values we already set on the object instead of using the values set by the parameters. This will enfore that the volume will not be calculated with values different from the ones we set earlier.

The last line of code

System.out.printn(obj.volume)

will print out the volume of the box to the screen

1 Comment

i think obj.height will result in an error about not being able to resolve height...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.