scripter73
Programmer
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.
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.