this (as in reference)

This class uses a FixDebugBox class to instantiate two Box objects

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2653 Views - Last Post: 06 March 2010 - 02:54 PM Rate Topic: -----

#1 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

this (as in reference)

Posted 06 March 2010 - 09:49 AM

Hello, I'm having a particular problem with this debugging exercise in my Java textbook. My goal is to have one class (FixDebugBox) instantiate two box objects in another class (FixDebugFour3). Right now this is what I have:

FixDebugFour3
public class FixDebugFour3
// This class uses a FixDebugBox class to instantiate two Box objects
{
  public static void main(String args[])
  {
	int width = 12,
        length = 10,
        height = 8;
		  
        FixDebugBox box1 = new FixDebugBox ();
        FixDebugBox box2 = new FixDebugBox(width, width, height);
        System.out.println("The dimensions of the first box are");
        box1.showData();
        System.out.println("The volume of the first box is");
        showVolume(box);
        System.out.println("The dimensions of the first box are");
        box2.showData();
        System.out.println("The volume of the second box is");
        showVolume(box2);
  }
  public static void showVolume(FixDebugBox)
  {
	double vol = aBox.getVolume();
        System.out.println(volume);
  }
  
}



FixDebugBox
public class FixDebugBox
{
//private data members
   private int width;
   private int length;
   private int height;
//public data members
   public DebugBox()
   {
    length = 1;
     width = 1;
     height = 1;
   }
   public DebugBox(int with, int lenght, int height)
   {
      this.width = width;
      this.length = length;
      this.height = height;
   }
   public void showData()
   {
     System.out.println("Width: "  + width + "  Length: " +
      length + "  Height: " + height);
   }
   public double getVolume()
   { 
     double vol = length * width * height;
  }
  
}



I added in the this because I thought that this.height, this.width, this.length would call the numbers from FixDebugFour3. After doing that and correcting some syntax errors made when I originally opened the files (mostly spelling errors). I saved and compiled. The result I had from compiling FixDebugFour3 came out to be:
 ----jGRASP exec: javac -g C:\Users\user\Documents\College Stuff\FixDebugFour3.java

FixDebugFour3.java:21: <identifier> expected
  public static void showVolume(FixDebugBox)
                                           ^
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.



I didn't know what to put for the identifier either. Was I supposed to call the FixDebugBox class so it could be identified? Got me confused for a long time.

Is This A Good Question/Topic? 0
  • +

Replies To: this (as in reference)

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: this (as in reference)

Posted 06 March 2010 - 09:54 AM

Your parameter needs a variable name. What you have in there now is just the type. I'm guessing it should be called 'aBox' since you don't have that declared/identified anywhere else.
Was This Post Helpful? 0
  • +
  • -

#3 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: this (as in reference)

Posted 06 March 2010 - 10:07 AM

