Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't Create an Instance In a Method

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
Hi,

I'm new to Java, and I'm working on a tutorial. Things were going smoothly and now I'm absolutely stumped. I've been working on this one exercise for two days.

I'm a professional, not a student, but I'm teaching myself Java.

Here's what I've got. I have a Box class that creates a box of 3 dimensions (width, heigh, length), and I'm working with the concept of Encapsulation.

I have successfully created an "oldBox" specific dimensions. Now I'm trying to create a "bigBox" that uses the oldBox to make an even bigger box. I'm trying to use a public method called "biggerBox" that returns a reference a a new Box that will be 25% larger in each dimension than the old box.

In the tutorial, I was given the code for the public method as:


public Box4 biggerBox(Box4 oldB){

return new Box4(1.25 *oldB.width(),1.25 *oldB.length(),1.25 *oldB.height());

}


Here's my class:




class Box4{

//instance variables
private double width;
private double length;
private double height;



//constructors
Box4(double w, double l, double h){
width = w;
length = l;
height = h;
}

Box4(double side){
width = side;
length = side;
height = side;
}

Box4(Box4 oldB){

width = oldB.width();
length = oldB.length();
height = oldB.height();
}


//methods
public double volume(){
return (width * length * height);
}

public double area(){
return 2 * faceArea() + 2 * topArea() + 2 * sideArea();

}

private double faceArea(){
return (width * height);
}

private double topArea(){
return (height * length);
}

private double sideArea(){
return (width * length);
}

public double width(){
return width;
}

public double length(){
return length;
}

public double height(){
return height;
}

public Box4 biggerBox(Box4 oldB){

//first constructor from above is called here
return new Box4(1.25 *oldB.width(),1.25 *oldB.length(),1.25 *oldB.height());

}

} //class Box



Now here's my "main()" class:




class BoxTester4{

public static void main(String[] args){

Box4 oldBox = new Box4(2.5, 5.0, 6.0);

System.out.println("(OldBox)Area: " + oldBox.area() + " volume: " + oldBox.volume() );

Box4 newBox = new Box4(oldBox);

System.out.println("(NewBox)Dimensions: " + "width: " + newBox.width() +
" length: " + newBox.length() + " height: " + newBox.height());

//Goal:want to call the public method that returns the reference
//so I can can use later in next line

//why can't I say this?

Box4 bigBox = biggerBox(oldBox);

//Review My Result
System.out.println("(BigBox)Dimensions: " + "width: " + bigBox.width() +
" length: " + bigBox.length() + " height: " + bigBox.height());


}
} //class BoxTester



Box4.java compiles, but when I try to compile BoxTester4.java, I get this:


>javac BoxTester4.java
BoxTester4.java:19: cannot resolve symbol
symbol : method biggerBox (Box4)
location: class BoxTester4
Box4 bigBox = biggerBox(oldBox);
^
1 error




I tried different things like trying to make bigBox global, etc., but I haven't gotten that far in the tutorial and don't think that's where the author's going.

Any help you can provide is very much appreciated.

Thanks again,

scripter73



Change Your Thinking, Change Your Life.
 
class BoxTester4{
public static void main(String[] args){

Box4 oldBox = new Box4(2.5, 5.0, 6.0);

System.out.println("(OldBox)Area: " + oldBox.area() + " volume: " + oldBox.volume() );

Box4 newBox = new Box4(oldBox);

System.out.println("(NewBox)Dimensions: " + "width: " + newBox.width() +
" length: " + newBox.length() + " height: " + newBox.height());

//Goal:want to call the public method that returns the reference
//so I can can use later in next line

//why can't I say this?

Box4 bigBox =oldBox.biggerBox(oldBox);

//Review My Result
System.out.println("(BigBox)Dimensions: " + "width: " + bigBox.width() +
" length: " + bigBox.length() + " height: " + bigBox.height());


}
} //class BoxTester
You need an object of Box4 to call the method in Box4.
Look at article about static method if you want to call a method in another class without an instance of object.
 
The second hint is better.
Because if you call

oldBox.biggerBox (oldBox);

the information of 'oldBox' is used twice, as Object, which is called, and as parameter.
Of course you could rewrite the method, without parameters:


public Box4 toBiggerBox ()
{
return new Box4 (1.25*width, 1.25*length, 1.25*height);
}

and perhaps think of '1.25' as an parameter


public Box4 toResizedBox (float zoom)
{
return new Box4(zoom*width, zoom*length, zoom*height);
}


Or you make the Method static, meaning, you may access it without an Object (like Integer.parseInt (foo);)

public static Box4 toBiggerBox (ob)
{
return new Box4 (1.25*ob.width, 1.25*ob.length, 1.25*ob.height);
}

Mention: you may access private fields in a class of it's own, even if the instance is differnt (oldBox, newBox), and needent call length (); etc.
Of course it is not prohibited to call the Methods.
 
Hi prosper and stefanwagner,

Of course! I need to use the existing object before I can use the method. (Slap on forehead!)

It worked like a charm.

Thanks for all of your help.

scripter73


Change Your Thinking, Change Your Life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top