早上好,如果这个问题很复杂/令人困惑,我深表歉意。我正在学习 Java 入门课程,但我的一项作业遇到了一些困难!你们这里的人似乎对所有事情都有答案,所以我知道你可以提供帮助!
该任务是为某些形状(矩形和三角形)创建规范,但必须这样实现:
所有形状都必须实现计算其面积/周长的计算接口。shapemanger 类将计算出的形状存储在一个能够存储多达 10 个对象的数组中......
public class App
{
public static void main(String[] args)
{
ShapeManager sm = new ShapeManager();
Rectangle r1 = new Rectangle(100,120,10,13);
sm.addShape(r1);
sm.dump();
}
}
public interface Calculated
{
public double calculateArea();
public double calculatePerimeter();
public void dump();
}
public class Rectangle implements Calculated
{
private int xCoord, yCoord, length, width, rectangleArea, rectanglePerimeter;
public Calculated rectangleShape;
public Rectangle(int XCoord, int YCoord, int Length, int Width)
{
XCoord = xCoord;
YCoord = yCoord;
Length = length;
Width = width;
}
public double calculateArea()
{
rectangleArea = length * width;
return rectangleArea;
}
public double calculatePerimeter()
{
rectanglePerimeter = (length *2) + (width * 2);
return rectanglePerimeter;
}
public void dump()
{
System.out.println("The Area of the rectangle = " + rectangleArea);
System.out.println("The Perimeter of the rectangle =" + rectanglePerimeter);
}
}
public class ShapeManager
{
private Calculated[] calcArray;
private Calculated rectangle;
public Calculated shape;
public ShapeManager()
{
calcArray = new Calculated[10];
}
public void addShape(Calculated s)
{
int address = 0;
while (calcArray[address] != null)
{
address++;
}
calcArray[address] = s;
}
public void dump()
{
for (int x = 0; x <= 9; x++)
{
if (calcArray[x] !=null)
{
calcArray[x].dump();
}
}
}
}
目前我的输出是:
矩形的面积 = 0 矩形的周长 =0
我真的很困惑我做错了什么,以及为什么输出没有正确计算。非常感谢您对此的任何帮助,非常感谢您的帮助和耐心等待!
好利索