So in the FixDebugFour3 class in this section
  public static void main(String args[])
  {
        int width = 12,
        length = 10,
        height = 8;



I would type in FixDebugBox = aBox
as shown:

  public static void main(String args[])
  {
        int width = 12,
        length = 10,
        height = 8;
        FixDebugBox = aBox



as well as in the FixDebugBox class in this section as shown:
public class FixDebugBox
{
//private data members
   private int width;
   private int length;
   private int height;
   private FixDebugBox (or) aBox


I added in the (or) because I was unsure if I should redeclare it just in case.
Once I give declare my parameter, the rest of the program should move smoothly?
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: this (as in reference)

Posted 06 March 2010 - 10:09 AM

No, that's not what KYA meant.

He meant that you needed to change the header of the showVolume() method to look like this...
public static void showVolume(FixDebugBox aBox)

The reason for this being that the parameter (aBox) needs a formal name, so that you can use it as a variable inside the method.
Was This Post Helpful? 0
  • +
  • -

#5 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: this (as in reference)

Posted 06 March 2010 - 10:26 AM

Ok, I gave the parameters because I must instantiate two objects. I decided to name them aBox and bBox respectively. This is what I have so far:
FixDebugFour3
public class FixDebugFour3
// This class uses a FixDebugBox class to instantiate two Box objects
{
  public static void main(String args[])
  {
        int width = 12;
        int length = 10;
        int height = 8;
                  
        FixDebugBox box1 = new FixDebugBox(length, width, height);
        FixDebugBox box2 = new FixDebugBox(length, width, height);
        System.out.println("The dimensions of the first box are");
        aBox.showData();
        System.out.println("The volume of the first box is");
        showVolume(aBox);
        System.out.println("The dimensions of the first box are");
        bBox.showData();
        System.out.println("The volume of the second box is");
        showVolume(bBox);
  }
  public static void showVolume(FixDebugBox aBox)
  {
        double vol = aBox.getVolume();
        System.out.println(volume);
		  double vol = bBox.getVolume();
		  System.out.println(volume);
  }
  
}



FixDebugBox
public class FixDebugBox
{
//private data members
   private int width;
   private int length;
   private int height;
//public data members
   {
    	length = 1;
     width = 1;
     height = 1;
   }
   public FixDebugBox(int with, int lenght, int height)
   {
      this.width = width;
      this.length = length;
      this.height = height;
   }
   public void showData()
   {
     System.out.println("Width: "  + width + "  Length: " +
      length + "  Height: " + height);
   }
   public double getVolume()
   { 
     double vol = length * width * height;
  }
  
}



I just wanted to make sure that the parameters give are ok. I've also noticed the public data members, which should be the default constructor. I think this is where the (this.) comes into play at this section
 public FixDebugBox(int with, int lenght, int height)
   {
      this.width = width;
      this.length = length;
      this.height = height;
   }


Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: this (as in reference)

Posted 06 March 2010 - 10:44 AM

You don't need the second set of statements in the showVolume() method. Whatever variable you send to the method as a parameter will have its information printed out.

public static void showVolume(FixDebugBox aBox)
{
    double vol = aBox.getVolume();
    System.out.println(volume);

    // whatever object you specify as the parameter will
    // always change, depending on how you call it.
}


If you call it like this...showVolume(box1) (box1 being the name of the object in your main method), then it will print box1's volume out.

showVolume(box2) will print out box2's volume.

Your calls in your main method need to reflect the examples, too. Your box variables exist there. aBox only exists inside of the body of showVolume().

And aside from misspelling length in the parameters of the constructor...it's good.

Hope this helps! :)
Was This Post Helpful? 1
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: this (as in reference)

Posted 06 March 2010 - 10:47 AM

Your showVolume() functions takes a box object parameter. That name you gave it 'aBox' is the only valid object you can play around with in there. Trying to call bBox inside that method should be throwing cannot find symbol or initialize errors of some nature. If you want to print both at the same time, your method needs two box parameters.


In fact, to avoid any confusion change the param name to something else, ex:

public static void showVolume(FixDebugBox theBoxISentToThisFunction){
        double vol = theBoxISentToThisFunction.getVolume();
        System.out.println(volume);
}



aBox and bBox do not exist to this method, only the box sent by parameter.
Was This Post Helpful? 1
  • +
  • -

#8 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: this (as in reference)

Posted 06 March 2010 - 11:17 AM

Thing is I felt comfortable with the aBox and bBox, if it's ok with you KYA. So every part I saw a box1 or box2, I switched it to the aBox or bBox respectively. Along with fixing a spelling error, I end up with this.
public class FixDebugFour3
// This class uses a FixDebugBox class to instantiate two Box objects
{
  public static void main(String args[])
  {
        int width = 12;
        int length = 10;
        int height = 8;
                  
        FixDebugBox aBox = new FixDebugBox(length, width, height);
        FixDebugBox bBox = new FixDebugBox(length, width, height);
        System.out.println("The dimensions of the first box are");
        aBox.showData();
        System.out.println("The volume of the first box is");
        showVolume(aBox);
        System.out.println("The dimensions of the first box are");
        bBox.showData();
        System.out.println("The volume of the second box is");
        showVolume(bBox);
  }
  public static void showVolume(FixDebugBox aBox)
  {
        double vol = aBox.getVolume();
        System.out.println(volume);
		  double vol = bBox.getVolume();
		  System.out.println(volume);
  }
  
}



FixDebugBox
public class FixDebugBox
{
//private data members
   private int width;
   private int length;
   private int height;
//public data members
   {
    length = 1;
     width = 1;
     height = 1;
   }
   public FixDebugBox(int with, int length, int height)
   {
      this.width = width;
      this.length = length;
      this.height = height;
   }
   public void showData()
   {
     System.out.println("Width: "  + width + "  Length: " +
      length + "  Height: " + height);
   }
   public double getVolume()
   { 
     double vol = length * width * height;
  }
  
}



I appreciate the help you're giving, saving me a lot of time.
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: this (as in reference)

Posted 06 March 2010 - 12:05 PM

The only reason I changed it was to avoid any potential confusion about the scope of that variable and you still have the same issue. That method is designed (or should be) to output the volume of one (and only one) Box object. Calling 'bBox' inside it as well is wrong and should not compile.


You should only call operations on the parameter whose name is 'aBox'. It's not the same as the 'aBox' in main(). Don't confuse the two.
Was This Post Helpful? 0
  • +
  • -

#10 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: this (as in reference)

Posted 06 March 2010 - 12:26 PM

Ok, I got rid of the aBox and bBox and went with what the debug exercise came with (box1 and box2). I also just deleted most of my additions so it can return to its original form when I got it and start from there. Now I have:

FixDebugFour3
public class FixDebugFour3
// This class uses a FixDebugBox class to instantiate two Box objects
{
  public static void main(String args[])
  {
        int width = 12;
        int length = 10;
        int height = 8;
                  
        FixDebugBox box1 = new FixDebugBox (length, width, height);
        FixDebugBox box2 = new FixDebugBox(length, width, height);
        System.out.println("The dimensions of the first box are");
        box1.showData();
        System.out.println("The volume of the first box is");
        showVolume(box1);
        System.out.println("The dimensions of the first box are");
        box2.showData();
        System.out.println("The volume of the second box is");
        showVolume(box2);
  }
  public static void showVolume(FixDebugBox aBox)
  {
        double vol = box1.getVolume();
        System.out.println(volume);
  }
  
}



Where the only errors I encountered in compiling them was:
 ----jGRASP exec: javac -g C:\Users\user\Documents\College Stuff\FixDebugFour3.java

FixDebugFour3.java:23: cannot find symbol
symbol  : variable box1
location: class FixDebugFour3
        double vol = box1.getVolume();
                     ^
FixDebugFour3.java:24: cannot find symbol
symbol  : variable volume
location: class FixDebugFour3
        System.out.println(volume);
                           ^
2 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.



So my next move would be to declare my vol (or volume). So it would look like this in this section of my exercise:

public class FixDebugFour3
// This class uses a FixDebugBox class to instantiate two Box objects
{
  public static void main(String args[])
  {
        int width = 12;
        int length = 10;
        int height = 8;
       int vol (volume?) = width * length * height;



I put (volume?) in there because I was confused as to what to declare it as. So after this step I have instantiated one Box object and need to instantiate another. But I at least instantiated one so I can have an easier time instantiating the second because of the note located at the beginning of the program.
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: this (as in reference)

Posted 06 March 2010 - 12:33 PM

Read the comments:

//the parameter is of the type FixDebugBox
//its name is 'aBox'
//you need to use the parameter's name on any FixDebugBox methods you call
//its name is 'aBox'

//you declare a double named 'vol' to hold the value of the box's volume
//this is wrong:
public static void showVolume(FixDebugBox aBox)
  {
        double vol = box1.getVolume();
        System.out.println(volume);
  }

//this is one right way to do it
public static void showVolume(FixDebugBox aBox)
  {
        double vol = aBox.getVolume();
        System.out.println(vol);//notice the variable name change
  }

//the other way:
public static void showVolume(FixDebugBox box1)
  {
        double volume = box1.getVolume(); //notice the variable name change
        System.out.println(volume);
  }




Don't go any farther until you understand the following:

1. The parameter has a name and that name is used to access the object's "stuff"
2. Variable names need to match. If you're getting cannot find or mismatch error check to see where you declared the variables
3. You don't even have to store it in a seperate variable:

public static void showVolume(FixDebugBox box1){
        System.out.println(box1.getVolume());
  }


Was This Post Helpful? 1
  • +
  • -

#12 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: this (as in reference)

Posted 06 March 2010 - 01:07 PM

Alright I understand variables needing to be the same, and I overlooked that while debugging. There were two different names used, but I fixed it so that there would only be one name; I chose:

public static void showVolume(FixDebugBox aBox)
  {
        double vol = aBox.getVolume();
        System.out.println(vol);
  }
  
}



I've also understood parameter more when to access another object's "stuff." And it's vital for this program to run without errors.

So far after compiling it again with the above fixed, I received one error:

 ----jGRASP exec: javac -g C:\Users\user\Documents\College Stuff\FixDebugFour3.java

FixDebugBox.java:27: missing return statement
  }
  ^
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.



This deals with my other file: FixDebugBox.java
public class FixDebugBox
{
//private data members
   private int width;
   private int length;
   private int height;
//public data members
   {
    length = 1;
     width = 1;
     height = 1;
   }
   public FixDebugBox(int with, int length, int height)
   {
      this.width = width;
      this.length = length;
      this.height = height;
   }
   public void showData()
   {
     System.out.println("Width: "  + width + "  Length: " +
      length + "  Height: " + height);
   }
   public double getVolume()
   { 
     double vol = length * width * height;
  	}
}


It seems like I must put in aBox or FixDebugFour3 in the file so it can do a call.

But referring back to the three things I must understand before moving forward, I am having trouble understanding #3.I am still a bit confused as to storing it into a separate variable. Is it because I have already stored it and can use it from the same source?
Was This Post Helpful? 0
  • +
  • -

#13 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: this (as in reference)

Posted 06 March 2010 - 01:19 PM

return vol in this method:


 public double getVolume(){ 
     double vol = length * width * height;
     return vol;
}


Was This Post Helpful? 1
  • +
  • -

#14 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: this (as in reference)

Posted 06 March 2010 - 01:32 PM

Alright it's debugged, the complete code is as follows:

FixDebugFour3
public class FixDebugFour3
// This class uses a FixDebugBox class to instantiate two Box objects
{
  public static void main(String args[])
  {
        int width = 12;
        int length = 10;
        int height = 8;
                  
        FixDebugBox box1 = new FixDebugBox (width, length, height);
        FixDebugBox box2 = new FixDebugBox(width, length, height);
        System.out.println("The dimensions of the first box are");
        box1.showData();
        System.out.println("The volume of the first box is");
        showVolume(box1);
        System.out.println("The dimensions of the first box are");
        box2.showData();
        System.out.println("The volume of the second box is");
        showVolume(box2);
  }
  public static void showVolume(FixDebugBox aBox)
  {
        double vol = aBox.getVolume();
        System.out.println(vol);
  }
  
}



FixDebugBox
public class FixDebugBox
{
//private data members
   private int width;
   private int length;
   private int height;
//public data members
   {
    length = 1;
     width = 1;
     height = 1;
   }
   public FixDebugBox(int with, int length, int height)
   {
      this.width = width;
      this.length = length;
      this.height = height;
   }
   public void showData()
   {
     System.out.println("Width: "  + width + "  Length: " +
      length + "  Height: " + height);
   }
   public double getVolume()
   { 
     double vol = length * width * height;
	  return vol;
  	}
}




Unfortunately, there's one problem that remains. When I ran the program, it came out to this:
 ----jGRASP exec: java FixDebugFour3

The dimensions of the first box are
Width: 1  Length: 10  Height: 8
The volume of the first box is
80.0
The dimensions of the first box are
Width: 1  Length: 10  Height: 8
The volume of the second box is
80.0

 ----jGRASP: operation complete.



I'm supposed to display the data of two different boxes. However the width, length, and height came out the same, when it was supposed to be different. I think I'm supposed to create an alternate constructor for the second box in the FixDebugBox.java file. So the problem would then lie in the this. references?
Was This Post Helpful? 0
  • +
  • -

#15 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: this (as in reference)

Posted 06 March 2010 - 01:38 PM

They came out the same because, in your main method, you called your constructor twice to construct two boxes with the same variables as parameters. The boxes are completely separate entities, but you just gave them the same attributes.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2