Java how to use toString method to return an accessor method

huangapple go评论68阅读模式
英文:

Java how to use toString method to return an accessor method

问题

public class Box {
    // Data field declarations
    public int height; // Height
    public int width; // Width
    public int length; // Length

    /** Sets the value of the data field height to input parameter value */
    public void setheight(int H) {
        height = H;
    }

    // End method

    /** Sets the value of the data field width to input parameter value */
    public void setwidth(int W) {
        width = W;
    }

    // End method

    /** Sets the value of the data field length to input parameter value */
    public void setlength(int L) {
        length = L;
    }

    // End method

    /** A constructor which takes 3 parameters **/
    public Box(int H, int L, int W) {
        height = H;
        length = L;
        width = W;
    }

    /** Constructor which creates a box **/
    public Box() {
    }

    /** Constructor which creates a cube **/
    public Box(int side) {
        height = side;
        length = side;
        width = side;
    }

    /** Returns the value of the data field SurfaceArea **/
    int getsurfaceArea() {
        return (2 * height) + (2 * width) + (2 * length);
    }

    // End method

    /** Returns the value of the data field volume */
    int getvolume() {
        return height * width * length;
    }

    // End method

    /**
     * Displays formatted Box information to the console window
     */
    public String toString() {
        return "Height is: " + height + ", Length is: " + length + ", Width is: " + width +
               ", Volume is: " + getvolume() + ", Surface Area: " + getsurfaceArea();
    }
    // End method
}
英文:

I've been working on this for a while and can't seem to get my head around it. I'm trying to calculate the volume and surface area from the length, height, and width (without putting the volume or surface area in the data fields) using the accessor method. I'm not sure why my code is coming up with these errors (see below) and would appreciate some educating as I'm really trying to learn how to code! Thank You Java how to use toString method to return an accessor method

Error: cannot find symbol
symbol: variable volume
location: class Box

Error: cannot find symbol
symbol: variable surfaceArea
location: class Box

public class Box {
//data field declarations
public int height; //height
public int width; //width
public int length; //length 
/**sets the value of the data field height to input parameter value*/
public void setheight(int H){
height = H;
}
//end method
/**sets the value of the data field width to input parameter value*/
public void setwidth(int W){
width = W;
} 
//end method
/**sets the value of the data field length to input parameter value*/
public void setlength(int L){
length = L;
}
//end method
/**A constructor which takes 3 parameters**/
public Box(int H, int L, int W){
H = height;
L = length;
W = width;
}
/**Constructor which creates a box**/
public Box(){
}
/**Constructor which creates a cube**/
public Box(int side){
side = height;
side = length;
side = width;
} 
/**returns the value of the data field SurfaceArea **/
int getsurfaceArea(){
return (2*height) + (2*width) + (2*length);
}
//end method
/**returns the value of the data field volume */
int getvolume(){
return height*width*length;;
}
//end method
/**displays formatted Box information to the console window */ 
public String toString(){
return "Height is: " + height + ", Length is: " + length + ", Width is: " + width + ", Volume is: " + volume + ", Surface Area: " + surfaceArea;
}
//end method

}

答案1

得分: 0

你的方法中存在一些编译错误:

return "Height is: " + height + ", Length is: " + length + ", Width is: " + width + ", Volume is: " + getvolume() + ", Surface Area: " + getsurfaceArea();

你将方法调用作为属性使用了。

英文:

You have some compilation Errors in your method :

   return "Height is: " + height + ", Length is: " + length + ", Width is: " + width + ", Volume is: " + getvolume() + ", Surface Area: " + getsurfaceArea();

You are calling methods as atributes.

答案2

得分: 0

以下是您要求的翻译内容:

在您的构造函数中,方法变量分配到类属性的顺序有误。

所以,您原本是这样的:

    /**
     * 一个包含3个参数的构造函数
     **/
    public Box(int H, int L, int W) {
        H = height;
        L = length;
        W = width;
    }

而您实际想要的是这样的:

    /**
     * 一个包含3个参数的构造函数
     **/
    public Box(int H, int L, int W) {
        this.height = H;
        this.length = L;
        this.width = W;
    }

等式的左侧应该是类成员,右侧是参数值。

使用this关键字可以很好地区分它们。

最后,在toString方法中,您需要调用计算体积和表面积的方法:

return "Height is: " + height + ", Length is: " + length + ", Width is: " + width + ", Volume is: " + getvolume() + ", Surface Area: " + getsurfaceArea();
英文:

In your constructors you have the method variable assignment to the class properties around the wrong way.

So where you have this:

    /**
     * A constructor which takes 3 parameters
     **/
    public Box(int H, int L, int W) {
        H = height;
        L = length;
        W = width;
    }

what you really want is this:

    /**
* A constructor which takes 3 parameters
**/
public Box(int H, int L, int W) {
this.height = H;
this.length = L;
this.width = W;
}

The left hand side of the = should be the class member and the right hand side the parameter value.

It might be a good idea to use the this keyword to keep track of which is which.

Finally in the toString method, you need to call the methods to calculate the volume and surface area

return "Height is: " + height + ", Length is: " + length + ", Width is: " + width + ", Volume is: " + getvolume() + ", Surface Area: " + getsurfaceArea()

huangapple
  • 本文由 发表于 2020年8月6日 09:25:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63275586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定