gpt4 book ai didi

Java无法找到变量矩形绘制/查找面积/查找周长

转载 作者:太空宇宙 更新时间:2023-11-04 13:29:32 25 4
gpt4 key购买 nike

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

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

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

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

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

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

矩形 rectA = new 矩形();

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

矩形 rectC = new 矩形(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 说它找不到变量宽度,尽管我已经列出来了。感谢您提前的帮助!

最佳答案

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

}
}

关于Java无法找到变量矩形绘制/查找面积/查找周长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32326307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com