Java 找不到变量矩形 Draw/find Area/find 周长

Java Can't find variable Rectangle Draw/find Area/find Perimeter

由于 java 无法找到我放置的变量的问题,我在这个赋值过程中遇到了问题。任务是: 实现具有以下属性的矩形 class。

在构造函数中指定了一个 Rectangle 对象,矩形的左右边缘分别为 x 和 x + 宽度。顶部和底部边缘位于 y 和 y + 高度。

方法 getPerimeter 计算并 returns 矩形的周长。

方法 getArea 计算和 returns 矩形的面积。

方法 draw 显示 Rectangle 对象的新实例。有关 DrawingTool 方法的详细信息,请参阅 DrawingTool API。

尝试使用默认构造函数和可以获取 x 和 y 坐标、矩形长度和宽度的构造函数创建矩形。以下是一些示例构造函数调用:

矩形 rectA = new Rectangle();

矩形 rectB = 新矩形(0, -80, 400, 160);

矩形 rectC = 新矩形(100, -100, 20, 300);

这是我的任务驱动程序:

public class Driver_class
{
public static void main(String[] args) {
        P4_Icel_Murad_Rectangle rectA = new P4_Icel_Murad_Rectangle();
        P4_Icel_Murad_Rectangle rectB = new P4_Icel_Murad_Rectangle(0,-80,400,160);
        P4_Icel_Murad_Rectangle rectC = new P4_Icel_Murad_Rectangle(100,-100,20,300);

}
}

和我的 main()

public class P4_Icel_Murad_Rectangle
{

    /**
     * Constructor for objects of class P4_Icel_Murad
     */
    public P4_Icel_Murad_Rectangle(double x, double y, double width, double height)
    {
        // initialise instance variables

        DrawingTool Pen;
        SketchPad Paper;
        //new sketchpad
        Paper = new SketchPad(500,500);
        Pen = new DrawingTool(Paper);
        getPerimeter();
        getArea();
        draw();        
    }
   //Constructor # 2
   public P4_Icel_Murad_Rectangle()
   {
       double x = 0;
         double y = 0;
         double width = 0;
         double height = 0;
       DrawingTool Pen;
        SketchPad Paper;
        //new sketchpad
        Paper = new SketchPad(500,500);
        Pen = new DrawingTool(Paper);
        getPerimeter();
        getArea();
        draw();
    }
public double getPerimeter(){
     double per = (width * 2) + height * 2;
    return per;
}
public double getArea(){
    double area = width * height;
    return area;
}
public void draw(){
 pen.down();
 pen.turnRight(90);
 pen.forward(x);
 pen.turnLeft(90);
 pen.forward(width);
 pen.turnLeft(90);
 pen.forward(height);
 pen.turnLeft();
 pen.forward(y);

}
}

Java说找不到可变宽度,虽然我已经列出来了。提前感谢您的帮助!

您的变量仅在构造函数的范围内声明。让他们 private 在 class.

public static class P4_Icel_Murad_Rectangle {

    SketchPad Paper = new SketchPad(500, 500);
    DrawingTool pen = new DrawingTool(Paper);

    double x = 0;
    double y = 0;
    double width = 0;
    double height = 0;

    /**
     * Constructor for objects of class P4_Icel_Murad
     */
    public P4_Icel_Murad_Rectangle(double x, double y, double width, double height) {
        // initialise instance variables

        getPerimeter();
        getArea();
        draw();
    }

    // Constructor # 2
    public P4_Icel_Murad_Rectangle() {
        getPerimeter();
        getArea();
        draw();
    }

    public double getPerimeter() {
        double per = (width * 2) + height * 2;
        return per;
    }

    public double getArea() {
        double area = width * height;
        return area;
    }

    public void draw() {
        pen.down();
        pen.turnRight(90);
        pen.forward(x);
        pen.turnLeft(90);
        pen.forward(width);
        pen.turnLeft(90);
        pen.forward(height);
        pen.turnLeft();
        pen.forward(y);

    }
}