简体   繁体   English

从实现的类向数组添加对象

[英]Adding objects to an array from an implemented class

Good morning, my apologies if this question is complex/confusing. 早上好,如果这个问题很复杂/令人困惑,我深表歉意。 I am in an intro java course and I am having some difficulties with one of my assignments! 我正在参加Java入门课程,我的一项作业遇到了一些困难! You guys on here seem to have an answer for everything so I know you can help! 你们在这里似乎对所有问题都有答案,所以我知道您可以提供帮助!

This assignment is to create specifications for some shapes (rectangles and triangles), but it must be implemeneted as such: 该任务是为某些形状(矩形和三角形)创建规格,但必须这样实现:

All shapes must implement a calculated interface which calculates their area/perimeter. 所有形状都必须实现一个计算接口,以计算其面积/周长。 A shapemanger class stores the calculated shapes in an array capable of storing up to 10 objects... 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();
            }
        }
    }
    }

Currently my output is: 目前我的输出是:

The Area of the rectangle = 0 The Perimeter of the rectangle =0 矩形的面积= 0矩形的周长= 0

I am really stuck on what I am doing wrong, and why the output isnt calculated properly. 我真的对我做错了,为什么输出计算不正确也感到困惑。 Any assistance on this is really appreciated, thank you very much for your help and your patience! 非常感谢您的协助,非常感谢您的帮助和耐心!

Allyso 盟友

Change your dump method to: dump方法更改为:

public void dump()
{
    System.out.println("The Area of the rectangle = " + calculateArea());
    System.out.println("The Perimeter of the rectangle =" + calculatePerimeter());
}

You can improve it further by removing the two fields: rectangleArea and rectanglePerimeter and make the calculate functions like this: 您可以通过删除以下两个字段来进一步改进它: rectangleArearectanglePerimeter ,并使计算函数如下所示:

public double calculateArea()
{  
    return length * width;
}

Now you save some memory as well. 现在,您还可以节省一些内存。 I would then rename the methods to getArea and getPerimeter . 然后,我将方法重命名为getAreagetPerimeter

You design needs some pruning of poor design choices, like caching the area etc: 您的设计需要修剪一些不良的设计选择,例如缓存区域等:

public interface Shape {
    public double getArea();
    public double getPerimeter();
    public void dump();
}

public class Rectangle implements Shape {
    private int xCoord, yCoord, length, width;

    public Rectangle(int XCoord, int YCoord, int Length, int Width) {
        XCoord = xCoord;
        YCoord = yCoord;
        Length = length;
        Width = width;
    }

    public double getArea() {
        return length * width;
    }

    public double getPerimeter() {
        return (length + width) * 2;
    }

    public void dump() {
        System.out.println("The Area of the rectangle = " + getArea());
        System.out.println("The Perimeter of the rectangle =" + getPerimeter());
    }
}

Replace the function 'dump' in 'Rectangle' class as below: 替换“矩形”类中的函数“转储”,如下所示:

public void dump()
{
    this.calculateArea();
    this.calculatePerimeter();
    System.out.println("The Area of the rectangle = " + rectangleArea);
    System.out.println("The Perimeter of the rectangle =" + rectanglePerimeter);
}

You could also do it in the loop inside 'dump' function in ShapeManager. 您也可以在ShapeManager中的“转储”功能内的循环中执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM