Object references in an array

Can anyone help with a java problem? I'm creating an array of objects with the following code:

Boxes[] theArray = new Boxes[10];

This array theArray contains 10 cells with each pointing to an object of the Boxes class. The Boxes class just creates a cardboard box with a length, width, and height; each dimension is an instance variable of the Boxes class.

In the Boxes class I have a method that will calculate the volume of a box. The code is as follows:

public void calculateVolume (Boxes b) {
volume = (this.getLength() * this.getWidth() * this.getHeight());
this.setVolume(volume);
}

How do I use the array to call this method which lives in the boxes class? For example, I want to call theArray[1].calculateVolume(theArray[1]); to calculate the volume of a box in theArray cell#1, but I keep getting a compiler error "cannot find symbol". I have imported the Boxes class into the class that contains my main method where I created the array. What the heck am I doing wrong?

Comments

  • : Can anyone help with a java problem? I'm creating an array of
    : objects with the following code:
    :
    : Boxes[] theArray = new Boxes[10];
    :
    : This array theArray contains 10 cells with each pointing to an
    : object of the Boxes class. The Boxes class just creates a cardboard
    : box with a length, width, and height; each dimension is an instance
    : variable of the Boxes class.
    :
    : In the Boxes class I have a method that will calculate the volume of
    : a box. The code is as follows:
    :
    : public void calculateVolume (Boxes b) {
    : volume = (this.getLength() * this.getWidth() * this.getHeight());
    : this.setVolume(volume);
    : }
    :
    : How do I use the array to call this method which lives in the boxes
    : class? For example, I want to call
    : theArray[1].calculateVolume(theArray[1]); to calculate the volume of
    : a box in theArray cell#1, but I keep getting a compiler error
    : "cannot find symbol". I have imported the Boxes class into the
    : class that contains my main method where I created the array. What
    : the heck am I doing wrong?
    :
    - Is calculateVolume() declared within the Boxes class?
    - Is the Boxes class public?
    - Is theArray variable visible to the code, which calls calculateVolume()?
    - If you call calculateVolume from main(), is theArray static?

    Note: You don't need the parameter, since it is unused.
  • : : Can anyone help with a java problem? I'm creating an array of
    : : objects with the following code:
    : :
    : : Boxes[] theArray = new Boxes[10];
    : :
    : : This array theArray contains 10 cells with each pointing to an
    : : object of the Boxes class. The Boxes class just creates a cardboard
    : : box with a length, width, and height; each dimension is an instance
    : : variable of the Boxes class.
    : :
    : : In the Boxes class I have a method that will calculate the volume of
    : : a box. The code is as follows:
    : :
    : : public void calculateVolume (Boxes b) {
    : : volume = (this.getLength() * this.getWidth() * this.getHeight());
    : : this.setVolume(volume);
    : : }
    : :
    : : How do I use the array to call this method which lives in the boxes
    : : class? For example, I want to call
    : : theArray[1].calculateVolume(theArray[1]); to calculate the volume of
    : : a box in theArray cell#1, but I keep getting a compiler error
    : : "cannot find symbol". I have imported the Boxes class into the
    : : class that contains my main method where I created the array. What
    : : the heck am I doing wrong?
    : :
    : - Is calculateVolume() declared within the Boxes class?
    : - Is the Boxes class public?
    : - Is theArray variable visible to the code, which calls
    : calculateVolume()?
    : - If you call calculateVolume from main(), is theArray static?
    :
    : Note: You don't need the parameter, since it is unused.


    Thanks for the help. Below is the entire Boxes class. It is short and very simple.

    [code]
    public class Boxes {
    private int length;
    private int width;
    private int height;
    private int volume;

    public void calculateVolume () {
    volume = (this.getLength() * this.getWidth() * this.getHeight());
    this.setVolume(volume);
    }
    public void setVolume(int myVolume) {
    volume = myVolume;
    }
    public int getLength () {
    return length;
    }
    public int getWidth () {
    return width;
    }
    public int getHeight() {
    return height;
    }
    public Boxes () {
    length = 1;
    width = 1;
    height = 1;
    }
    }
    [/code]
    I'm trying to call the calculateVolume method with a Boxes object which is created with the following code:

    Boxes[] theArray = new Boxes[10];

    the way that I'm trying to call the method is :

    theArray[1].calculateVolume();

    theArray is created in another class other than Boxes which would import Boxes. I know this version of Boxes doesn't show an import statement, but the complete class does include it. I have just simplified the Boxes class for this question.

    Why does the compiler keep telling me that it "cannot find symbol" when I try to call the calculate Volume method? Can you offer any suggestions for solving the problem?
  • : : : Can anyone help with a java problem? I'm creating an array of
    : : : objects with the following code:
    : : :
    : : : Boxes[] theArray = new Boxes[10];
    : : :
    : : : This array theArray contains 10 cells with each pointing to an
    : : : object of the Boxes class. The Boxes class just creates a cardboard
    : : : box with a length, width, and height; each dimension is an instance
    : : : variable of the Boxes class.
    : : :
    : : : In the Boxes class I have a method that will calculate the volume of
    : : : a box. The code is as follows:
    : : :
    : : : public void calculateVolume (Boxes b) {
    : : : volume = (this.getLength() * this.getWidth() * this.getHeight());
    : : : this.setVolume(volume);
    : : : }
    : : :
    : : : How do I use the array to call this method which lives in the boxes
    : : : class? For example, I want to call
    : : : theArray[1].calculateVolume(theArray[1]); to calculate the volume of
    : : : a box in theArray cell#1, but I keep getting a compiler error
    : : : "cannot find symbol". I have imported the Boxes class into the
    : : : class that contains my main method where I created the array. What
    : : : the heck am I doing wrong?
    : : :
    : : - Is calculateVolume() declared within the Boxes class?
    : : - Is the Boxes class public?
    : : - Is theArray variable visible to the code, which calls
    : : calculateVolume()?
    : : - If you call calculateVolume from main(), is theArray static?
    : :
    : : Note: You don't need the parameter, since it is unused.
    :
    :
    : Thanks for the help. Below is the entire Boxes class. It is short
    : and very simple.
    :
    : [code]:
    : public class Boxes {
    : private int length;
    : private int width;
    : private int height;
    : private int volume;
    :
    : public void calculateVolume () {
    : volume = (this.getLength() * this.getWidth() * this.getHeight());
    : this.setVolume(volume);
    : }
    : public void setVolume(int myVolume) {
    : volume = myVolume;
    : }
    : public int getLength () {
    : return length;
    : }
    : public int getWidth () {
    : return width;
    : }
    : public int getHeight() {
    : return height;
    : }
    : public Boxes () {
    : length = 1;
    : width = 1;
    : height = 1;
    : }
    : }
    : [/code]:
    : I'm trying to call the calculateVolume method with a Boxes object
    : which is created with the following code:
    :
    : Boxes[] theArray = new Boxes[10];
    :
    : the way that I'm trying to call the method is :
    :
    : theArray[1].calculateVolume();
    :
    : theArray is created in another class other than Boxes which would
    : import Boxes. I know this version of Boxes doesn't show an import
    : statement, but the complete class does include it. I have just
    : simplified the Boxes class for this question.
    :
    : Why does the compiler keep telling me that it "cannot find symbol"
    : when I try to call the calculate Volume method? Can you offer any
    : suggestions for solving the problem?
    :
    Here are the two still unanswered questions:
    - Is theArray variable visible to the code, which calls calculateVolume()?
    - If you call calculateVolume from main(), is theArray static?
  • : : : : Can anyone help with a java problem? I'm creating an array of
    : : : : objects with the following code:
    : : : :
    : : : : Boxes[] theArray = new Boxes[10];
    : : : :
    : : : : This array theArray contains 10 cells with each pointing to an
    : : : : object of the Boxes class. The Boxes class just creates a cardboard
    : : : : box with a length, width, and height; each dimension is an instance
    : : : : variable of the Boxes class.
    : : : :
    : : : : In the Boxes class I have a method that will calculate the volume of
    : : : : a box. The code is as follows:
    : : : :
    : : : : public void calculateVolume (Boxes b) {
    : : : : volume = (this.getLength() * this.getWidth() * this.getHeight());
    : : : : this.setVolume(volume);
    : : : : }
    : : : :
    : : : : How do I use the array to call this method which lives in the boxes
    : : : : class? For example, I want to call
    : : : : theArray[1].calculateVolume(theArray[1]); to calculate the volume of
    : : : : a box in theArray cell#1, but I keep getting a compiler error
    : : : : "cannot find symbol". I have imported the Boxes class into the
    : : : : class that contains my main method where I created the array. What
    : : : : the heck am I doing wrong?
    : : : :
    : : : - Is calculateVolume() declared within the Boxes class?
    : : : - Is the Boxes class public?
    : : : - Is theArray variable visible to the code, which calls
    : : : calculateVolume()?
    : : : - If you call calculateVolume from main(), is theArray static?
    : : :
    : : : Note: You don't need the parameter, since it is unused.
    : :
    : :
    : : Thanks for the help. Below is the entire Boxes class. It is short
    : : and very simple.
    : :
    : : [code]: :
    : : public class Boxes {
    : : private int length;
    : : private int width;
    : : private int height;
    : : private int volume;
    : :
    : : public void calculateVolume () {
    : : volume = (this.getLength() * this.getWidth() * this.getHeight());
    : : this.setVolume(volume);
    : : }
    : : public void setVolume(int myVolume) {
    : : volume = myVolume;
    : : }
    : : public int getLength () {
    : : return length;
    : : }
    : : public int getWidth () {
    : : return width;
    : : }
    : : public int getHeight() {
    : : return height;
    : : }
    : : public Boxes () {
    : : length = 1;
    : : width = 1;
    : : height = 1;
    : : }
    : : }
    : : [/code]: :
    : : I'm trying to call the calculateVolume method with a Boxes object
    : : which is created with the following code:
    : :
    : : Boxes[] theArray = new Boxes[10];
    : :
    : : the way that I'm trying to call the method is :
    : :
    : : theArray[1].calculateVolume();
    : :
    : : theArray is created in another class other than Boxes which would
    : : import Boxes. I know this version of Boxes doesn't show an import
    : : statement, but the complete class does include it. I have just
    : : simplified the Boxes class for this question.
    : :
    : : Why does the compiler keep telling me that it "cannot find symbol"
    : : when I try to call the calculate Volume method? Can you offer any
    : : suggestions for solving the problem?
    : :
    : Here are the two still unanswered questions:
    : - Is theArray variable visible to the code, which calls
    : calculateVolume()?
    : - If you call calculateVolume from main(), is theArray static?

    Thanks. Here is the driver that I am using to call theArray. The main method is static, and I don't know if the theArray variable is visible to the code..maybe you can tell from the code:

    //import packages.assignments.util.*;


    public class ObjectExperiment {

    public static void main (String[] Args ) {

    //Creates a 10 cell array of objects.
    Object[] theArray = new Object [10];

    // Makes each cell in the array into a new Boxes object.
    for (int i=0; i<10; i++ ) {
    theArray[i] = new Boxes();
    }

    // Prints out each box in the array.
    for (int i=0; i<10; i++ ) {
    System.out.println(theArray[i]);

    }
    //This is the line of code that generates the error.
    theArray[0].calculateVolume();

    }
    }
  • : Thanks. Here is the driver that I am using to call theArray. The
    : main method is static, and I don't know if the theArray variable is
    : visible to the code..maybe you can tell from the code:
    :
    : //import packages.assignments.util.*;
    :
    :
    : public class ObjectExperiment {
    :
    : public static void main (String[] Args ) {
    :
    : //Creates a 10 cell array of objects.
    : [b][red]Object[] theArray = new Object [10];[/red][/b]
    :
    : // Makes each cell in the array into a new Boxes object.
    : for (int i=0; i<10; i++ ) {
    : theArray[i] = new Boxes();
    : }
    :
    : // Prints out each box in the array.
    : for (int i=0; i<10; i++ ) {
    : System.out.println(theArray[i]);
    :
    : }
    : //This is the line of code that generates the error.
    : theArray[0].calculateVolume();
    :
    : }
    : }
    :
    The problem is the definition of your array (red above). You haven't created an array of Boxes, but an array of Object. Then you filled that array of Object with 1 Boxes in each element. For the computer the array is still an array of Object. This means that each element only "knows" the methods listed in the definition of the Object class.
    Since the Object class doesn't know the calculateVolume(), the compiler gives a compilation error when trying to call calculateVolume().
    Solution: declare your array as an array of Boxes instead of Object.
  • : : Thanks. Here is the driver that I am using to call theArray. The
    : : main method is static, and I don't know if the theArray variable is
    : : visible to the code..maybe you can tell from the code:
    : :
    : : //import packages.assignments.util.*;
    : :
    : :
    : : public class ObjectExperiment {
    : :
    : : public static void main (String[] Args ) {
    : :
    : : //Creates a 10 cell array of objects.
    : : [b][red]Object[] theArray = new Object [10];[/red][/b]
    : :
    : : // Makes each cell in the array into a new Boxes object.
    : : for (int i=0; i<10; i++ ) {
    : : theArray[i] = new Boxes();
    : : }
    : :
    : : // Prints out each box in the array.
    : : for (int i=0; i<10; i++ ) {
    : : System.out.println(theArray[i]);
    : :
    : : }
    : : //This is the line of code that generates the error.
    : : theArray[0].calculateVolume();
    : :
    : : }
    : : }
    : :
    : The problem is the definition of your array (red above). You haven't
    : created an array of Boxes, but an array of Object. Then you filled
    : that array of Object with 1 Boxes in each element. For the computer
    : the array is still an array of Object. This means that each element
    : only "knows" the methods listed in the definition of the Object
    : class.
    : Since the Object class doesn't know the calculateVolume(), the
    : compiler gives a compilation error when trying to call
    : calculateVolume().
    : Solution: declare your array as an array of Boxes instead of Object.

    It worked perfectly. Thank you so much for your help. I wish I could buy you dinner or something for your time and effort!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories