Java 找不到变量 矩形绘制/查找区域/查找周边



由于java无法找到我放置的变量,我在执行此赋值时遇到了问题。任务是:实现一个具有以下属性的Rectangle类。

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

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

方法getArea计算并返回矩形的面积。

方法绘制显示矩形对象的新实例。有关DrawingTool方法的详细信息,请参阅DrawingTool API。

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

矩形rectA=新矩形((;

矩形rectB=新矩形(0,-80,400160(;

矩形rectC=新矩形(100,-100,20300(;

这是我的任务驱动程序:

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);
}
}

和我的主((

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说它找不到可变宽度,尽管我已经列出了它。感谢您的提前帮助!

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);
    }
}

您的变量仅在构造函数的作用域中声明。使它们成为课堂上的private

最新更新